The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg, useGT, useMessages } from "gt-react"; |
| 2 | import { useState } from "react"; |
| 3 | import { |
| 4 | duoFoldState, |
| 5 | duoHoldOrientation, |
| 6 | type DuoCommand, |
| 7 | type DuoControlState, |
| 8 | } from "@t3tools/client-runtime/device/duo-control"; |
| 9 | import type { DeviceScreenSize } from "@t3tools/client-runtime/device/stream"; |
| 10 | import { DeviceDuoGlyph } from "./DeviceDuoGlyph"; |
| 11 | import { Button } from "~/components/ui/button"; |
| 12 | import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; |
| 13 | |
| 14 | const FOLDS = [ |
| 15 | { id: "closed", angle: 0 }, |
| 16 | { id: "half", angle: 90 }, |
| 17 | { id: "open", angle: 180 }, |
| 18 | ] as const; |
| 19 | const STANDS = [ |
| 20 | { id: "laptop", label: msg("Laptop stand") }, |
| 21 | { id: "tent", label: msg("Tent stand") }, |
| 22 | ] as const; |
| 23 | |
| 24 | export function DeviceDuoControls(props: { |
| 25 | screen: DeviceScreenSize; |
| 26 | state: DuoControlState; |
| 27 | enabled: boolean; |
| 28 | onCommand: (command: DuoCommand) => void; |
| 29 | }) { |
| 30 | const { screen } = props; |
| 31 | const gt = useGT(); |
| 32 | const m = useMessages(); |
| 33 | const { fold, stand, phoneVertical: reportedVertical, settled } = duoFoldState(screen); |
| 34 | const [settledVertical, setSettledVertical] = useState(reportedVertical); |
| 35 | if (settled && settledVertical !== reportedVertical) setSettledVertical(reportedVertical); |
| 36 | const [standVertical, setStandVertical] = useState(settledVertical); |
| 37 | const phoneVertical = stand ? standVertical : settledVertical; |
| 38 | // Only one command can wait; defer fold controls until stand rotation and queued commands settle in the |
| 39 | // correct display frame. |
| 40 | const foldWaits = props.state.pending && (stand || props.state.requested?.control === "pose"); |
| 41 | const foldLabels = { |
| 42 | closed: gt("Closed", { $context: "iPhone Duo fold shape" }), |
| 43 | half: phoneVertical |
| 44 | ? gt("Book", { $context: "iPhone Duo fold shape" }) |
| 45 | : gt("Laptop", { $context: "iPhone Duo fold shape" }), |
| 46 | open: gt("Open", { $context: "iPhone Duo fold shape" }), |
| 47 | }; |
| 48 | const button = ( |
| 49 | key: string, |
| 50 | label: string, |
| 51 | pressed: boolean, |
| 52 | onClick: () => void, |
| 53 | glyph: React.ReactNode, |
| 54 | waits = false, |
| 55 | ) => ( |
| 56 | <Tooltip key={key}> |
| 57 | <TooltipTrigger |
| 58 | render={ |
| 59 | <Button |
| 60 | size="icon" |
| 61 | variant={pressed ? "secondary" : "ghost"} |
| 62 | disabled={!props.enabled || waits} |
| 63 | aria-label={label} |
| 64 | aria-pressed={pressed} |
| 65 | data-pressed={pressed ? "" : undefined} |
| 66 | onClick={onClick} |
| 67 | /> |
| 68 | } |
| 69 | > |
| 70 | {glyph} |
| 71 | </TooltipTrigger> |
| 72 | <TooltipPopup side="left">{label}</TooltipPopup> |
| 73 | </Tooltip> |
| 74 | ); |
| 75 | const group = |
| 76 | "pointer-events-auto flex shrink-0 flex-col items-center gap-1 rounded-full border border-border/50 bg-background/80 p-1 shadow-sm"; |
| 77 | return ( |
| 78 | <div aria-label={gt("iPhone Duo stands")} className="flex flex-col items-center gap-2"> |
| 79 | <div role="group" aria-label={gt("Fold shape")} className={group}> |
| 80 | {FOLDS.map(({ id, angle: value }) => |
| 81 | button( |
| 82 | id, |
| 83 | foldLabels[id], |
| 84 | !stand && fold === id, |
| 85 | () => { |
| 86 | if (stand) |
| 87 | props.onCommand({ |
| 88 | control: "orientation", |
| 89 | value: duoHoldOrientation(standVertical, screen.screenId), |
| 90 | }); |
| 91 | props.onCommand({ control: "angle", value }); |
| 92 | }, |
| 93 | <DeviceDuoGlyph pose={id === "half" ? "book" : id} rotated={!phoneVertical} />, |
| 94 | foldWaits, |
| 95 | ), |
| 96 | )} |
| 97 | </div> |
| 98 | <div role="group" aria-label={gt("Device stance")} className={group}> |
| 99 | {STANDS.map(({ id, label }) => |
| 100 | button( |
| 101 | id, |
| 102 | m(label), |
| 103 | screen.hingePose === id, |
| 104 | () => { |
| 105 | if (!stand) setStandVertical(settledVertical); |
| 106 | props.onCommand({ control: "pose", value: id }); |
| 107 | }, |
| 108 | <DeviceDuoGlyph pose={id} />, |
| 109 | ), |
| 110 | )} |
| 111 | </div> |
| 112 | {props.state.error ? ( |
| 113 | <Tooltip> |
| 114 | <TooltipTrigger |
| 115 | render={ |
| 116 | <span |
| 117 | tabIndex={0} |
| 118 | role="alert" |
| 119 | aria-label={props.state.error} |
| 120 | className="pointer-events-auto text-xs text-destructive" |
| 121 | > |
| 122 | ! |
| 123 | </span> |
| 124 | } |
| 125 | /> |
| 126 | <TooltipPopup side="left">{props.state.error}</TooltipPopup> |
| 127 | </Tooltip> |
| 128 | ) : null} |
| 129 | </div> |
| 130 | ); |
| 131 | } |