import { useEffect, useState } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { useRouter } from "@tanstack/react-router"; import { useTranslation } from "react-i18next"; import toast from "react-hot-toast"; import { useConnection, useDeviceConnectionActions, } from "@/hooks/useConnection"; import { usePageHeadingFocus } from "@/hooks/usePageHeadingFocus"; import { useSelectDevice } from "@/hooks/useSelectDevice"; import { deviceRegistry, parsedEndpointForRecord, useDeviceRegistry, type DeviceRecord, } from "@/lib/devices/deviceRegistry"; import { credentialKeyForRecord, credentialStore, } from "@/lib/crypto/credentials"; import { invalidateLibraryImageCache } from "@/lib/libraryImageCache"; import { logger } from "@/lib/logger"; import { DeviceRow } from "@/components/DeviceRow"; import { PageFrame } from "@/components/PageFrame"; import { HeaderButton } from "@/components/wui/HeaderButton"; import { Card } from "@/components/wui/Card"; import { Button } from "@/components/wui/Button"; import { ModalActionBar } from "@/components/wui/ModalActionBar"; import { TextInput } from "@/components/wui/TextInput"; import { SlideModal } from "@/components/SlideModal"; import { formatAppDateTime } from "@/lib/formatDate"; import { BackIcon } from "@/lib/images"; import { Route } from "@/routes/settings.devices_.$recordId"; import { appBackNavigationOptions } from "@/lib/tabSessionStore"; const selectRecords = (state: { records: Record }) => state.records; const selectActiveRecordId = (state: { activeRecordId: string | null }) => state.activeRecordId; /** * True once the stored devices have been read, either successfully or not. * A failed read never becomes hydrated, so waiting on `hydrated` alone would * leave this page blank for good. */ const selectRegistrySettled = (state: { hydrated: boolean; hydrationError: string | null; }) => state.hydrated || state.hydrationError !== null; export function DeviceDetail() { const { t } = useTranslation(); const router = useRouter(); const queryClient = useQueryClient(); const params = Route.useParams(); const records = useDeviceRegistry(selectRecords); const activeRecordId = useDeviceRegistry(selectActiveRecordId); const registrySettled = useDeviceRegistry(selectRegistrySettled); const goBack = () => void router.navigate(appBackNavigationOptions("/settings/devices")); const record = records[params.recordId]; const endpoint = parsedEndpointForRecord(record); const address = endpoint?.address ?? (record?.ble ? t("settings.bluetoothScan.viaBluetooth") : null); const { isConnected, activeConnection } = useConnection(); const { forgetDevice } = useDeviceConnectionActions(); const { selectRecord } = useSelectDevice(); const headingTitle = record?.name || address || ""; const headingRef = usePageHeadingFocus(headingTitle); const initialName = record?.name ?? ""; const [draftName, setDraftName] = useState(initialName); const [trackedRecordId, setTrackedRecordId] = useState(record?.recordId); const [confirmOpen, setConfirmOpen] = useState(false); const [forgetting, setForgetting] = useState(false); const [combineOpen, setCombineOpen] = useState(false); const [combineWithId, setCombineWithId] = useState(null); const [combineFailed, setCombineFailed] = useState(false); const [isCombining, setIsCombining] = useState(false); // Reset the draft when navigating between different device records. if (record?.recordId !== trackedRecordId) { setTrackedRecordId(record?.recordId); setDraftName(initialName); } // Records arrive asynchronously, so a page opened cold — a deep link, or a // reload sitting on this route — has no record yet through no fault of the // id in the URL. Redirecting before the read has settled would bounce the // user off a device that was about to appear. useEffect(() => { if ( !forgetting && !isCombining && registrySettled && (!record || !address) ) { router.navigate({ to: "/settings/devices", replace: true }); } }, [address, forgetting, isCombining, record, registrySettled, router]); if (!record || !address) return null; const isCurrentDevice = activeRecordId === record.recordId; const isActive = isCurrentDevice && isConnected; const handleSaveName = (next: string) => { void deviceRegistry.setCustomName(record.recordId, next); }; const handleUseThisDevice = () => { void selectRecord(record.recordId); router.navigate({ to: "/settings" }); }; const handleConfirmForget = async () => { if (forgetting) return; setForgetting(true); try { await forgetDevice(record.recordId); invalidateLibraryImageCache(record.recordId); queryClient.removeQueries({ predicate: (query) => query.queryKey.includes(record.recordId), }); setConfirmOpen(false); router.navigate({ to: "/settings/devices", replace: true }); } catch { // Registry writes roll back automatically. Credential cleanup failures // leave the record unchanged, so the user can retry from the same // confirmation instead of receiving a false success. setForgetting(false); toast.error(t("settings.deviceDetail.forgetFailed")); } }; const nameOf = (other: DeviceRecord) => other.name || parsedEndpointForRecord(other)?.address || t("settings.bluetoothScan.unnamed"); const otherRecords = Object.values(records) .filter((other) => other.recordId !== record.recordId) .sort( (left, right) => (right.lastConnectedAt ?? 0) - (left.lastConnectedAt ?? 0), ); const combineWith = combineWithId ? records[combineWithId] : undefined; const closeCombine = () => { if (isCombining) return; setCombineOpen(false); setCombineWithId(null); setCombineFailed(false); }; const handleConfirmCombine = async () => { if (!combineWith || isCombining) return; setIsCombining(true); setCombineFailed(false); // Once this page's own record is merged away the page is leaving, and // the redirect for a missing record must not run over that navigation. let leaving = false; try { // The record that survives is the one in use, else one that holds a // pairing, else the one this page is showing. let keepOther = combineWith.recordId === activeRecordId; if (!keepOther && !isCurrentDevice) { const paired = new Set( (await credentialStore.list()).map((entry) => entry.deviceKey), ); const hasCredentials = (candidate: DeviceRecord) => paired.has(credentialKeyForRecord(candidate.recordId)) || (candidate.legacyCredentialKey !== undefined && paired.has(candidate.legacyCredentialKey)); keepOther = !hasCredentials(record) && hasCredentials(combineWith); } const target = keepOther ? combineWith : record; const source = keepOther ? record : combineWith; await deviceRegistry.mergeRecords(target.recordId, source.recordId); for (const recordId of [target.recordId, source.recordId]) { invalidateLibraryImageCache(recordId); queryClient.removeQueries({ predicate: (query) => query.queryKey.includes(recordId), }); } setCombineOpen(false); setCombineWithId(null); if (keepOther) { leaving = true; router.navigate({ to: "/settings/devices/$recordId", params: { recordId: target.recordId }, replace: true, }); } } catch (error) { logger.error("Failed to combine device records", error, { category: "storage", action: "combineDeviceRecords", severity: "error", recordId: record.recordId, otherRecordId: combineWith.recordId, }); setCombineFailed(true); } finally { if (!leaving) setIsCombining(false); } }; const lastConnectedLine = record.lastConnectedAt !== undefined ? t("settings.deviceDetail.lastConnected", { when: formatAppDateTime(record.lastConnectedAt) ?? "", }) : t("settings.deviceDetail.neverConnected"); return ( } aria-label={t("nav.back")} /> } headerCenter={

{isActive && ( )} {headingTitle}

} >

{t("settings.deviceDetail.infoHeading")}

{t("settings.deviceDetail.address", { value: address })}

{isActive && activeConnection?.link && (

{t("settings.deviceDetail.link", { value: t( activeConnection.link === "bluetooth" ? "connection.linkBluetooth" : "connection.linkWifi", ), })}

)} {record.platform && (

{t("settings.deviceDetail.platform", { value: record.platform })}

)} {record.version && (

{t("settings.deviceDetail.version", { value: record.version })}

)}

{lastConnectedLine}

{!isActive && (