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

MobileClientsUserProfilePage.tsx

166 lines5.8 KB

1import type { RelayClientDeviceRecord } from "@t3tools/contracts/relay";
2import { RefreshCwIcon, SmartphoneIcon } from "lucide-react";
3​
4import { useManagedRelayDevices } from "../../cloud/managedRelayState";
5import { cn } from "../../lib/utils";
6import { Badge } from "../ui/badge";
7import { Button } from "../ui/button";
8import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty";
9import { Skeleton } from "../ui/skeleton";
10import {
11 mobileClientNotificationDetail,
12 mobileClientPlatformLabel,
13 mobileClientUpdatedAtLabel,
14} from "./MobileClientsUserProfilePage.logic";
15​
16const MOBILE_CLIENT_SKELETON_ROWS = ["primary", "secondary"] as const;
17​
18function MobileClientStatusBadge({
19 enabled,
20 label,
21}: {
22 readonly enabled: boolean;
23 readonly label: string;
24}) {
25 return (
26 <Badge variant={enabled ? "success" : "outline"}>
27 {label}: {enabled ? "On" : "Off"}
28 </Badge>
29 );
31​
32function MobileClientRow({ device }: { readonly device: RelayClientDeviceRecord }) {
33 return (
34 <li className="rounded-xl border bg-card p-4 text-card-foreground shadow-sm/4">
35 <div className="flex items-start gap-3">
36 <div className="flex size-9 shrink-0 items-center justify-center rounded-lg border bg-muted/40 text-muted-foreground">
37 <SmartphoneIcon className="size-4" />
38 </div>
39 <div className="min-w-0 flex-1">
40 <div className="flex flex-col gap-1 sm:flex-row sm:items-start sm:justify-between sm:gap-3">
41 <div className="min-w-0">
42 <h3 className="truncate text-sm font-semibold text-foreground">{device.label}</h3>
43 <p className="text-xs text-muted-foreground">{mobileClientPlatformLabel(device)}</p>
44 </div>
45 <p className="shrink-0 text-[11px] text-muted-foreground/75">
46 {mobileClientUpdatedAtLabel(device.updatedAt)}
47 </p>
48 </div>
49 <div className="mt-3 flex flex-wrap gap-1.5">
50 <MobileClientStatusBadge
51 enabled={device.notifications.enabled}
52 label="Push notifications"
53 />
54 <MobileClientStatusBadge
55 enabled={device.liveActivities.enabled}
56 label="Live Activities"
57 />
58 </div>
59 <p className="mt-2 text-xs leading-relaxed text-muted-foreground/80">
60 {mobileClientNotificationDetail(device)}
61 </p>
62 </div>
63 </div>
64 </li>
65 );
67​
68function MobileClientsSkeleton() {
69 return (
70 <div aria-label="Loading mobile clients" className="space-y-3" role="status">
71 {MOBILE_CLIENT_SKELETON_ROWS.map((row) => (
72 <div key={row} className="rounded-xl border p-4">
73 <div className="flex gap-3">
74 <Skeleton className="size-9 shrink-0 rounded-lg" />
75 <div className="flex-1 space-y-2">
76 <Skeleton className="h-4 w-36" />
77 <Skeleton className="h-3 w-28" />
78 <div className="flex gap-2 pt-1">
79 <Skeleton className="h-5 w-28" />
80 <Skeleton className="h-5 w-24" />
81 </div>
82 </div>
83 </div>
84 </div>
85 ))}
86 </div>
87 );
89​
90function EmptyMobileClients() {
91 return (
92 <Empty className="min-h-72 rounded-xl border border-dashed bg-muted/15">
93 <EmptyMedia variant="icon">
94 <SmartphoneIcon />
95 </EmptyMedia>
96 <EmptyHeader>
97 <EmptyTitle>No mobile clients</EmptyTitle>
98 <EmptyDescription>
99 Sign in to T3 Code on your iPhone to register it for push notifications and Live
100 Activities.
101 </EmptyDescription>
102 </EmptyHeader>
103 </Empty>
104 );
106​
107export function MobileClientsUserProfilePage() {
108 const devicesState = useManagedRelayDevices();
109 const devices = devicesState.data ?? [];
110 const isInitialLoad =
111 !devicesState.accountId || (devicesState.data === null && !devicesState.error);
112 const hasErrorWithoutData = devicesState.error !== null && devicesState.data === null;
113​
114 return (
115 <div className="flex min-h-[30rem] w-full flex-col bg-background text-foreground">
116 <header className="flex flex-col gap-4 border-b px-6 py-5 sm:flex-row sm:items-center sm:justify-between">
117 <div>
118 <h2 className="text-base font-semibold tracking-[-0.01em]">Mobile clients</h2>
119 <p className="mt-1 text-sm text-muted-foreground">
120 Devices registered to receive T3 Connect activity from your environments.
121 </p>
122 </div>
123 <Button
124 size="sm"
125 variant="outline"
126 disabled={devicesState.isPending}
127 onClick={devicesState.refresh}
128 >
129 <RefreshCwIcon className={cn("size-3.5", devicesState.isPending && "animate-spin")} />
130 Refresh
131 </Button>
132 </header>
133​
134 <div className="flex-1 p-6">
135 {devicesState.error ? (
136 <div
137 className="mb-4 flex flex-col gap-3 rounded-lg border border-destructive/25 bg-destructive/5 p-3 text-sm sm:flex-row sm:items-center sm:justify-between"
138 role="alert"
139 >
140 <div>
141 <p className="font-medium text-destructive-foreground">
142 Could not load mobile clients
143 </p>
144 <p className="mt-0.5 text-xs text-muted-foreground">{devicesState.error}</p>
145 </div>
146 <Button size="xs" variant="outline" onClick={devicesState.refresh}>
147 Try again
148 </Button>
149 </div>
150 ) : null}
151​
152 {isInitialLoad ? (
153 <MobileClientsSkeleton />
154 ) : hasErrorWithoutData ? null : devices.length > 0 ? (
155 <ul className="space-y-3">
156 {devices.map((device) => (
157 <MobileClientRow key={device.deviceId} device={device} />
158 ))}
159 </ul>
160 ) : (
161 <EmptyMobileClients />
162 )}
163 </div>
164 </div>
165 );