create-expo-updates-server 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/dist/template/.dockerignore +16 -0
  2. package/dist/template/README.md +51 -660
  3. package/dist/template/app/api/admin/device-targets/route.test.ts +43 -0
  4. package/dist/template/app/api/admin/device-targets/route.ts +91 -0
  5. package/dist/template/app/api/admin/devices/route.test.ts +41 -0
  6. package/dist/template/app/api/admin/devices/route.ts +15 -0
  7. package/dist/template/app/api/admin/updates/[encodedKey]/audience/route.ts +75 -0
  8. package/dist/template/app/api/device-heartbeat/route.ts +2 -0
  9. package/dist/template/app/globals.css +114 -15
  10. package/dist/template/app/layout.tsx +12 -4
  11. package/dist/template/app/login/page.tsx +22 -18
  12. package/dist/template/app/updates/[encodedKey]/page.tsx +112 -88
  13. package/dist/template/app/updates/devices/page.tsx +264 -0
  14. package/dist/template/app/updates/embedded/page.tsx +102 -68
  15. package/dist/template/app/updates/emergency-redirects/page.tsx +144 -111
  16. package/dist/template/app/updates/layout.tsx +6 -1
  17. package/dist/template/app/updates/page.tsx +262 -212
  18. package/dist/template/components/admin/admin-shell.tsx +196 -0
  19. package/dist/template/components/admin/language-switcher.tsx +25 -0
  20. package/dist/template/components/admin/theme-provider.tsx +15 -0
  21. package/dist/template/components/ui/alert.tsx +65 -0
  22. package/dist/template/components/ui/badge.tsx +47 -0
  23. package/dist/template/components/ui/button.tsx +63 -0
  24. package/dist/template/components/ui/card.tsx +91 -0
  25. package/dist/template/components/ui/chart.tsx +373 -0
  26. package/dist/template/components/ui/checkbox.tsx +31 -0
  27. package/dist/template/components/ui/dialog.tsx +158 -0
  28. package/dist/template/components/ui/dropdown-menu.tsx +256 -0
  29. package/dist/template/components/ui/input.tsx +20 -0
  30. package/dist/template/components/ui/label.tsx +23 -0
  31. package/dist/template/components/ui/native-select.tsx +61 -0
  32. package/dist/template/components/ui/popover.tsx +88 -0
  33. package/dist/template/components/ui/scroll-area.tsx +57 -0
  34. package/dist/template/components/ui/select.tsx +189 -0
  35. package/dist/template/components/ui/separator.tsx +27 -0
  36. package/dist/template/components/ui/sheet.tsx +142 -0
  37. package/dist/template/components/ui/sidebar.tsx +736 -0
  38. package/dist/template/components/ui/skeleton.tsx +13 -0
  39. package/dist/template/components/ui/switch.tsx +34 -0
  40. package/dist/template/components/ui/table.tsx +115 -0
  41. package/dist/template/components/ui/tabs.tsx +90 -0
  42. package/dist/template/components/ui/textarea.tsx +17 -0
  43. package/dist/template/components/ui/tooltip.tsx +56 -0
  44. package/dist/template/components.json +22 -0
  45. package/dist/template/docs/admin-ui.md +35 -0
  46. package/dist/template/docs/device-targeting.md +129 -0
  47. package/dist/template/docs/doctor.md +137 -0
  48. package/dist/template/docs/expo-app-setup.md +174 -47
  49. package/dist/template/docs/getting-started.md +105 -146
  50. package/dist/template/docs/getting-started.ru.md +106 -191
  51. package/dist/template/docs/index.md +14 -33
  52. package/dist/template/docs/installation-telemetry.md +233 -0
  53. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +89 -0
  54. package/dist/template/docs/migrations/2026-10-19-device-heartbeat.sql +131 -0
  55. package/dist/template/docs/migrations/2026-10-22-device-analytics.sql +264 -0
  56. package/dist/template/docs/migrations/2026-10-22-device-targeting-activation.sql +108 -0
  57. package/dist/template/docs/migrations/2026-10-22-device-targeting.sql +130 -0
  58. package/dist/template/docs/migrations/2026-10-22-device-version-metadata.sql +154 -0
  59. package/dist/template/docs/migrations/schema.sql +868 -6
  60. package/dist/template/docs/npm-installer.md +15 -30
  61. package/dist/template/docs/operations-guide.md +87 -0
  62. package/dist/template/docs/ota-client-lifecycle.md +263 -0
  63. package/dist/template/docs/ota-update-policy.md +36 -2
  64. package/dist/template/docs/supabase-setup.md +4 -0
  65. package/dist/template/hooks/use-mobile.ts +21 -0
  66. package/dist/template/lib/utils.ts +6 -0
  67. package/dist/template/package-lock.json +2325 -101
  68. package/dist/template/package.json +13 -3
  69. package/dist/template/scripts/configure-expo-app.mjs +11 -15
  70. package/dist/template/scripts/doctor.mjs +51 -0
  71. package/dist/template/scripts/ensure-test-bucket.mjs +21 -0
  72. package/dist/template/scripts/lib/device-identity.test.ts +29 -0
  73. package/dist/template/scripts/lib/doctor-probes.mjs +139 -0
  74. package/dist/template/scripts/lib/doctor.mjs +208 -0
  75. package/dist/template/scripts/lib/doctor.test.mjs +262 -0
  76. package/dist/template/scripts/lib/expo-app-configurator.mjs +14 -9
  77. package/dist/template/scripts/lib/expo-app-configurator.test.ts +3 -2
  78. package/dist/template/scripts/lib/ota-client-configurator.mjs +69 -0
  79. package/dist/template/scripts/lib/ota-client-configurator.test.ts +88 -0
  80. package/dist/template/scripts/lib/ota-client.test.ts +55 -0
  81. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +173 -0
  82. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +123 -0
  83. package/dist/template/scripts/lib/ota-update-controller.test.ts +231 -0
  84. package/dist/template/scripts/tests/device-analytics-migrations.mjs +80 -0
  85. package/dist/template/scripts/tests/device-analytics.mjs +126 -0
  86. package/dist/template/scripts/tests/device-heartbeat.mjs +86 -0
  87. package/dist/template/scripts/tests/device-targeting-activation.mjs +111 -0
  88. package/dist/template/scripts/tests/device-targeting.mjs +39 -0
  89. package/dist/template/scripts/tests/device-version-metadata.mjs +31 -0
  90. package/dist/template/scripts/tests/manifest-identity-concurrency.mjs +106 -0
  91. package/dist/template/scripts/tests/manifest-identity.sql +204 -0
  92. package/dist/template/src/client/admin-types.ts +1 -0
  93. package/dist/template/src/client/devices/analytics.tsx +429 -0
  94. package/dist/template/src/client/devices/device-name.tsx +82 -0
  95. package/dist/template/src/client/devices/registry.tsx +399 -0
  96. package/dist/template/src/client/devices/use-devices-report.ts +108 -0
  97. package/dist/template/src/client/devices/version-adoption-chart.tsx +269 -0
  98. package/dist/template/src/client/devices/version-groups.test.ts +57 -0
  99. package/dist/template/src/client/devices/version-groups.ts +101 -0
  100. package/dist/template/src/client/distribution-control.tsx +135 -81
  101. package/dist/template/src/client/guard-action-combobox.tsx +13 -24
  102. package/dist/template/src/client/i18n/en.json +286 -0
  103. package/dist/template/src/client/i18n/messages.test.ts +42 -0
  104. package/dist/template/src/client/i18n/messages.ts +31 -0
  105. package/dist/template/src/client/i18n/provider.tsx +55 -0
  106. package/dist/template/src/client/i18n/use-formatters.ts +38 -0
  107. package/dist/template/src/client/logout-button.tsx +3 -1
  108. package/dist/template/src/client/update-audience-editor.tsx +285 -0
  109. package/dist/template/src/client/update-policy-editor.tsx +53 -41
  110. package/dist/template/src/server/controllers/manifest.test.ts +250 -7
  111. package/dist/template/src/server/controllers/manifest.ts +39 -31
  112. package/dist/template/src/server/devices/audience.test.ts +48 -0
  113. package/dist/template/src/server/devices/audience.ts +86 -0
  114. package/dist/template/src/server/devices/heartbeat.test.ts +39 -0
  115. package/dist/template/src/server/devices/heartbeat.ts +75 -0
  116. package/dist/template/src/server/devices/report-types.ts +114 -0
  117. package/dist/template/src/server/devices/report.test.ts +29 -0
  118. package/dist/template/src/server/devices/report.ts +27 -0
  119. package/dist/template/src/server/lib/admin-audience-activation.test.ts +94 -0
  120. package/dist/template/src/server/lib/admin-updates.ts +32 -168
  121. package/dist/template/src/server/lib/distribution-control.test.ts +23 -2
  122. package/dist/template/src/server/lib/distribution-control.ts +13 -4
  123. package/dist/template/src/server/lib/supabase.js +10 -10
  124. package/dist/template/src/server/lib/supabase.test.ts +11 -0
  125. package/dist/template/src/server/manifest/manifest-identity.test.ts +30 -0
  126. package/dist/template/src/server/manifest/manifest-identity.ts +24 -0
  127. package/dist/template/src/server/manifest/manifest-repository.ts +31 -102
  128. package/dist/template/src/server/manifest/manifest-response.ts +1 -1
  129. package/dist/template/templates/expo-app/ota-client/README.md +59 -0
  130. package/dist/template/templates/expo-app/ota-client/controller.js +60 -0
  131. package/dist/template/templates/expo-app/ota-client/device-identity.js +26 -0
  132. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +52 -0
  133. package/dist/template/templates/expo-app/ota-client/index.d.ts +47 -0
  134. package/dist/template/templates/expo-app/ota-client/index.js +3 -0
  135. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.js +48 -0
  136. package/dist/template/templates/expo-app/ota-client/model/update-controller.js +182 -0
  137. package/dist/template/templates/expo-app/ota-client/runtime.js +112 -0
  138. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.js +56 -0
  139. package/dist/template-manifest.json +582 -77
  140. package/lib/cli.mjs +4 -6
  141. package/package.json +1 -1
@@ -0,0 +1,82 @@
1
+ "use client";
2
+ import { useI18n } from "@/src/client/i18n/provider";
3
+
4
+ import { useEffect, useState } from "react";
5
+ import { Input } from "@/components/ui/input";
6
+ import { Button } from "@/components/ui/button";
7
+ export function DeviceName({
8
+ deviceId,
9
+ platform,
10
+ }: {
11
+ deviceId: string;
12
+ platform: string;
13
+ }) {
14
+ const { t } = useI18n();
15
+ const [name, setName] = useState("");
16
+ const [status, setStatus] = useState("");
17
+ const [busy, setBusy] = useState(false);
18
+ const [loaded, setLoaded] = useState(false);
19
+ useEffect(() => {
20
+ const abort = new AbortController();
21
+ fetch(
22
+ `/api/admin/device-targets?platform=${platform}&deviceId=${deviceId}`,
23
+ {
24
+ signal: abort.signal,
25
+ },
26
+ )
27
+ .then(async (r) => {
28
+ if (!r.ok) throw new Error("Не удалось загрузить имя");
29
+ const data = await r.json();
30
+ if (abort.signal.aborted) return;
31
+ setName(data.name || "");
32
+ setLoaded(true);
33
+ })
34
+ .catch((e) => {
35
+ if (!abort.signal.aborted) setStatus(e.message);
36
+ });
37
+ return () => abort.abort();
38
+ }, [deviceId, platform]);
39
+ async function save() {
40
+ setBusy(true);
41
+ setStatus("");
42
+ try {
43
+ const r = await fetch("/api/admin/device-targets", {
44
+ method: "PUT",
45
+ headers: { "Content-Type": "application/json" },
46
+ body: JSON.stringify({ deviceId, name: name.trim() }),
47
+ });
48
+ if (!r.ok)
49
+ throw new Error((await r.json()).error || "Не удалось сохранить");
50
+ setStatus(t("Имя сохранено — устройство можно выбрать в аудитории OTA."));
51
+ } catch (e) {
52
+ setStatus((e as Error).message);
53
+ } finally {
54
+ setBusy(false);
55
+ }
56
+ }
57
+ return (
58
+ <div className="space-y-2 px-6 pb-6">
59
+ <label htmlFor="ota-device-name" className="text-sm font-medium">
60
+ {t("Имя устройства")}
61
+ </label>
62
+ <Input
63
+ id="ota-device-name"
64
+ maxLength={80}
65
+ placeholder={t("Мой телефон")}
66
+ value={name}
67
+ disabled={!loaded || busy}
68
+ onChange={(e) => setName(e.target.value)}
69
+ />
70
+ <Button
71
+ variant="outline"
72
+ onClick={save}
73
+ disabled={!loaded || busy || !name.trim()}
74
+ >
75
+ {t("Сохранить имя")}
76
+ </Button>
77
+ <p className="text-xs text-muted-foreground" role="status">
78
+ {t(status)}
79
+ </p>
80
+ </div>
81
+ );
82
+ }
@@ -0,0 +1,399 @@
1
+ "use client";
2
+ import { useFormatters } from "@/src/client/i18n/use-formatters";
3
+ import { useI18n } from "@/src/client/i18n/provider";
4
+
5
+ import { DeviceName } from "./device-name";
6
+ import { useRef, useState } from "react";
7
+ import { Copy, Check, ChevronLeft, ChevronRight, Search } from "lucide-react";
8
+ import { Button } from "@/components/ui/button";
9
+ import { Badge } from "@/components/ui/badge";
10
+ import {
11
+ Card,
12
+ CardHeader,
13
+ CardTitle,
14
+ CardDescription,
15
+ CardContent,
16
+ } from "@/components/ui/card";
17
+ import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
18
+ import {
19
+ Table,
20
+ TableHeader,
21
+ TableRow,
22
+ TableHead,
23
+ TableBody,
24
+ TableCell,
25
+ } from "@/components/ui/table";
26
+ import {
27
+ Sheet,
28
+ SheetContent,
29
+ SheetHeader,
30
+ SheetTitle,
31
+ SheetDescription,
32
+ } from "@/components/ui/sheet";
33
+ import type {
34
+ DeviceInstallation,
35
+ DevicesReport,
36
+ } from "@/src/server/devices/report-types";
37
+ import { useKindNames, percent } from "./analytics";
38
+ function CopyValue({ value }: { value: string }) {
39
+ const { t } = useI18n();
40
+ const [state, setState] = useState<"idle" | "copied" | "failed">("idle");
41
+ return (
42
+ <div className="flex items-start gap-2">
43
+ <code className="min-w-0 flex-1 break-all text-xs leading-6">
44
+ {value}
45
+ </code>
46
+ <Button
47
+ variant="ghost"
48
+ size="icon"
49
+ className="size-7 shrink-0"
50
+ aria-label={t("Скопировать {0}", [value])}
51
+ onClick={async () => {
52
+ try {
53
+ await navigator.clipboard.writeText(value);
54
+ setState("copied");
55
+ } catch {
56
+ setState("failed");
57
+ }
58
+ }}
59
+ >
60
+ {state === "copied" ? (
61
+ <Check className="size-3.5" />
62
+ ) : (
63
+ <Copy className="size-3.5" />
64
+ )}
65
+ </Button>
66
+ <span className="sr-only" role="status">
67
+ {state === "copied"
68
+ ? t("Скопировано")
69
+ : state === "failed"
70
+ ? t("Не удалось скопировать. Выделите текст вручную.")
71
+ : ""}
72
+ </span>
73
+ </div>
74
+ );
75
+ }
76
+ export function Registry({
77
+ report,
78
+ loading,
79
+ paginate,
80
+ }: {
81
+ report: DevicesReport;
82
+ loading: boolean;
83
+ paginate: (offset: number) => void;
84
+ }) {
85
+ const { t } = useI18n();
86
+ const opener = useRef<HTMLButtonElement | null>(null);
87
+ const kindNames = useKindNames();
88
+ const { number, fullDate } = useFormatters();
89
+ const [selected, setSelected] = useState<DeviceInstallation | null>(null);
90
+ return (
91
+ <>
92
+ <Card className="min-w-0 shadow-none">
93
+ <CardHeader>
94
+ <div className="flex flex-wrap items-center gap-3">
95
+ <CardTitle>{t("Реестр установок")}</CardTitle>
96
+ <Badge variant="secondary">{number(report.total)}</Badge>
97
+ </div>
98
+ <CardDescription>
99
+ {t(
100
+ "Последнее известное состояние. Нажмите на ID, чтобы посмотреть детали.",
101
+ )}
102
+ </CardDescription>
103
+ </CardHeader>
104
+ <CardContent>
105
+ <Tabs defaultValue="devices">
106
+ <TabsList className="mb-4">
107
+ <TabsTrigger value="devices">{t("Установки")}</TabsTrigger>
108
+ <TabsTrigger value="versions">
109
+ {t("Все версии · ")}
110
+ {report.versions.length}
111
+ </TabsTrigger>
112
+ </TabsList>
113
+ <TabsContent value="devices">
114
+ <Table>
115
+ <TableHeader>
116
+ <TableRow>
117
+ <TableHead>{t("Установка")}</TableHead>
118
+ <TableHead>{t("Платформа / канал")}</TableHead>
119
+ <TableHead>{t("Версия")}</TableHead>
120
+ <TableHead>{t("Запуск")}</TableHead>
121
+ <TableHead>{t("Последняя связь · UTC")}</TableHead>
122
+ </TableRow>
123
+ </TableHeader>
124
+ <TableBody>
125
+ {report.devices.map((d) => (
126
+ <TableRow key={`${d.platform}:${d.installation_id}`}>
127
+ <TableCell>
128
+ <Button
129
+ variant="link"
130
+ className="h-auto p-0 font-mono text-xs"
131
+ title={d.installation_id}
132
+ onClick={(event) => {
133
+ opener.current = event.currentTarget;
134
+ setSelected(d);
135
+ }}
136
+ aria-label={t("Детали установки {0}", [
137
+ d.installation_id,
138
+ ])}
139
+ >
140
+ {d.installation_id.slice(0, 8)}…
141
+ {d.installation_id.slice(-4)}
142
+ </Button>
143
+ <p className="mt-1 text-xs text-muted-foreground">
144
+ {t("С ")}
145
+ {fullDate(d.first_seen_at).split(",")[0]}
146
+ </p>
147
+ </TableCell>
148
+ <TableCell>
149
+ <span className="capitalize">{d.platform}</span>
150
+ <p className="mt-1 text-xs text-muted-foreground">
151
+ {d.channel}
152
+ </p>
153
+ </TableCell>
154
+ <TableCell>
155
+ <p
156
+ className="max-w-56 truncate text-sm"
157
+ title={
158
+ d.update_label ||
159
+ d.resolved_update_id ||
160
+ kindNames[d.launch_kind]
161
+ }
162
+ >
163
+ {d.update_label ||
164
+ d.resolved_update_id?.slice(0, 8) ||
165
+ kindNames[d.launch_kind]}
166
+ </p>
167
+ <p
168
+ className="mt-1 text-xs text-muted-foreground"
169
+ title={d.runtime_version}
170
+ >
171
+ {d.runtime_version}
172
+ </p>
173
+ <p className="mt-1 text-xs text-muted-foreground">
174
+ Native {d.native_version || "—"} (
175
+ {d.native_build || "—"})
176
+ </p>
177
+ </TableCell>
178
+ <TableCell>
179
+ <Badge
180
+ variant={
181
+ d.launch_kind === "ota" ? "default" : "secondary"
182
+ }
183
+ >
184
+ {kindNames[d.launch_kind]}
185
+ </Badge>
186
+ {d.emergency_launch && (
187
+ <Badge
188
+ variant="destructive"
189
+ className="mt-1 block w-fit"
190
+ >
191
+ Emergency
192
+ </Badge>
193
+ )}
194
+ </TableCell>
195
+ <TableCell className="text-xs tabular-nums">
196
+ {fullDate(d.last_seen_at).replace(" UTC", "")}
197
+ </TableCell>
198
+ </TableRow>
199
+ ))}
200
+ </TableBody>
201
+ </Table>
202
+ {!report.devices.length && (
203
+ <div className="flex flex-col items-center gap-2 py-12 text-center">
204
+ <Search className="size-6 text-muted-foreground" />
205
+ <p className="text-sm font-medium">
206
+ {t("Установки не найдены")}
207
+ </p>
208
+ <p className="max-w-sm text-xs leading-5 text-muted-foreground">
209
+ {t(
210
+ "Измените фильтры или дождитесь heartbeat от приложения с подключённой телеметрией.",
211
+ )}
212
+ </p>
213
+ </div>
214
+ )}
215
+ <div className="mt-4 flex flex-wrap items-center justify-between gap-3 border-t pt-4">
216
+ <p className="text-xs text-muted-foreground">
217
+ {report.total
218
+ ? t("{0}–{1} из {2}", [
219
+ number(report.offset + 1),
220
+ number(
221
+ Math.min(
222
+ report.offset + report.pageSize,
223
+ report.total,
224
+ ),
225
+ ),
226
+ number(report.total),
227
+ ])
228
+ : t("0 установок")}
229
+ </p>
230
+ <div className="flex gap-2">
231
+ <Button
232
+ size="sm"
233
+ variant="outline"
234
+ disabled={loading || report.offset === 0}
235
+ onClick={() =>
236
+ paginate(Math.max(0, report.offset - report.pageSize))
237
+ }
238
+ >
239
+ <ChevronLeft />
240
+ {t("Назад")}
241
+ </Button>
242
+ <Button
243
+ size="sm"
244
+ variant="outline"
245
+ disabled={
246
+ loading || report.offset + report.pageSize >= report.total
247
+ }
248
+ onClick={() => paginate(report.offset + report.pageSize)}
249
+ >
250
+ {t("Далее")}
251
+ <ChevronRight />
252
+ </Button>
253
+ </div>
254
+ </div>
255
+ </TabsContent>
256
+ <TabsContent value="versions">
257
+ <Table>
258
+ <TableHeader>
259
+ <TableRow>
260
+ <TableHead>{t("Версия / ID")}</TableHead>
261
+ <TableHead>{t("Платформа / канал")}</TableHead>
262
+ <TableHead>Runtime / native</TableHead>
263
+ <TableHead>{t("Запуск")}</TableHead>
264
+ <TableHead className="text-right">
265
+ {t("Установки")}
266
+ </TableHead>
267
+ <TableHead className="text-right">{t("Доля")}</TableHead>
268
+ </TableRow>
269
+ </TableHeader>
270
+ <TableBody>
271
+ {report.versions.map((v, i) => (
272
+ <TableRow key={i}>
273
+ <TableCell>
274
+ <p
275
+ className="max-w-56 truncate font-medium"
276
+ title={v.update_label || undefined}
277
+ >
278
+ {v.update_label || kindNames[v.launch_kind]}
279
+ </p>
280
+ {v.update_id && (
281
+ <code
282
+ className="mt-1 block text-xs text-muted-foreground"
283
+ title={v.update_id}
284
+ >
285
+ {v.update_id}
286
+ </code>
287
+ )}
288
+ </TableCell>
289
+ <TableCell>
290
+ {v.platform}
291
+ <p className="text-xs text-muted-foreground">
292
+ {v.channel}
293
+ </p>
294
+ </TableCell>
295
+ <TableCell>
296
+ {v.runtime_version}
297
+ <p className="text-xs text-muted-foreground">
298
+ {v.native_version || "—"} ({v.native_build || "—"})
299
+ </p>
300
+ </TableCell>
301
+ <TableCell>
302
+ <Badge variant="secondary">
303
+ {kindNames[v.launch_kind]}
304
+ </Badge>
305
+ </TableCell>
306
+ <TableCell className="text-right tabular-nums">
307
+ {number(v.installations)}
308
+ </TableCell>
309
+ <TableCell className="text-right tabular-nums">
310
+ {percent(v.installations, report.total)}
311
+ </TableCell>
312
+ </TableRow>
313
+ ))}
314
+ </TableBody>
315
+ </Table>
316
+ {!report.versions.length && (
317
+ <p className="py-8 text-center text-sm text-muted-foreground">
318
+ {t("Нет версий в выбранной выборке.")}
319
+ </p>
320
+ )}
321
+ </TabsContent>
322
+ </Tabs>
323
+ </CardContent>
324
+ </Card>
325
+ <Sheet
326
+ open={!!selected}
327
+ onOpenChange={(open) => {
328
+ if (!open) setSelected(null);
329
+ }}
330
+ >
331
+ <SheetContent
332
+ className="w-full overflow-y-auto sm:max-w-lg"
333
+ onCloseAutoFocus={(event) => {
334
+ event.preventDefault();
335
+ opener.current?.focus();
336
+ }}
337
+ >
338
+ <SheetHeader>
339
+ <SheetTitle>{t("Детали установки")}</SheetTitle>
340
+ <SheetDescription>
341
+ {t(
342
+ "Идентификатор установки приложения и последнее зарегистрированное состояние.",
343
+ )}
344
+ </SheetDescription>
345
+ </SheetHeader>
346
+ {selected?.device_id && (
347
+ <DeviceName
348
+ key={selected.device_id}
349
+ deviceId={selected.device_id}
350
+ platform={selected.platform}
351
+ />
352
+ )}
353
+ {selected && (
354
+ <dl className="space-y-5 px-6 pb-8">
355
+ {[
356
+ ["Installation ID", selected.installation_id],
357
+ ["Device ID", selected.device_id || "—"],
358
+ [t("Платформа"), selected.platform],
359
+ [t("Канал"), selected.channel],
360
+ ["Runtime", selected.runtime_version],
361
+ [
362
+ "Native version / build",
363
+ `${selected.native_version || "—"} (${selected.native_build || "—"})`,
364
+ ],
365
+ [t("Тип запуска"), kindNames[selected.launch_kind]],
366
+ [t("Название обновления"), selected.update_label || "—"],
367
+ ["OTA artifact ID", selected.resolved_update_id || "—"],
368
+ ["Launch update ID", selected.reported_update_id || "—"],
369
+ [
370
+ "Emergency launch",
371
+ selected.emergency_launch ? t("Да") : t("Нет"),
372
+ ],
373
+ [t("Первый heartbeat"), fullDate(selected.first_seen_at)],
374
+ [t("Последний heartbeat"), fullDate(selected.last_seen_at)],
375
+ [
376
+ t("Время события на устройстве"),
377
+ fullDate(selected.observed_at),
378
+ ],
379
+ ].map(([label, value]) => (
380
+ <div key={label}>
381
+ <dt className="mb-1 text-xs font-medium text-muted-foreground">
382
+ {label}
383
+ </dt>
384
+ <dd>
385
+ {label.includes("ID") && value !== "—" ? (
386
+ <CopyValue value={value} />
387
+ ) : (
388
+ <p className="break-words text-sm">{value}</p>
389
+ )}
390
+ </dd>
391
+ </div>
392
+ ))}
393
+ </dl>
394
+ )}
395
+ </SheetContent>
396
+ </Sheet>
397
+ </>
398
+ );
399
+ }
@@ -0,0 +1,108 @@
1
+ "use client";
2
+ import { useI18n } from "@/src/client/i18n/provider";
3
+
4
+ import { useEffect, useState } from "react";
5
+ import type { DevicesReport } from "@/src/server/devices/report-types";
6
+ export type Filters = {
7
+ days: string;
8
+ platform: string;
9
+ channel: string;
10
+ runtime: string;
11
+ launchKind: string;
12
+ search: string;
13
+ };
14
+ export const defaultFilters: Filters = {
15
+ days: "7",
16
+ platform: "",
17
+ channel: "",
18
+ runtime: "",
19
+ launchKind: "",
20
+ search: "",
21
+ };
22
+ export function useDevicesReport() {
23
+ const { t } = useI18n();
24
+ const [filters, setFilters] = useState(defaultFilters);
25
+ const [applied, setApplied] = useState(defaultFilters);
26
+ const [offset, setOffset] = useState(0);
27
+ const [revision, setRevision] = useState(0);
28
+ const [report, setReport] = useState<DevicesReport | null>(null);
29
+ const [error, setError] = useState("");
30
+ const [loading, setLoading] = useState(true);
31
+ useEffect(() => {
32
+ const controller = new AbortController();
33
+ const params = new URLSearchParams({ offset: String(offset) });
34
+ Object.entries(applied).forEach(([key, value]) => {
35
+ if (value.trim()) params.set(key, value.trim());
36
+ });
37
+ fetch(`/api/admin/devices?${params}`, {
38
+ signal: controller.signal,
39
+ cache: "no-store",
40
+ })
41
+ .then(async (response) => {
42
+ const data = await response.json();
43
+ if (!response.ok)
44
+ throw new Error(data.error || "Не удалось загрузить статистику");
45
+ return data as DevicesReport;
46
+ })
47
+ .then((data) => {
48
+ if (!controller.signal.aborted) {
49
+ if (data.offset > 0 && data.offset >= data.total) {
50
+ setOffset(
51
+ Math.max(
52
+ 0,
53
+ Math.floor((data.total - 1) / data.pageSize) * data.pageSize,
54
+ ),
55
+ );
56
+ return;
57
+ }
58
+ setReport(data);
59
+ setError("");
60
+ setLoading(false);
61
+ }
62
+ })
63
+ .catch((e) => {
64
+ if (!controller.signal.aborted) {
65
+ setError(e.message);
66
+ setLoading(false);
67
+ }
68
+ });
69
+ return () => controller.abort();
70
+ }, [applied, offset, revision]);
71
+ const refresh = () => {
72
+ setLoading(true);
73
+ setError("");
74
+ setRevision((n) => n + 1);
75
+ };
76
+ const apply = () => {
77
+ setLoading(true);
78
+ setError("");
79
+ setApplied({ ...filters });
80
+ setOffset(0);
81
+ };
82
+ const reset = () => {
83
+ setFilters(defaultFilters);
84
+ setApplied({ ...defaultFilters });
85
+ setOffset(0);
86
+ setLoading(true);
87
+ setError("");
88
+ };
89
+ const paginate = (next: number) => {
90
+ setLoading(true);
91
+ setError("");
92
+ setOffset(next);
93
+ setRevision((n) => n + 1);
94
+ };
95
+ return {
96
+ filters,
97
+ setFilters,
98
+ applied,
99
+ report,
100
+ error: t(error),
101
+ loading,
102
+ offset,
103
+ apply,
104
+ reset,
105
+ refresh,
106
+ paginate,
107
+ };
108
+ }