Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DeviceDuoControls.tsx

127 lines4.1 KB

1import { useState } from "react";
2import {
3 duoFoldState,
4 duoHoldOrientation,
5 type DuoCommand,
6 type DuoControlState,
7} from "@t3tools/client-runtime/device/duo-control";
8import type { DeviceScreenSize } from "@t3tools/client-runtime/device/stream";
9import { DeviceDuoGlyph } from "./DeviceDuoGlyph";
10import { Button } from "~/components/ui/button";
11import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
12​
13const FOLDS = [
14 { id: "closed", angle: 0 },
15 { id: "half", angle: 90 },
16 { id: "open", angle: 180 },
17] as const;
18const STANDS = [
19 { id: "laptop", label: "Laptop stand" },
20 { id: "tent", label: "Tent stand" },
21] as const;
22​
23export function DeviceDuoControls(props: {
24 screen: DeviceScreenSize;
25 state: DuoControlState;
26 enabled: boolean;
27 onCommand: (command: DuoCommand) => void;
28}) {
29 const { screen } = props;
30 const { fold, stand, phoneVertical: reportedVertical, settled } = duoFoldState(screen);
31 const [settledVertical, setSettledVertical] = useState(reportedVertical);
32 if (settled && settledVertical !== reportedVertical) setSettledVertical(reportedVertical);
33 const [standVertical, setStandVertical] = useState(settledVertical);
34 const phoneVertical = stand ? standVertical : settledVertical;
35 // Folding out of a stand sends the rotation back, then the fold, and only one command can
36 // wait. Hold the fold buttons until a stand, or anything queued on one, has landed; the
37 // rotation must also be read in the frame of the display the stand settles on.
38 const foldWaits = props.state.pending && (stand || props.state.requested?.control === "pose");
39 const foldLabels = {
40 closed: "Closed",
41 half: phoneVertical ? "Book" : "Laptop",
42 open: "Open",
43 };
44 const button = (
45 key: string,
46 label: string,
47 pressed: boolean,
48 onClick: () => void,
49 glyph: React.ReactNode,
50 waits = false,
51 ) => (
52 <Tooltip key={key}>
53 <TooltipTrigger
54 render={
55 <Button
56 size="icon"
57 variant={pressed ? "secondary" : "ghost"}
58 disabled={!props.enabled || waits}
59 aria-label={label}
60 aria-pressed={pressed}
61 data-pressed={pressed ? "" : undefined}
62 onClick={onClick}
63 />
64 }
65 >
66 {glyph}
67 </TooltipTrigger>
68 <TooltipPopup side="left">{label}</TooltipPopup>
69 </Tooltip>
70 );
71 const group =
72 "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";
73 return (
74 <div aria-label="iPhone Duo stands" className="flex flex-col items-center gap-2">
75 <div role="group" aria-label="Fold shape" className={group}>
76 {FOLDS.map(({ id, angle: value }) =>
77 button(
78 id,
79 foldLabels[id],
80 !stand && fold === id,
81 () => {
82 if (stand)
83 props.onCommand({
84 control: "orientation",
85 value: duoHoldOrientation(standVertical, screen.screenId),
86 });
87 props.onCommand({ control: "angle", value });
88 },
89 <DeviceDuoGlyph pose={id === "half" ? "book" : id} rotated={!phoneVertical} />,
90 foldWaits,
91 ),
92 )}
93 </div>
94 <div role="group" aria-label="Device stance" className={group}>
95 {STANDS.map(({ id, label }) =>
96 button(
97 id,
98 label,
99 screen.hingePose === id,
100 () => {
101 if (!stand) setStandVertical(settledVertical);
102 props.onCommand({ control: "pose", value: id });
103 },
104 <DeviceDuoGlyph pose={id} />,
105 ),
106 )}
107 </div>
108 {props.state.error ? (
109 <Tooltip>
110 <TooltipTrigger
111 render={
112 <span
113 tabIndex={0}
114 role="alert"
115 aria-label={props.state.error}
116 className="pointer-events-auto text-xs text-destructive"
117 >
118 !
119 </span>
120 }
121 />
122 <TooltipPopup side="left">{props.state.error}</TooltipPopup>
123 </Tooltip>
124 ) : null}
125 </div>
126 );