Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ScheduledTaskPickerScreens.tsx

166 lines5.5 KB

1import { useNavigation } from "@react-navigation/native";
2import * as Haptics from "expo-haptics";
3import { useMemo, useState } from "react";
4​
5import { AppText as Text } from "../../components/ui/AppText";
6import { buildModelOptions, groupByProvider } from "../../lib/modelOptions";
7import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
8import { useEnvironmentServerConfig, useProjects } from "../../state/entities";
9import { useDebouncedValue, usePaginatedBranches } from "../../state/queries";
10import { BranchPickerScreen } from "../threads/NewTaskContextPickerScreens";
11import { ThreadSettingsPickerScreen } from "../threads/ThreadSettingsSheet";
12import { SettingsScreen } from "./components/SettingsScreen";
13import { useScheduledTaskEditor } from "./scheduled-task-editor";
14​
15function MissingTaskDraft() {
16 return (
17 <SettingsScreen title="Scheduled task">
18 <Text className="p-5 text-base text-foreground-muted">Open a scheduled task form first.</Text>
19 </SettingsScreen>
20 );
22​
23export function ScheduledTaskModelPickerRouteScreen() {
24 const navigation = useNavigation();
25 const { editor, setEditor } = useScheduledTaskEditor();
26 const config = useEnvironmentServerConfig(editor?.environmentId ?? null);
27 const selectedModel = editor?.draft.modelSelection ?? null;
28 const projectId = editor?.draft.projectId ?? null;
29 const models = useMemo(
30 () => buildModelOptions(config, selectedModel, undefined, projectId),
31 [config, selectedModel, projectId],
32 );
33 const providerGroups = useMemo(() => groupByProvider(models), [models]);
34 const selectedOption = models.find(
35 (option) =>
36 option.selection.instanceId === selectedModel?.instanceId &&
37 option.selection.model === selectedModel.model,
38 );
39 const optionDescriptors = useMemo(
40 () =>
41 resolveProviderOptionDescriptors({
42 capabilities: selectedOption?.capabilities,
43 selections: selectedModel?.options,
44 }),
45 [selectedOption?.capabilities, selectedModel?.options],
46 );
47​
48 if (!editor) return <MissingTaskDraft />;
49​
50 return (
51 <ThreadSettingsPickerScreen
52 environmentId={editor.environmentId}
53 projectId={projectId}
54 providerGroups={providerGroups}
55 selectedModel={selectedModel}
56 prompt={editor.draft.prompt}
57 onPromptChange={(prompt) =>
58 setEditor((current) =>
59 current ? { ...current, draft: { ...current.draft, prompt } } : current,
60 )
61 }
62 onSelectModel={(option) =>
63 setEditor((current) =>
64 current
65 ? {
66 ...current,
67 draft: {
68 ...current.draft,
69 modelSelection: option.selection,
70 modelSelectionIsExplicit: true,
71 },
72 }
73 : current,
74 )
75 }
76 optionDescriptors={optionDescriptors}
77 onUpdateOptionSelections={(options) =>
78 setEditor((current) =>
79 current?.draft.modelSelection
80 ? {
81 ...current,
82 draft: {
83 ...current.draft,
84 modelSelection: { ...current.draft.modelSelection, options },
85 modelSelectionIsExplicit: true,
86 },
87 }
88 : current,
89 )
90 }
91 runtimeMode={editor.draft.runtimeMode}
92 onUpdateRuntimeMode={(runtimeMode) =>
93 setEditor((current) =>
94 current
95 ? {
96 ...current,
97 draft: { ...current.draft, runtimeMode },
98 }
99 : current,
100 )
101 }
102 onClose={() => navigation.goBack()}
103 />
104 );
106​
107export function ScheduledTaskBranchPickerRouteScreen() {
108 const navigation = useNavigation();
109 const { editor, setEditor } = useScheduledTaskEditor();
110 const projects = useProjects();
111 const project =
112 projects.find(
113 (entry) =>
114 entry.environmentId === editor?.environmentId && entry.id === editor.draft.projectId,
115 ) ?? null;
116 const [query, setQuery] = useState("");
117 const debouncedQuery = useDebouncedValue(query, 150);
118 const branches = usePaginatedBranches({
119 environmentId: editor?.environmentId ?? null,
120 cwd: project?.workspaceRoot ?? null,
121 query: debouncedQuery,
122 });
123 const visibleBranches = branches.refs.filter(
124 (branch) => !branch.isRemote && branch.name.toLowerCase().includes(query.trim().toLowerCase()),
125 );
126​
127 if (!editor) return <MissingTaskDraft />;
128​
129 return (
130 <BranchPickerScreen
131 title="Base branch"
132 project={project}
133 branches={visibleBranches}
134 selectedBranchName={editor.draft.baseRef}
135 query={query}
136 onQueryChange={setQuery}
137 loading={
138 query.trim() !== debouncedQuery.trim() || (branches.isPending && branches.data === null)
139 }
140 error={project ? branches.error : "This project is no longer available."}
141 refreshing={branches.isFetchingNextPage}
142 hasMore={branches.data?.nextCursor != null}
143 onRefresh={branches.refresh}
144 onLoadMore={branches.loadNext}
145 onSelect={(branch) => {
146 void Haptics.selectionAsync();
147 setEditor((current) =>
148 current ? { ...current, draft: { ...current.draft, baseRef: branch.name } } : current,
149 );
150 navigation.goBack();
151 }}
152 worktree={{
153 startFromOrigin: editor.draft.startFromOrigin,
154 onChangeStartFromOrigin: (startFromOrigin) =>
155 setEditor((current) =>
156 current
157 ? {
158 ...current,
159 draft: { ...current.draft, startFromOrigin },
160 }
161 : current,
162 ),
163 }}
164 />
165 );