import { type JSX, useEffect, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
Fingerprint,
FolderOpen,
RotateCcw,
Wand2,
Trash2,
History,
ShieldCheck,
Cpu,
} from 'lucide-react';
import type { CloudAccount } from '@/types/cloudAccount';
import type { DeviceProfile, DeviceProfilesSnapshot, DeviceProfileVersion } from '@/types/account';
import {
bindCloudIdentityProfile,
bindCloudIdentityProfileWithPayload,
deleteCloudIdentityProfileRevision,
getCloudIdentityProfiles,
openCloudIdentityStorageFolder,
previewGenerateCloudIdentityProfile,
restoreCloudIdentityProfileRevision,
restoreCloudBaselineProfile,
} from '@/actions/cloud';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { useToast } from '@/components/ui/use-toast';
interface IdentityProfileDialogProps {
account: CloudAccount | null;
open: boolean;
onOpenChange: (open: boolean) => void;
}
function formatError(error: unknown): string {
if (error instanceof Error && error.message) {
return error.message;
}
return String(error);
}
function renderProfile(profile?: DeviceProfile): JSX.Element {
if (!profile) {
return (
N/A
);
}
const rows: Array<{ label: string; value: string }> = [
{ label: 'machineId', value: profile.machineId },
{ label: 'macMachineId', value: profile.macMachineId },
{ label: 'devDeviceId', value: profile.devDeviceId },
{ label: 'sqmId', value: profile.sqmId },
];
return (
{rows.map((row) => (
{row.label}
{row.value}
))}
);
}
function sortHistory(history: DeviceProfileVersion[]): DeviceProfileVersion[] {
return [...history].sort((a, b) => b.createdAt - a.createdAt);
}
export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityProfileDialogProps) {
const { t } = useTranslation();
const { toast } = useToast();
const [snapshot, setSnapshot] = useState(null);
const [initialLoading, setInitialLoading] = useState(false);
const [refreshing, setRefreshing] = useState(false);
const [actionKey, setActionKey] = useState(null);
const [previewProfile, setPreviewProfile] = useState(null);
const actionLockRef = useRef(false);
const history = useMemo(() => sortHistory(snapshot?.history || []), [snapshot?.history]);
const showLoadingPlaceholder = initialLoading && !snapshot;
const refreshProfiles = async (options?: { silent?: boolean }) => {
if (!account) {
setSnapshot(null);
return;
}
const silent = options?.silent === true;
if (silent) {
setRefreshing(true);
} else {
setInitialLoading(true);
}
try {
const result = await getCloudIdentityProfiles({ accountId: account.id });
setSnapshot(result);
} catch (error) {
toast({
title: t('cloud.toast.actionFailed', { defaultValue: 'Action failed' }),
description: formatError(error),
variant: 'destructive',
});
} finally {
if (silent) {
setRefreshing(false);
} else {
setInitialLoading(false);
}
}
};
useEffect(() => {
if (!open) {
return;
}
refreshProfiles();
}, [open, account?.id]);
const runAction = async (key: string, action: () => Promise) => {
if (actionLockRef.current) {
return;
}
actionLockRef.current = true;
setActionKey(key);
try {
await action();
await refreshProfiles({ silent: true });
} catch (error) {
toast({
title: t('cloud.toast.actionFailed', { defaultValue: 'Action failed' }),
description: formatError(error),
variant: 'destructive',
});
} finally {
actionLockRef.current = false;
setActionKey(null);
}
};
if (!account) {
return null;
}
return (
);
}