|
17 | 17 | import React from "react"
|
18 | 18 | import prettyMillis from "pretty-ms"
|
19 | 19 | import { Profiles } from "madwizard"
|
| 20 | +import { Loading } from "@kui-shell/plugin-client-common" |
20 | 21 | import { Grid, GridItem, Tile } from "@patternfly/react-core"
|
21 | 22 |
|
| 23 | +import ProfileWatcher from "../../tray/watchers/profile/list" |
| 24 | + |
22 | 25 | import PlusIcon from "@patternfly/react-icons/dist/esm/icons/user-plus-icon"
|
23 | 26 | import ProfileIcon from "@patternfly/react-icons/dist/esm/icons/user-icon"
|
24 | 27 |
|
| 28 | +type Props = Record<string, never> |
| 29 | + |
| 30 | +type State = { |
| 31 | + watcher: ProfileWatcher |
| 32 | + profiles: Profiles.Profile[] |
| 33 | + catastrophicError?: unknown |
| 34 | +} |
| 35 | + |
| 36 | +class ProfileExplorer extends React.PureComponent<Props, State> { |
| 37 | + public constructor(props: Props) { |
| 38 | + super(props) |
| 39 | + this.init() |
| 40 | + } |
| 41 | + |
| 42 | + private readonly updateFn = () => { |
| 43 | + // slice to force a render; TODO we could do a comparison to avoid |
| 44 | + // false re-renders if we want to get fancy |
| 45 | + this.setState((curState) => ({ |
| 46 | + profiles: curState.watcher.profiles.slice(), |
| 47 | + })) |
| 48 | + } |
| 49 | + |
| 50 | + private async init() { |
| 51 | + try { |
| 52 | + const watcher = await new ProfileWatcher(this.updateFn, await Profiles.profilesPath({}, true)).init() |
| 53 | + this.setState({ |
| 54 | + watcher, |
| 55 | + profiles: [], |
| 56 | + }) |
| 57 | + } catch (err) { |
| 58 | + console.error(err) |
| 59 | + this.setState({ catastrophicError: err }) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public componentWillUnmount() { |
| 64 | + if (this.state && this.state.watcher) { |
| 65 | + this.state.watcher.close() |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public render() { |
| 70 | + if (this.state && this.state.catastrophicError) { |
| 71 | + return "Internal Error" |
| 72 | + } else if (!this.state || !this.state.profiles) { |
| 73 | + return <Loading /> |
| 74 | + } else { |
| 75 | + return ( |
| 76 | + <Grid className="codeflare--gallery-grid flex-fill sans-serif top-pad left-pad right-pad bottom-pad" hasGutter> |
| 77 | + {this.state.profiles.map((_) => ( |
| 78 | + <GridItem key={_.name}> |
| 79 | + <Tile className="codeflare--tile" title={_.name} icon={<ProfileIcon />} isStacked> |
| 80 | + {`Last used ${prettyMillis(Date.now() - _.lastUsedTime, { compact: true })} ago`} |
| 81 | + </Tile> |
| 82 | + </GridItem> |
| 83 | + ))} |
| 84 | + |
| 85 | + { |
| 86 | + <GridItem> |
| 87 | + <Tile className="codeflare--tile codeflare--tile-new" title="New Profile" icon={<PlusIcon />} isStacked> |
| 88 | + Customize a profile |
| 89 | + </Tile> |
| 90 | + </GridItem> |
| 91 | + } |
| 92 | + </Grid> |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
25 | 98 | // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
26 | 99 | export default async function getProfiles() {
|
27 |
| - const profiles = await Profiles.list({}) |
28 |
| - |
29 | 100 | return {
|
30 |
| - react: ( |
31 |
| - <Grid className="codeflare--gallery-grid flex-fill sans-serif top-pad left-pad right-pad bottom-pad" hasGutter> |
32 |
| - {profiles.map((_) => ( |
33 |
| - <GridItem key={_.profile.name}> |
34 |
| - <Tile className="codeflare--tile" title={_.profile.name} icon={<ProfileIcon />} isStacked> |
35 |
| - {`Last used ${prettyMillis(Date.now() - _.profile.lastUsedTime, { compact: true })} ago`} |
36 |
| - </Tile> |
37 |
| - </GridItem> |
38 |
| - ))} |
39 |
| - |
40 |
| - { |
41 |
| - <GridItem> |
42 |
| - <Tile className="codeflare--tile codeflare--tile-new" title="New Profile" icon={<PlusIcon />} isStacked> |
43 |
| - Customize a profile |
44 |
| - </Tile> |
45 |
| - </GridItem> |
46 |
| - } |
47 |
| - </Grid> |
48 |
| - ), |
| 101 | + react: <ProfileExplorer />, |
49 | 102 | }
|
50 | 103 | }
|
0 commit comments