60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import { useState } from 'react'
|
|
import { Fingerprint, Loader2, Trash2 } from 'lucide-react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { useIntlLocale } from '../../i18n'
|
|
import { authClient } from '../../lib/auth-client'
|
|
import { Button } from '../../components/ui'
|
|
|
|
export function PasskeyList({ onRefresh }: { onRefresh: () => void }) {
|
|
const { data, isPending } = authClient.useListPasskeys()
|
|
const [deletingId, setDeletingId] = useState<string | null>(null)
|
|
const { t } = useTranslation('account')
|
|
const locale = useIntlLocale()
|
|
|
|
const handleDelete = async (id: string) => {
|
|
setDeletingId(id)
|
|
const { error } = await authClient.$fetch('/passkey/delete-passkey', {
|
|
method: 'POST',
|
|
body: { id },
|
|
})
|
|
setDeletingId(null)
|
|
if (!error) onRefresh()
|
|
}
|
|
|
|
if (isPending) {
|
|
return <Loader2 className="size-5 animate-spin text-foreground/40" />
|
|
}
|
|
|
|
const passkeys = data ?? []
|
|
if (passkeys.length === 0) {
|
|
return <p className="text-sm text-foreground/60">{t('security.noPasskeys')}</p>
|
|
}
|
|
|
|
return (
|
|
<ul className="divide-y divide-border rounded-xl border border-border bg-surface">
|
|
{passkeys.map((passkey) => (
|
|
<li key={passkey.id} className="flex items-center gap-3 px-4 py-3">
|
|
<span className="rounded-lg bg-accent-soft p-2">
|
|
<Fingerprint className="size-4 text-accent" />
|
|
</span>
|
|
<div className="min-w-0 flex-1">
|
|
<p className="truncate text-sm font-medium text-primary">{passkey.name ?? 'Passkey'}</p>
|
|
<p className="text-xs text-foreground/50">
|
|
{passkey.deviceType === 'singleDevice' ? t('security.device') : t('security.securityKey')} ·{' '}
|
|
{new Date(passkey.createdAt).toLocaleDateString(locale)}
|
|
</p>
|
|
</div>
|
|
<Button
|
|
size="icon"
|
|
variant="ghost"
|
|
aria-label={t('security.deletePasskey')}
|
|
disabled={deletingId === passkey.id}
|
|
onClick={() => void handleDelete(passkey.id)}
|
|
>
|
|
<Trash2 className="size-4 text-danger" />
|
|
</Button>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)
|
|
} |