diff --git a/apps/mobile/src/components/ui/ListActionRow.tsx b/apps/mobile/src/components/ui/ListActionRow.tsx index 35da97d517c9e..507fe7cb4a8dd 100644 --- a/apps/mobile/src/components/ui/ListActionRow.tsx +++ b/apps/mobile/src/components/ui/ListActionRow.tsx @@ -9,6 +9,8 @@ export function ListActionRow( props: ListRowContentProps & { readonly tone?: "default" | "danger"; readonly loading?: boolean; + /** When the row's label alone doesn't say what tapping it does. */ + readonly accessibilityLabel?: string; readonly onPress: () => void; }, ) { @@ -17,7 +19,7 @@ export function ListActionRow( return ( ) { + const navigation = useNavigation(); + const environmentId = Option.getOrNull(decodeEnvironmentId(route.params.environmentId)); + const orgId = Option.getOrNull(decodeOrgId(route.params.orgId)); + const personId = Option.getOrNull(decodePersonId(route.params.personId)); + // Held here and nowhere else; leaving the page drops it. + const [made, setMade] = useState(null); + const [linking, setLinking] = useState(false); + const [problem, setProblem] = useState(null); + const target = + environmentId === null || orgId === null || personId === null + ? null + : { environmentId, orgId, personId }; + + const link = async () => { + if (target === null || linking) return; + setLinking(true); + setProblem(null); + const outcome = await runAtomCommand( + appAtomRegistry, + kinClient.deviceLink, + { environmentId: target.environmentId, input: { orgId: target.orgId } }, + { reportFailure: false, reportDefect: true }, + ); + setLinking(false); + if (AsyncResult.isSuccess(outcome)) setMade(outcome.value); + else setProblem(kinDeviceLinkProblem(squashAtomCommandFailure(outcome))); + }; + + return ( + + (made === null ? navigation.goBack() : setMade(null))} + {...(made === null ? {} : { confirm: { label: "Done", onPress: () => setMade(null) } })} + /> + + + {target === null ? ( + + ) : made !== null ? ( + void link()} + /> + ) : ( + <> + {problem === null ? null : } + + void link()} + /> + + + + )} + + + + ); +} + +/** + * A person's signed-in devices with a way to remove each: your own here, or a teammate's on + * their sheet for the owner. + */ +export function KinDeviceList(props: { + readonly target: KinDevicesTarget; + readonly self: boolean; +}) { + const { environmentId, orgId, personId } = props.target; + const atom = kinClient.devices({ environmentId, input: { orgId, personId } }); + const result = useAtomValue(atom); + const refresh = useAtomRefresh(atom); + const [removing, setRemoving] = useState(null); + const [problem, setProblem] = useState(null); + const held = Option.getOrNull(AsyncResult.value(result)); + const nowMs = useNowMinuteMs(); + const devices = held === null ? null : kinActiveDevices(held.devices, nowMs); + + const remove = (device: KinPersonDevice) => { + const copy = kinRemoveDeviceCopy(device, props.self, held?.devices ?? [], nowMs); + Dialog.confirm({ + title: copy.title, + message: copy.detail, + confirmText: KIN_DEVICES_COPY.remove, + destructive: true, + onConfirm: () => { + setRemoving(device.sessionId); + setProblem(null); + void runAtomCommand( + appAtomRegistry, + kinClient.deviceRemove, + { environmentId, input: { orgId, personId, sessionId: device.sessionId } }, + { reportFailure: false, reportDefect: true }, + ).then((outcome) => { + setRemoving(null); + if (AsyncResult.isFailure(outcome)) + setProblem(kinRemoveDeviceProblem(squashAtomCommandFailure(outcome))); + // This phone is signed out now; the app's access-ended screen takes over. + else if (device.current === true) return; + refresh(); + }); + }, + }); + }; + + return ( + <> + {problem === null ? null : } + + {devices === null ? ( + AsyncResult.isFailure(result) ? ( + + ) : ( + + + + ) + ) : devices.length === 0 ? ( + + + {KIN_DEVICES_COPY.empty} + + + ) : ( + devices.map((device, index) => ( + + remove(device)} + /> + + )) + )} + + + ); +} + +/** The link and QR for one more device; shown once, then gone. */ +function KinDeviceLinkView(props: { + readonly made: KinPeopleLinkDeviceResult; + readonly target: KinDevicesTarget; + readonly linking: boolean; + /** Why the last "Make a new link" failed. */ + readonly problem: string | null; + readonly onRenew: () => void; +}) { + const { made } = props; + const { environmentId, orgId, personId } = props.target; + const connection = Option.getOrNull( + useAtomValue(environmentSession.preparedConnectionValueAtom(environmentId)), + ); + const listed = Option.getOrNull( + AsyncResult.value( + useAtomValue(kinClient.devices({ environmentId, input: { orgId, personId } })), + ), + ); + const [copied, setCopied] = useState(false); + const nowMs = useNowMinuteMs(); + if (connection === null) { + return ; + } + const link = kinInviteLink(connection.httpBaseUrl, made.secret); + const left = kinDeviceLinkLeft(made.expiresAt, nowMs); + const joined = + listed === null + ? null + : kinDeviceJustLinked(kinActiveDevices(listed.devices, nowMs), made.linkId); + if (joined !== null) + return ( + + ); + if (left.expired) + return ( + <> + {props.problem === null ? null : } + + } + /> + + ); + return ( + <> + + + + + {KIN_DEVICES_COPY.scan} + + + + {kinInviteLinkLocalOnly(connection.httpBaseUrl) ? ( + + {KIN_INVITE_LOCAL_ONLY_NOTE} + + ) : null} + + + {problem === null || made !== null ? null : ( +

+ {problem} +

+ )} + {made === null ? null : ( + void makeLink()} + onClose={() => { + setMade(null); + void refreshDevices(props); + }} + /> + )} + + ); +} + +/** A teammate's devices on the owner's sheet, each with Remove. */ +export function KinTeammateDevices(props: KinDevicesTarget) { + return ( +
+

Devices

+ +
+ ); +} + +function KinDeviceList(props: KinDevicesTarget & { readonly mine: boolean }) { + const { environmentId, orgId, personId } = props; + const { devices, all, nowMs, failed, now } = useActiveDevices(props); + const remove = useAtomCommand(kinClient.deviceRemove, { reportFailure: false }); + const [asked, setAsked] = useState(null); + const [removing, setRemoving] = useState>(new Set()); + const [problem, setProblem] = useState(null); + const copy = asked === null ? null : kinRemoveDeviceCopy(asked, props.mine, all, nowMs); + + const removeDevice = async (device: KinPersonDevice) => { + setProblem(null); + setRemoving((held) => new Set(held).add(device.sessionId)); + const result = await remove({ + environmentId, + input: { orgId, personId, sessionId: device.sessionId }, + }); + // This browser's own session just ended: a full load shows the signed-out screen. + if (AsyncResult.isSuccess(result) && device.current === true) { + window.location.reload(); + return; + } + if (!AsyncResult.isSuccess(result)) + setProblem(kinRemoveDeviceProblem(squashAtomCommandFailure(result))); + await refreshDevices(props); + setRemoving((held) => { + const next = new Set(held); + next.delete(device.sessionId); + return next; + }); + }; + + if (devices === null) { + return ( +

+ {failed ? "Couldn't load devices." : "Loading…"} +

+ ); + } + const shown = devices.filter((device) => !removing.has(device.sessionId)); + return ( + <> + {shown.length === 0 ? ( +

+ {props.mine ? "No devices linked yet." : "Not signed in anywhere."} +

+ ) : ( +
    + {shown.map((device) => ( +
  • + + + {kinDeviceLabel(device)} + {device.current === true ? This device : null} + + {kinDeviceLine(device, now)} + + {kinDeviceRemovable(device) ? ( + + ) : null} +
  • + ))} +
+ )} + {problem === null ? null : ( +

+ {problem} +

+ )} + !open && setAsked(null)}> + + + {copy?.title} + {copy?.detail} + + + }>Cancel + + + + + + ); +} + +function CopyButton(props: { + readonly text: string; + readonly label: string; + readonly what: string; +}) { + const { copyToClipboard, isCopied } = useCopyToClipboard({ target: props.what }); + return ( + + ); +} + +/** The link the host just made, shown once; dropped when the dialog closes. */ +function KinLinkDeviceDialog(props: { + readonly made: KinPeopleLinkDeviceResult; + /** The label of a device that joined with it, once one has. */ + readonly joined: string | null; + readonly environmentId: EnvironmentId; + readonly busy: boolean; + /** Why the last "Make a new link" failed. */ + readonly problem: string | null; + readonly onRenew: () => void; + readonly onClose: () => void; +}) { + const { made } = props; + const connection = Option.getOrNull(usePreparedConnection(props.environmentId)); + const left = kinDeviceLinkLeft(made.expiresAt, useNowMinuteMs()); + const link = connection === null ? null : kinInviteLink(connection.httpBaseUrl, made.secret); + return ( + { + if (!open) props.onClose(); + }} + > + + + {KIN_DEVICES_COPY.link} + + The device signs in as you. Anyone with this link can do the same until it's used, so + only open it on your own device. + + + + {props.joined !== null ? ( + + {props.joined} is linked and signed in as you. + + ) : left.expired ? ( +
+ + {KIN_DEVICES_COPY.expiredDetail} + + {props.problem === null ? null : ( + + {props.problem} + + )} +
+ ) : link === null ? ( + + {KIN_DEVICES_COPY.dropped} + + ) : ( +
+
+
+ +
+
+

{KIN_DEVICES_COPY.scan}

+

+ {left.text} {KIN_DEVICES_COPY.replaces} +

+
+ +
+
+
+ {kinInviteLinkLocalOnly(link) ? ( + + {KIN_INVITE_LOCAL_ONLY_NOTE} + + ) : null} +
+ )} +
+ + {left.expired && props.joined === null ? ( + + ) : null} + + +
+
+ ); +} diff --git a/apps/web/src/kin/KinProfileSheet.tsx b/apps/web/src/kin/KinProfileSheet.tsx index 914e1ae9197ea..d1d4b877d3ecc 100644 --- a/apps/web/src/kin/KinProfileSheet.tsx +++ b/apps/web/src/kin/KinProfileSheet.tsx @@ -52,6 +52,7 @@ import { KinAwayField } from "./KinAway"; import { useOpenKinPeopleChat } from "./KinPeopleChat"; import { kinSettingsSectionIn } from "./kinSettingsAddress"; import { KIN_DIALOG_PARTS, KIN_SHEET_PARTS } from "./kinOverlayParts"; +import { KinMyDevices } from "./KinDevices"; import { kinClient } from "./kinClient"; import { useKinProfiles } from "./kinPictures"; import { useKinHostAt } from "./useKinHost"; @@ -312,15 +313,21 @@ export function KinProfileBody(props: KinProfileTarget) { onDone={() => setEditing(false)} /> ) : ( - setEditing(true)} - /> + <> + setEditing(true)} + /> + {/* The owner's own devices are T3 Code pairings, managed in its settings. */} + {mine && !holding.isOwner ? ( + + ) : null} + ); } diff --git a/apps/web/src/kin/KinTeamView.tsx b/apps/web/src/kin/KinTeamView.tsx index 5bfad954d07d0..25d1e46c0bdee 100644 --- a/apps/web/src/kin/KinTeamView.tsx +++ b/apps/web/src/kin/KinTeamView.tsx @@ -93,6 +93,7 @@ import { connectionAtomRuntime } from "../connection/runtime"; import { appAtomRegistry } from "../rpc/atomRegistry"; import { useAtomCommand } from "../state/use-atom-command"; import { KinAvatar } from "./KinAvatar"; +import { KinTeammateDevices } from "./KinDevices"; import { KinInviteDialog, type KinInviteDialogState } from "./KinInviteDialog"; import { KinAwayField, KinHandOverButton } from "./KinAway"; import { useKinProfiles } from "./kinPictures"; @@ -548,6 +549,13 @@ function KinTeammateDialog(props: { )} )} + {offered.remove && teammate.personId !== null && teammate.state === "joined" ? ( + + ) : null} {offered.remove && teammate.personId !== null ? (