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