From d6b5cff6c7831709e7dc3ddbd67717359ef4b3b5 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:27:32 +0800 Subject: [PATCH] Import apps/browser/src/common.ts --- apps/browser/src/common.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 apps/browser/src/common.ts diff --git a/apps/browser/src/common.ts b/apps/browser/src/common.ts new file mode 100644 index 0000000..7971dc6 --- /dev/null +++ b/apps/browser/src/common.ts @@ -0,0 +1,34 @@ +import type { RemoteHost } from './types.js' + +/** Full serializable launcher state pushed from the service worker to the popup. */ +export interface AppSettings { serverUrl: string } +export interface AppState { + settings?: AppSettings + authorized: boolean + account?: string + hosts: RemoteHost[] + refreshing: boolean + openingHostId?: string + notice?: string + authorizationBusy: boolean + authorizationError?: string +} + +export const emptyState: AppState = { + authorized: false, + hosts: [], + refreshing: false, + authorizationBusy: false, +} + +export function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +export function relativeTime(value: number): string { + const elapsed = Math.max(0, Date.now() - value) + if (elapsed < 60_000) return 'now' + if (elapsed < 3_600_000) return `${Math.floor(elapsed / 60_000)}m ago` + if (elapsed < 86_400_000) return `${Math.floor(elapsed / 3_600_000)}h ago` + return `${Math.floor(elapsed / 86_400_000)}d ago` +}