Files
HealthLog/src/components/settings/trusted-devices-card.tsx
T
Marc-André Bombeck b2e1aa0cde refactor(settings): one header, one action row, one integration card
The sweep that follows from the primitives. Nothing here is new behaviour;
every change removes a second way of saying something the surface already
said one way somewhere else.

Headers. The two collapsible security cards stop rebuilding
`SettingsCardHeader` by hand to carry a chevron — the chevron rides inside
the title button, and the description moves out of the collapsed region, so
the first paint is no longer a bare title. The import card shell, the export
and import section headings, and the four free-standing group headers stop
inventing their own type scale: a heading above a GROUP of cards is
`SectionHeading` (`text-base`), a card title is `SettingsCardHeader`
(`text-lg`), and the step between them is what says which is which. The
targets card gets a title instead of a naked icon.

Actions. Every card-level row is `SettingsCardActions`. The primary action
comes out of the header's status slot in six cards (password, web push,
pillar order, coach memory, and the two danger-zone triggers) and out of the
middle of the card in three more; the reset that hung in the dashboard-layout
header joins save as a secondary in the same row. Result and status lines
move ABOVE the row — sources' cross-link, the invite form's two status lines
— because nothing follows a card's actions. Revoking a share link is
`variant="destructive"` rather than an `outline` hand-tinted red in two
different ways. The import row had four variants for four buttons; it has
two. Export downloads are the card's primary, not an outline link.

Integrations. One description sentence with the setup-guide link inline: the
second paragraph was byte-identical across six of eight cards, which is the
tell that it said nothing about the provider it sat under. The `<hr>` under
the header — which existed only in this folder — is gone. So is the hint that
pointed at a guide 40 px above it, and the second format for "not
configured". Connect is one button everywhere: primary, `sm`, in the action
row, instead of primary-and-small in three cards and outline-and-large in
four.

Plus the standards leftovers the audit listed: two `pl-5` list gutters, one
`scroll-mt-20` anchor, a purple header icon, `text-[10px]` badges, four
hand-rolled callout shells that `SettingsInfoTile` already paints, and the
two inline buttons that never had the mobile tap floor at all.
2026-08-07 19:39:11 +02:00

177 lines
5.9 KiB
TypeScript

"use client";
/**
* v1.23 — Settings → "Trusted devices" ("remember this device").
*
* Lists the browsers the user opted to trust at the second-factor step. A
* trusted device skips factor 2 for 30 days (the password is still required);
* this card makes them visible and revocable. The list is hidden entirely when
* empty so a user who never opted in sees no clutter.
*/
import { useState } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { Loader2, ShieldCheck } from "lucide-react";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardActions } from "@/components/settings/_card-actions";
import { ConfirmButton } from "@/components/ui/confirm-button";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { useTranslations, useFormatters } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { apiGet, apiDelete } from "@/lib/api/api-fetch";
interface TrustedDeviceRow {
id: string;
label: string | null;
createdAt: string;
lastUsedAt: string;
expiresAt: string;
isCurrent: boolean;
}
export function TrustedDevicesCard({
isAuthenticated,
}: {
isAuthenticated: boolean;
}) {
const { t } = useTranslations();
const fmt = useFormatters();
const queryClient = useQueryClient();
const [status, setStatus] = useState<string | null>(null);
const { data, isLoading, isError } = useQuery({
queryKey: queryKeys.trustedDevices(),
queryFn: () =>
apiGet<{ devices: TrustedDeviceRow[] }>("/api/auth/me/trusted-devices"),
enabled: isAuthenticated,
});
function invalidate() {
void queryClient.invalidateQueries({
queryKey: queryKeys.trustedDevices(),
});
void queryClient.invalidateQueries({
queryKey: queryKeys.securityActivity(),
});
}
const revokeOne = useMutation({
mutationFn: (id: string) => apiDelete(`/api/auth/me/trusted-devices/${id}`),
onSuccess: () => {
setStatus(null);
invalidate();
},
onError: () => setStatus(t("settings.security.trustedDevices.revokeError")),
});
const revokeAll = useMutation({
mutationFn: () =>
apiDelete<{ revoked: number }>("/api/auth/me/trusted-devices"),
onSuccess: (res) => {
setStatus(
t("settings.security.trustedDevices.revokeAllDone", {
count: res?.revoked ?? 0,
}),
);
invalidate();
},
onError: () => setStatus(t("settings.security.trustedDevices.revokeError")),
});
const devices = data?.devices ?? [];
// Hide the whole card when the user has no trusted devices — nothing to show
// and no opt-in is offered here (that happens at the second-factor step).
if (!isLoading && !isError && devices.length === 0) {
return null;
}
return (
<SettingsCard data-slot="settings-trusted-devices-card">
<SettingsCardHeader
icon={ShieldCheck}
title={t("settings.security.trustedDevices.title")}
/>
<div className="space-y-4">
<p className="text-muted-foreground text-xs">
{t("settings.security.trustedDevices.description")}
</p>
{isLoading && (
<Loader2 className="text-muted-foreground h-5 w-5 animate-spin motion-reduce:animate-none" />
)}
{isError && (
<p role="alert" className="text-destructive text-sm">
{t("settings.security.trustedDevices.loadError")}
</p>
)}
{devices.length > 0 && (
<ul className="divide-border divide-y">
{devices.map((d) => (
<li
key={d.id}
className="flex items-center justify-between gap-3 py-3"
data-slot="trusted-device-row"
>
<div className="min-w-0 space-y-0.5">
<p className="truncate text-sm font-medium">
{d.label ?? t("settings.security.trustedDevices.unnamed")}
{d.isCurrent && (
<span className="text-success ml-2 text-xs font-normal">
{t("settings.security.trustedDevices.current")}
</span>
)}
</p>
<p className="text-muted-foreground text-xs">
{t("settings.security.trustedDevices.expires", {
date: fmt.date(d.expiresAt),
})}
</p>
</div>
<ConfirmButton
slot="revoke-trusted-device"
size="sm"
className="min-h-9 shrink-0"
label={t("settings.security.trustedDevices.revoke")}
title={t(
"settings.security.trustedDevices.revokeConfirmTitle",
)}
body={t("settings.security.trustedDevices.revokeConfirmBody")}
confirmLabel={t("settings.security.trustedDevices.revoke")}
onConfirm={() => revokeOne.mutate(d.id)}
pending={revokeOne.isPending}
/>
</li>
))}
</ul>
)}
{devices.length > 0 && (
<SettingsCardActions>
<ConfirmButton
slot="revoke-all-trusted-devices"
className="min-h-11 sm:min-h-9"
label={t("settings.security.trustedDevices.revokeAll")}
title={t(
"settings.security.trustedDevices.revokeAllConfirmTitle",
)}
body={t("settings.security.trustedDevices.revokeAllConfirmBody")}
confirmLabel={t("settings.security.trustedDevices.revokeAll")}
onConfirm={() => revokeAll.mutate()}
pending={revokeAll.isPending}
/>
</SettingsCardActions>
)}
{status && (
<p role="status" className="text-muted-foreground text-right text-sm">
{status}
</p>
)}
</div>
</SettingsCard>
);
}