MCPcopy Index your code
hub / github.com/coder/coder / CreateTaskForm

Function CreateTaskForm

site/src/modules/tasks/TaskPrompt/TaskPrompt.tsx:146–428  ·  view source on GitHub ↗
({ templates, onSuccess })

Source from the content-addressed store, hash-verified

144};
145
146const CreateTaskForm: FC<CreateTaskFormProps> = ({ templates, onSuccess }) => {
147 const { user, permissions } = useAuthenticated();
148 const queryClient = useQueryClient();
149 const [prompt, setPrompt] = useState("");
150
151 // Template
152 const [selectedTemplateId, setSelectedTemplateId] = useState<string>(
153 templates[0].id,
154 );
155 const selectedTemplate = templates.find(
156 (t) => t.id === selectedTemplateId,
157 ) as Template;
158
159 // Template versions
160 const [selectedVersionId, setSelectedVersionId] = useState(
161 selectedTemplate.active_version_id,
162 );
163 useEffect(() => {
164 setSelectedVersionId(selectedTemplate.active_version_id);
165 }, [selectedTemplate]);
166
167 // Presets
168 const { data: presets, isLoading: isLoadingPresets } = useQuery(
169 templateVersionPresets(selectedVersionId),
170 );
171 const [selectedPresetId, setSelectedPresetId] = useState<string>();
172 useEffect(() => {
173 const defaultPreset = presets?.find((p) => p.Default);
174 setSelectedPresetId(defaultPreset?.ID ?? presets?.[0]?.ID);
175 }, [presets]);
176 // External Auth
177 const {
178 externalAuth,
179 externalAuthError,
180 isPollingExternalAuth,
181 isLoadingExternalAuth,
182 } = useExternalAuth(selectedVersionId);
183 const missedExternalAuth = externalAuth?.filter(
184 (auth) => !auth.optional && !auth.authenticated,
185 );
186 const isMissingExternalAuth = missedExternalAuth
187 ? missedExternalAuth.length > 0
188 : true;
189
190 const createTaskMutation = useMutation({
191 mutationFn: async ({ prompt }: CreateTaskMutationFnProps) => {
192 // Users with updateTemplates permission can select the version to use.
193 if (permissions.updateTemplates) {
194 return API.createTask(user.id, {
195 input: prompt,
196 template_version_id: selectedVersionId,
197 template_version_preset_id: selectedPresetId,
198 });
199 }
200
201 // For regular users we want to enforce task creation to always use the latest
202 // active template version, to avoid issues when the active version changes
203 // between template load and user action.

Callers

nothing calls this directly

Calls 7

useAuthenticatedFunction · 0.90
templateVersionPresetsFunction · 0.90
useExternalAuthFunction · 0.90
getOSKeyFunction · 0.90
findMethod · 0.80
filterMethod · 0.80

Tested by

no test coverage detected