• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import m from 'mithril';
16import {
17  PreflightCheck,
18  PreflightCheckResult,
19  WithPreflightChecks,
20} from '../interfaces/connection_check';
21import {Spinner} from '../../../widgets/spinner';
22import {Icon} from '../../../widgets/icon';
23import {linkify} from '../../../base/mithril_utils';
24
25type PreflightCheckWithResult = PreflightCheck & {
26  result?: PreflightCheckResult;
27};
28
29export class PreflightCheckRenderer {
30  private results = new Array<PreflightCheckWithResult>();
31  private allChecksCompleted = false;
32  private numChecksFailed = 0;
33
34  constructor(private testTarget: WithPreflightChecks) {}
35
36  async runPreflightChecks(): Promise<boolean> {
37    this.allChecksCompleted = false;
38    this.numChecksFailed = 0;
39    for await (const check of this.testTarget.runPreflightChecks()) {
40      const entry: PreflightCheckWithResult = {...check, result: check.status};
41      this.results.push(entry);
42      this.numChecksFailed += check.status.ok ? 0 : 1;
43      m.redraw();
44    }
45    this.allChecksCompleted = true;
46    m.redraw();
47    return this.numChecksFailed === 0;
48  }
49
50  renderIcon(): m.Children {
51    const attrs = {filled: true, className: 'preflight-checks-icon'};
52    if (!this.allChecksCompleted) {
53      return m(Spinner);
54    }
55    if (this.numChecksFailed > 0) {
56      attrs.className += ' ok';
57      return m(Icon, {icon: 'report', ...attrs});
58    }
59    attrs.className += ' error';
60    return m(Icon, {icon: 'check_circle', ...attrs});
61  }
62
63  renderTable(): m.Children {
64    return m(
65      'table.preflight-checks-table',
66      this.results.map((res) =>
67        m(
68          'tr',
69          m('td', res.name),
70          m(
71            'td',
72            res.result === undefined
73              ? m(Spinner)
74              : res.result.ok
75                ? m('span.ok', linkify(res.result.value))
76                : m('span.error', linkify(res.result.error)),
77            res.remediation && m('div', m(res.remediation)),
78          ),
79        ),
80      ),
81    );
82  }
83}
84