| 10 | import { useQuery } from '@tanstack/react-query' |
| 11 | |
| 12 | function SettingsUI() { |
| 13 | const [civitaiApiKey, setCivitaiApiKey] = React.useState<string>() |
| 14 | |
| 15 | const getSettingsQuery = useQuery({ |
| 16 | queryKey: ['settings'], |
| 17 | queryFn: async () => { |
| 18 | const resp = await fetch('/api/get_config') |
| 19 | const data = (await resp.json()) as Config |
| 20 | return data |
| 21 | }, |
| 22 | enabled: !civitaiApiKey, |
| 23 | }) |
| 24 | |
| 25 | useEffect(() => { |
| 26 | if (getSettingsQuery.data) { |
| 27 | setCivitaiApiKey(getSettingsQuery.data.credentials.civitai.apikey) |
| 28 | } |
| 29 | }, [getSettingsQuery.data]) |
| 30 | |
| 31 | const { toast } = useToast() |
| 32 | |
| 33 | const setCivitaiCredentialsMutation = useMutation({ |
| 34 | mutationFn: async ({ |
| 35 | civitai_api_key, |
| 36 | }: { |
| 37 | civitai_api_key: string |
| 38 | }) => { |
| 39 | const response = await fetch(`/api/update_config`, { |
| 40 | method: 'POST', |
| 41 | headers: { |
| 42 | 'Content-Type': 'application/json', |
| 43 | }, |
| 44 | body: JSON.stringify({ |
| 45 | credentials: { |
| 46 | civitai: { |
| 47 | apikey: civitai_api_key, |
| 48 | }, |
| 49 | }, |
| 50 | }), |
| 51 | }) |
| 52 | const data = await response.json() |
| 53 | return data |
| 54 | }, |
| 55 | onSuccess: async () => { |
| 56 | toast({ |
| 57 | title: 'Saved your settings!', |
| 58 | }) |
| 59 | }, |
| 60 | }) |
| 61 | |
| 62 | if (getSettingsQuery.isLoading) { |
| 63 | return <div>Loading...</div> |
| 64 | } |
| 65 | |
| 66 | return ( |
| 67 | <> |
| 68 | <div className="flex flex-col p-10"> |
| 69 | <div className="flex flex-col space-y-2"> |