|
| 1 | +import { createMemo, onMount } from "solid-js" |
| 2 | +import { useSync } from "@tui/context/sync" |
| 3 | +import { DialogSelect, type DialogSelectOption } from "@tui/ui/dialog-select" |
| 4 | +import type { TextPart } from "@opencode-ai/sdk/v2" |
| 5 | +import { Locale } from "@/util/locale" |
| 6 | +import { useSDK } from "@tui/context/sdk" |
| 7 | +import { useRoute } from "@tui/context/route" |
| 8 | +import { useDialog } from "../../ui/dialog" |
| 9 | + |
| 10 | +export function DialogForkFromTimeline(props: { |
| 11 | + sessionID: string |
| 12 | + onMove: (messageID: string) => void |
| 13 | +}) { |
| 14 | + const sync = useSync() |
| 15 | + const dialog = useDialog() |
| 16 | + const sdk = useSDK() |
| 17 | + const route = useRoute() |
| 18 | + |
| 19 | + onMount(() => { |
| 20 | + dialog.setSize("large") |
| 21 | + }) |
| 22 | + |
| 23 | + const options = createMemo((): DialogSelectOption<string>[] => { |
| 24 | + const messages = sync.data.message[props.sessionID] ?? [] |
| 25 | + const result = [] as DialogSelectOption<string>[] |
| 26 | + for (const message of messages) { |
| 27 | + if (message.role !== "user") continue |
| 28 | + const part = (sync.data.part[message.id] ?? []).find( |
| 29 | + (x) => x.type === "text" && !x.synthetic && !x.ignored, |
| 30 | + ) as TextPart |
| 31 | + if (!part) continue |
| 32 | + result.push({ |
| 33 | + title: part.text.replace(/\n/g, " "), |
| 34 | + value: message.id, |
| 35 | + footer: Locale.time(message.time.created), |
| 36 | + onSelect: async (dialog) => { |
| 37 | + const forked = await sdk.client.session.fork({ |
| 38 | + sessionID: props.sessionID, |
| 39 | + messageID: message.id, |
| 40 | + }) |
| 41 | + route.navigate({ |
| 42 | + sessionID: forked.data!.id, |
| 43 | + type: "session", |
| 44 | + }) |
| 45 | + dialog.clear() |
| 46 | + }, |
| 47 | + }) |
| 48 | + } |
| 49 | + result.reverse() |
| 50 | + return result |
| 51 | + }) |
| 52 | + |
| 53 | + return <DialogSelect onMove={(option) => props.onMove(option.value)} title="Fork from message" options={options()} /> |
| 54 | +} |
0 commit comments