import { useEffect } from "react" import { withRefresh } from "@/api/with-refresh.ts" import { orgStore } from "@/auth/org.ts" import { makeOrgsApi } from "@/sdkClient.ts" import { zodResolver } from "@hookform/resolvers/zod" import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { useForm } from "react-hook-form" import { toast } from "sonner" import { z } from "zod" import { Button } from "@/components/ui/button.tsx" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card.tsx" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form.tsx" import { Input } from "@/components/ui/input.tsx" /* const isS3 = (c: DtoCredentialOut) => c.provider === "aws" && c.scope_kind === "service" && // scope may be JSON; allow both object and stringified JSON (() => { const s = (c as any).scope try { const obj = typeof s === "string" ? JSON.parse(s) : s || {} return obj?.service === "s3" } catch { return false } })() */ const schema = z.object({ name: z.string().min(1, "Required"), domain: z.string().optional(), }) type Values = z.infer export const OrgSettings = () => { const api = makeOrgsApi() const qc = useQueryClient() const orgId = orgStore.get() const q = useQuery({ enabled: !!orgId, queryKey: ["org", orgId], queryFn: () => withRefresh(() => api.getOrg({ id: orgId! })), }) /* const credentialQ = useQuery({ queryKey: ["credentials", "s3"], queryFn: () => credentialsApi.listCredentials(), // client-side filter }) const s3Credentials = useMemo(() => (credentialQ.data ?? []).filter(isS3), [credentialQ.data]) */ const form = useForm({ resolver: zodResolver(schema), defaultValues: { name: "", domain: "", }, }) useEffect(() => { if (q.data) { form.reset({ name: q.data.name ?? "", domain: q.data.domain ?? "", }) } }, [q.data, form]) const updateMut = useMutation({ mutationFn: (v: Partial) => api.updateOrg({ id: orgId!, body: v }), onSuccess: () => { void qc.invalidateQueries({ queryKey: ["org", orgId] }) toast.success("Organization updated") }, onError: (e: any) => toast.error(e?.message ?? "Update failed"), }) const deleteMut = useMutation({ mutationFn: () => api.deleteOrg({ id: orgId! }), onSuccess: () => { toast.success("Organization deleted") orgStore.set("") void qc.invalidateQueries({ queryKey: ["orgs:mine"] }) }, onError: (e: any) => toast.error(e?.message ?? "Delete failed"), }) if (!orgId) { return

Pick an organization.

} if (q.isLoading) return

Loading...

if (q.error) return

Failed to load.

const onSubmit = (v: Values) => { const delta: Partial = {} if (v.name !== q.data?.name) delta.name = v.name const normDomain = v.domain?.trim() || undefined if ((normDomain ?? null) !== (q.data?.domain ?? null)) delta.domain = normDomain if (Object.keys(delta).length === 0) return updateMut.mutate(delta) } return ( Organization Settings
( Name )} /> ( Domain (optional) )} />
) }