list.tsx 5.3 KB
Newer Older
A
Asher 已提交
1 2 3 4 5 6
import * as React from "react"
import { Application, isExecutableApplication, isRunningApplication } from "../../common/api"
import { HttpError } from "../../common/http"
import { getSession, killSession } from "../api"
import { RequestError } from "../components/error"

A
Asher 已提交
7 8 9
/**
 * An application's details (name and icon).
 */
A
Asher 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
export const AppDetails: React.FunctionComponent<Application> = (props) => {
  return (
    <>
      {props.icon ? (
        <img className="icon" src={`data:image/png;base64,${props.icon}`}></img>
      ) : (
        <div className="icon -missing"></div>
      )}
      <div className="name">{props.name}</div>
    </>
  )
}

export interface AppRowProps {
  readonly app: Application
  onKilled(app: Application): void
  open(app: Application): void
}

A
Asher 已提交
29 30 31
/**
 * A single application row. Can be killed if it's a running application.
 */
A
Asher 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
export const AppRow: React.FunctionComponent<AppRowProps> = (props) => {
  const [killing, setKilling] = React.useState<boolean>(false)
  const [error, setError] = React.useState<HttpError>()

  function kill(): void {
    if (isRunningApplication(props.app)) {
      setKilling(true)
      killSession(props.app)
        .then(() => {
          setKilling(false)
          props.onKilled(props.app)
        })
        .catch((error) => {
          setError(error)
          setKilling(false)
        })
    }
  }

  return (
    <div className="app-row">
      <button className="launch" onClick={(): void => props.open(props.app)}>
        <AppDetails {...props.app} />
      </button>
      {isRunningApplication(props.app) && !killing ? (
        <button className="kill" onClick={(): void => kill()}>
          {error ? error.message : killing ? "..." : "kill"}
        </button>
      ) : (
        undefined
      )}
    </div>
  )
}

export interface AppListProps {
  readonly header: string
  readonly apps?: ReadonlyArray<Application>
  open(app: Application): void
  onKilled(app: Application): void
}

A
Asher 已提交
74 75 76 77 78
/**
 * A list of applications. If undefined, show loading text. If empty, show a
 * message saying no items are found. Applications can be clicked and killed
 * (when applicable).
 */
A
Asher 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
export const AppList: React.FunctionComponent<AppListProps> = (props) => {
  return (
    <div className="app-list">
      <h2 className="header">{props.header}</h2>
      {props.apps && props.apps.length > 0 ? (
        props.apps.map((app, i) => <AppRow key={i} app={app} {...props} />)
      ) : props.apps ? (
        <RequestError error={`No ${props.header.toLowerCase()} found`} />
      ) : (
        <div className="loader">loading...</div>
      )}
    </div>
  )
}

export interface ApplicationSection {
  readonly apps?: ReadonlyArray<Application>
  readonly header: string
}

export interface AppLoaderProps {
  readonly app?: Application
  setApp(app?: Application): void
  getApps(): Promise<ReadonlyArray<ApplicationSection>>
}

/**
A
Asher 已提交
106 107
 * Application sections/groups. Handles loading of the application
 * sections, errors, opening applications, and killing applications.
A
Asher 已提交
108 109 110
 */
export const AppLoader: React.FunctionComponent<AppLoaderProps> = (props) => {
  const [apps, setApps] = React.useState<ReadonlyArray<ApplicationSection>>()
A
Asher 已提交
111
  const [error, setError] = React.useState<HttpError | Error>()
A
Asher 已提交
112 113 114 115 116 117 118 119

  const refresh = (): void => {
    props
      .getApps()
      .then(setApps)
      .catch((e) => setError(e.message))
  }

A
Asher 已提交
120
  // Every time the component loads go ahead and refresh the list.
A
Asher 已提交
121 122 123 124
  React.useEffect(() => {
    refresh()
  }, [props])

A
Asher 已提交
125 126 127 128
  /**
   * Open an application if not already open. For executable applications create
   * a session first.
   */
A
Asher 已提交
129
  function open(app: Application): void {
A
Asher 已提交
130 131 132
    if (props.app && props.app.name === app.name) {
      return setError(new Error(`${app.name} is already open`))
    }
A
Asher 已提交
133 134 135 136 137 138
    props.setApp(app)
    if (!isRunningApplication(app) && isExecutableApplication(app)) {
      getSession(app)
        .then((session) => {
          props.setApp({ ...app, ...session })
        })
A
Asher 已提交
139 140 141 142
        .catch((error) => {
          props.setApp(undefined)
          setError(error)
        })
A
Asher 已提交
143 144 145
    }
  }

A
Asher 已提交
146 147 148
  // In the case of an error fetching the apps, have the ability to try again.
  // In the case of failing to load an app, have the ability to go back to the
  // list (where the user can try again if they wish).
A
Asher 已提交
149 150 151 152
  if (error) {
    return (
      <RequestError
        error={error}
A
Asher 已提交
153
        onCloseText={props.app ? "Go Back" : "Try Again"}
A
Asher 已提交
154 155
        onClose={(): void => {
          setError(undefined)
A
Asher 已提交
156 157 158
          if (!props.app) {
            refresh()
          }
A
Asher 已提交
159 160 161 162 163
        }}
      />
    )
  }

A
Asher 已提交
164
  // If an app is currently loading, provide the option to cancel.
A
Asher 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  if (props.app && !props.app.loaded) {
    return (
      <div className="app-loader">
        <div className="opening">Opening</div>
        <div className="app-row">
          <AppDetails {...props.app} />
        </div>
        <button
          className="cancel"
          onClick={(): void => {
            props.setApp(undefined)
          }}
        >
          Cancel
        </button>
      </div>
    )
  }

A
Asher 已提交
184
  // Apps are currently loading.
A
Asher 已提交
185 186 187
  if (!apps) {
    return (
      <div className="app-loader">
A
Asher 已提交
188
        <div className="loader">loading...</div>
A
Asher 已提交
189 190 191 192
      </div>
    )
  }

A
Asher 已提交
193
  // Apps have loaded.
A
Asher 已提交
194 195 196 197 198 199 200 201
  return (
    <>
      {apps.map((section, i) => (
        <AppList key={i} open={open} onKilled={refresh} {...section} />
      ))}
    </>
  )
}