import type { TemplateResult } from "lit"; import type { AppState } from "../../src/client/src/appState"; import type { HtmlTemplateTag, PiWebPlugin } from "../../src/client/src/plugins/types"; import type { PiWebComponentStatus, PiWebInstallationInfo, PiWebStatusMessage, PiWebStatusResponse } from "../../src/shared/apiTypes"; function messagesFor(state: AppState): PiWebStatusMessage[] { return state.piWebStatus?.messages ?? []; } function statusFor(state: AppState): PiWebStatusResponse | undefined { return state.piWebStatus; } function messageCount(state: AppState): number { return messagesFor(state).length; } function isLocalOrUnknownInstallation(installation: PiWebInstallationInfo | undefined): boolean { return installation === undefined || installation.kind === "local" || installation.kind === "unknown"; } function shouldShowUpdatesPanel(state: AppState): boolean { const status = statusFor(state); if (messageCount(state) > 0) return true; if (status === undefined) return false; return isLocalOrUnknownInstallation(status.components.web.installation) || isLocalOrUnknownInstallation(status.components.sessiond.installation); } function formatVersion(version: string | undefined): string { return version === undefined || version === "" ? "unknown" : version; } function installationLabel(installation: PiWebInstallationInfo | undefined): string { if (installation === undefined) return "installation unknown"; if (installation.kind === "pi-package") { const scope = installation.scope === undefined ? "" : ` · ${installation.scope}`; const source = installation.source ?? "Pi package"; return `${source}${scope}`; } if (installation.kind === "npm-global") return "global npm package"; if (installation.kind === "local") return "local checkout"; return "installation unknown"; } function renderComponent(html: HtmlTemplateTag, component: PiWebComponentStatus): TemplateResult { const status = !component.available ? "unavailable" : component.stale ? "restart needed" : "current"; return html`
${component.label} ${status} running ${formatVersion(component.runtimeVersion)} · installed ${formatVersion(component.installedVersion)} ${installationLabel(component.installation)}${component.installation?.path === undefined ? "" : ` · ${component.installation.path}`}
`; } function renderCommand(html: HtmlTemplateTag, label: string, command: string): TemplateResult { return html`
${label} ${command}
`; } function renderCommands(html: HtmlTemplateTag, status: PiWebStatusResponse): TemplateResult | undefined { const commands = [ ["Update", status.commands.update], ["Restart all", status.commands.restart], ["Restart Web/UI", status.commands.restartWeb], ["Restart session daemon", status.commands.restartSessiond], ["Status", status.commands.status], ].filter((entry): entry is [string, string] => typeof entry[1] === "string" && entry[1] !== ""); if (commands.length === 0) return undefined; return html`
Suggested commands ${commands.map(([label, command]) => renderCommand(html, label, command))}
`; } function renderUpdatesPanel(html: HtmlTemplateTag, state: AppState): TemplateResult { const status = statusFor(state); if (status === undefined) { return html`
Updates

Checking PI WEB update status…

`; } const messages = status.messages; return html`
Updatesbeta${messages.length > 0 ? html`${String(messages.length)}` : null}
${messages.length === 0 ? html`

No PI WEB update or restart messages.

` : messages.map((message) => html`
${message.title}${message.severity}

${message.body}

${message.command === undefined ? null : html`${message.command}`}
`)}
Installed services ${renderComponent(html, status.components.web)} ${renderComponent(html, status.components.sessiond)}
${renderCommands(html, status)}
Generated ${status.generatedAt} ${status.release.latestVersion === undefined ? null : html`Latest npm release ${status.release.latestVersion}`} ${status.release.skipped === true ? html`Remote version check skipped.` : null} ${status.release.error === undefined ? null : html`Remote version check failed: ${status.release.error}`}
`; } const plugin: PiWebPlugin = { apiVersion: 1, name: "Updates", activate: ({ html, svg }) => ({ contributions: { workspacePanels: [ { id: "workspace.updates", title: "Updates", icon: svg` `, order: 100, visible: (context) => shouldShowUpdatesPanel(context.state), badge: (context) => { const count = messageCount(context.state); return html`beta${count > 0 ? html` · ${String(count)}` : null}`; }, render: (context) => renderUpdatesPanel(html, context.state), }, ], }, }), }; export default plugin;