The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg, useMessages } from "gt-react-native"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 4 | import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 5 | import { useMemo, useCallback, type Dispatch, type SetStateAction } from "react"; |
| 6 | |
| 7 | import { useProjects, useServerConfigs, useThreadShell } from "../../state/entities"; |
| 8 | import { useEnvironmentQuery } from "../../state/query"; |
| 9 | import { threadFamilies } from "../../state/threads"; |
| 10 | import { createDraft, scheduledTaskDefaultModel } from "./scheduledTaskDraft"; |
| 11 | import { useSettingsEnvironmentFilter } from "./settings-environment-filter"; |
| 12 | import { settingsTargetsForProject } from "./settings-environment-filter.logic"; |
| 13 | |
| 14 | import { |
| 15 | scheduledTaskEditorHasChanges, |
| 16 | scheduledTaskEditorSessionAtom, |
| 17 | setScheduledTaskEditorSaving, |
| 18 | startScheduledTaskEditor, |
| 19 | updateScheduledTaskEditor, |
| 20 | readScheduledTaskEditor, |
| 21 | type ScheduledTaskEditor, |
| 22 | } from "./scheduled-task-editor-state"; |
| 23 | |
| 24 | export const NEW_THREAD_DESTINATION_LABEL = msg("New thread each run"); |
| 25 | |
| 26 | export function useScheduledTaskThread( |
| 27 | environmentId: EnvironmentId | null, |
| 28 | threadId: ThreadId | null, |
| 29 | ) { |
| 30 | const ref = |
| 31 | environmentId === null || threadId === null ? null : scopeThreadRef(environmentId, threadId); |
| 32 | const shell = useThreadShell(ref); |
| 33 | // The shell can omit older settled threads; their family read still carries the row. |
| 34 | const family = useEnvironmentQuery( |
| 35 | ref !== null && shell === null |
| 36 | ? threadFamilies.familyAtom(ref.environmentId, ref.threadId) |
| 37 | : null, |
| 38 | ); |
| 39 | return shell ?? family.data?.threads.find((row) => row.id === threadId) ?? null; |
| 40 | } |
| 41 | |
| 42 | export function useScheduledTaskDestinationLabel( |
| 43 | environmentId: EnvironmentId | null, |
| 44 | threadId: ThreadId | null, |
| 45 | ): string { |
| 46 | const m = useMessages(); |
| 47 | const thread = useScheduledTaskThread(environmentId, threadId); |
| 48 | if (environmentId === null || threadId === null) return m(NEW_THREAD_DESTINATION_LABEL); |
| 49 | return thread?.title.trim() || m(msg("Thread {id}", { id: threadId })); |
| 50 | } |
| 51 | |
| 52 | /** The form draft lives in an atom, so it survives while its native picker routes are on top. */ |
| 53 | export function useScheduledTaskEditor() { |
| 54 | const { selectedTargets, selectedProjectKey, projectGroups } = useSettingsEnvironmentFilter(); |
| 55 | const selectedGroup = projectGroups.find((group) => group.key === selectedProjectKey); |
| 56 | const configs = useServerConfigs(); |
| 57 | const projects = useProjects(); |
| 58 | const defaultTarget = settingsTargetsForProject( |
| 59 | selectedTargets, |
| 60 | selectedProjectKey === null ? null : selectedGroup, |
| 61 | )[0]; |
| 62 | const draftForEnvironment = useCallback( |
| 63 | (environmentId: EnvironmentId) => { |
| 64 | const environmentProjects = projects.filter( |
| 65 | (project) => project.environmentId === environmentId, |
| 66 | ); |
| 67 | const project = |
| 68 | environmentProjects.find((project) => |
| 69 | selectedGroup?.members.some( |
| 70 | (member) => |
| 71 | member.project.environmentId === environmentId && member.project.id === project.id, |
| 72 | ), |
| 73 | ) ?? |
| 74 | environmentProjects.find( |
| 75 | (project) => project.pinnedAt != null && project.archivedAt == null, |
| 76 | ) ?? |
| 77 | environmentProjects.find((project) => project.archivedAt == null); |
| 78 | return createDraft( |
| 79 | project?.id ?? null, |
| 80 | scheduledTaskDefaultModel(configs.get(environmentId) ?? null, project ?? null), |
| 81 | ); |
| 82 | }, |
| 83 | [configs, projects, selectedGroup], |
| 84 | ); |
| 85 | // Direct links can open the new-task form before environments have connected. |
| 86 | const defaultEditor = useMemo( |
| 87 | () => |
| 88 | defaultTarget |
| 89 | ? { |
| 90 | environmentId: defaultTarget.environmentId, |
| 91 | environmentLabel: defaultTarget.label, |
| 92 | draft: draftForEnvironment(defaultTarget.environmentId), |
| 93 | } |
| 94 | : null, |
| 95 | [defaultTarget, draftForEnvironment], |
| 96 | ); |
| 97 | const session = useAtomValue(scheduledTaskEditorSessionAtom); |
| 98 | const voiceOwnerKey = `scheduled-task:${session?.id ?? 0}`; |
| 99 | const editor = session ? session.current : defaultEditor; |
| 100 | const initial = session ? session.initial : defaultEditor; |
| 101 | const hasChanges = scheduledTaskEditorHasChanges(initial, editor); |
| 102 | const saving = session?.saving ?? false; |
| 103 | const setSaving = (next: boolean) => setScheduledTaskEditorSaving(next, defaultEditor); |
| 104 | const setEditor: Dispatch<SetStateAction<ScheduledTaskEditor | null>> = (update) => { |
| 105 | updateScheduledTaskEditor(update, defaultEditor); |
| 106 | }; |
| 107 | const startEditor = startScheduledTaskEditor; |
| 108 | const resetEditor = () => startEditor(defaultEditor); |
| 109 | const readEditor = () => readScheduledTaskEditor(voiceOwnerKey, defaultEditor); |
| 110 | return { |
| 111 | editor, |
| 112 | voiceOwnerKey, |
| 113 | readEditor, |
| 114 | setEditor, |
| 115 | startEditor, |
| 116 | hasChanges, |
| 117 | saving, |
| 118 | setSaving, |
| 119 | resetEditor, |
| 120 | draftForEnvironment, |
| 121 | }; |
| 122 | } |