Spaces:
Build error
Build error
File size: 3,028 Bytes
0bfe2e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import React from 'react';
import { Modal } from '@/components/ui/modal';
import { TextInput } from '@/components/ui/text-input';
import { Button } from '@/components/ui/button';
import { UserConfigAPI } from '@/services/api';
import { useUserData } from '@/context/userData';
import { toast } from 'sonner';
import { PasswordInput } from './ui/password-input';
interface ConfigModalProps {
open: boolean;
onSuccess: () => void;
onOpenChange: (v: boolean) => void;
initialUuid?: string;
}
export function ConfigModal({
open,
onSuccess,
onOpenChange,
initialUuid,
}: ConfigModalProps) {
const { setUserData, setUuid, setPassword, setEncryptedPassword } =
useUserData();
const [uuid, setUuidInput] = React.useState(initialUuid || '');
const [password, setPasswordInput] = React.useState('');
const [loading, setLoading] = React.useState(false);
console.log(`received initialUuid: ${initialUuid}`);
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setLoading(true);
try {
const result = await UserConfigAPI.loadConfig(uuid, password);
if (!result.success || !result.data) {
toast.error(result.error?.message || 'Failed to load configuration');
return;
}
setUserData((prev) => ({
...prev,
...result.data!.config, // we just checked that this is not null
}));
setUuid(uuid);
setPassword(password);
setEncryptedPassword(result.data.encryptedPassword);
onSuccess();
} catch (err) {
toast.error(
err instanceof Error ? err.message : 'Failed to load configuration'
);
} finally {
setLoading(false);
}
};
// Reset form when modal opens/closes
React.useEffect(() => {
if (!open) {
setPasswordInput('');
}
}, [open]);
// Handle initialUuid changes
React.useEffect(() => {
if (initialUuid) {
setUuidInput(initialUuid);
} else {
setUuidInput('');
}
}, [initialUuid]);
const handleCancel = () => {
onOpenChange(false);
};
return (
<Modal open={open} onOpenChange={onOpenChange} title="Load Configuration">
<form onSubmit={handleSubmit} className="space-y-4">
<TextInput
label="UUID"
id="uuid"
value={uuid}
onValueChange={(value) => setUuidInput(value)}
placeholder="Enter your configuration UUID"
required
disabled={!!initialUuid}
/>
<PasswordInput
label="Password"
id="password"
value={password}
onValueChange={(value) => setPasswordInput(value)}
placeholder="Enter your configuration password"
required
/>
<div className="flex justify-end gap-2">
<Button type="button" onClick={handleCancel}>
Cancel
</Button>
<Button type="submit" loading={loading}>
Load
</Button>
</div>
</form>
</Modal>
);
}
|