|
| 1 | +import { createMemo } from "solid-js" |
| 2 | +import type { Session } from "@opencode-ai/schema/session" |
| 3 | +import { useLanguage } from "@/context/language" |
| 4 | +import { useServer } from "@/context/server" |
| 5 | +import { useSessionEntities } from "@/context/session-entities-provider" |
| 6 | +import { useTabs } from "@/context/tabs" |
| 7 | +import { createSessionLifecycleClient, lifecycleRequestID } from "@/utils/session-lifecycle-client" |
| 8 | +import { showToast } from "@/utils/toast" |
| 9 | +import { errorMessage } from "@/pages/layout/helpers" |
| 10 | + |
| 11 | +/** |
| 12 | + * Archived sessions for the active server, read directly from the entity store -- the same |
| 13 | + * store home/tabs/etc. read, so this view can never disagree with them about what is archived. |
| 14 | + * No project grouping, no markdown prefetch, no command-palette registration: this is a much |
| 15 | + * smaller surface than home's session list, and borrowing that heavier apparatus for it would |
| 16 | + * be scope creep, not reuse. |
| 17 | + */ |
| 18 | +export function createArchivedController() { |
| 19 | + const server = useServer() |
| 20 | + const entities = useSessionEntities() |
| 21 | + const tabs = useTabs() |
| 22 | + const language = useLanguage() |
| 23 | + |
| 24 | + const records = createMemo(() => { |
| 25 | + const prefix = `${server.key} ` |
| 26 | + return Object.entries(entities.state.entities) |
| 27 | + .filter(([key, entity]) => key.startsWith(prefix) && entity.value?.lifecycle.state === "archived") |
| 28 | + .map(([, entity]) => entity.value as Session.Info) |
| 29 | + // `time.updated` is typed as the decoded `DateTimeUtcFromMillis` schema shape, but these |
| 30 | + // objects never actually go through the schema decoder (they arrive as raw client-next |
| 31 | + // JSON, see session-entities-sync.tsx's `as unknown as Session.Info` cast) -- at runtime |
| 32 | + // it is still a plain millis number, same as `compareSessionTime`'s V1-shaped equivalent. |
| 33 | + .sort((a, b) => Number(b.time.updated) - Number(a.time.updated)) |
| 34 | + }) |
| 35 | + |
| 36 | + const restore = async (session: Session.Info) => { |
| 37 | + const conn = server.current |
| 38 | + if (!conn || conn.type !== "http") return |
| 39 | + const client = createSessionLifecycleClient(conn.http) |
| 40 | + try { |
| 41 | + await client.restore({ |
| 42 | + sessionID: session.id, |
| 43 | + requestID: lifecycleRequestID(), |
| 44 | + expectedLifecycleRevision: session.lifecycleRevision, |
| 45 | + }) |
| 46 | + // No optimistic status here (unlike archive/trash) -- the design deliberately does not |
| 47 | + // list a `restore_pending` state, and the real `session.updated`/`lifecycle.changed` |
| 48 | + // event that follows re-fetches and dispatches for real within a turn or two. The row |
| 49 | + // leaving this list happens because the entity store says so, same discipline as archive. |
| 50 | + } catch (cause) { |
| 51 | + showToast({ |
| 52 | + title: language.t("common.requestFailed"), |
| 53 | + description: errorMessage(cause, language.t("common.requestFailed")), |
| 54 | + }) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + copy: { language }, |
| 60 | + records, |
| 61 | + restore, |
| 62 | + isOpenTab: (session: Session.Info) => |
| 63 | + tabs.store.some((tab) => tab.type === "session" && tab.server === server.key && tab.sessionId === session.id), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export type ArchivedController = ReturnType<typeof createArchivedController> |
0 commit comments