create-expo-updates-server 0.1.6 → 0.2.1

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 (143) hide show
  1. package/dist/template/.dockerignore +16 -0
  2. package/dist/template/Dockerfile +5 -0
  3. package/dist/template/README.md +51 -660
  4. package/dist/template/app/api/admin/device-targets/route.test.ts +43 -0
  5. package/dist/template/app/api/admin/device-targets/route.ts +91 -0
  6. package/dist/template/app/api/admin/devices/route.test.ts +41 -0
  7. package/dist/template/app/api/admin/devices/route.ts +15 -0
  8. package/dist/template/app/api/admin/updates/[encodedKey]/audience/route.ts +75 -0
  9. package/dist/template/app/api/device-heartbeat/route.ts +2 -0
  10. package/dist/template/app/globals.css +114 -15
  11. package/dist/template/app/layout.tsx +12 -4
  12. package/dist/template/app/login/page.tsx +22 -18
  13. package/dist/template/app/updates/[encodedKey]/page.tsx +112 -88
  14. package/dist/template/app/updates/devices/page.tsx +264 -0
  15. package/dist/template/app/updates/embedded/page.tsx +102 -68
  16. package/dist/template/app/updates/emergency-redirects/page.tsx +144 -111
  17. package/dist/template/app/updates/layout.tsx +6 -1
  18. package/dist/template/app/updates/page.tsx +262 -212
  19. package/dist/template/components/admin/admin-shell.tsx +196 -0
  20. package/dist/template/components/admin/language-switcher.tsx +25 -0
  21. package/dist/template/components/admin/theme-provider.tsx +15 -0
  22. package/dist/template/components/ui/alert.tsx +65 -0
  23. package/dist/template/components/ui/badge.tsx +47 -0
  24. package/dist/template/components/ui/button.tsx +63 -0
  25. package/dist/template/components/ui/card.tsx +91 -0
  26. package/dist/template/components/ui/chart.tsx +373 -0
  27. package/dist/template/components/ui/checkbox.tsx +31 -0
  28. package/dist/template/components/ui/dialog.tsx +158 -0
  29. package/dist/template/components/ui/dropdown-menu.tsx +256 -0
  30. package/dist/template/components/ui/input.tsx +20 -0
  31. package/dist/template/components/ui/label.tsx +23 -0
  32. package/dist/template/components/ui/native-select.tsx +61 -0
  33. package/dist/template/components/ui/popover.tsx +88 -0
  34. package/dist/template/components/ui/scroll-area.tsx +57 -0
  35. package/dist/template/components/ui/select.tsx +189 -0
  36. package/dist/template/components/ui/separator.tsx +27 -0
  37. package/dist/template/components/ui/sheet.tsx +142 -0
  38. package/dist/template/components/ui/sidebar.tsx +736 -0
  39. package/dist/template/components/ui/skeleton.tsx +13 -0
  40. package/dist/template/components/ui/switch.tsx +34 -0
  41. package/dist/template/components/ui/table.tsx +115 -0
  42. package/dist/template/components/ui/tabs.tsx +90 -0
  43. package/dist/template/components/ui/textarea.tsx +17 -0
  44. package/dist/template/components/ui/tooltip.tsx +56 -0
  45. package/dist/template/components.json +22 -0
  46. package/dist/template/docker/minio.Dockerfile +11 -0
  47. package/dist/template/docker-compose.yml +7 -9
  48. package/dist/template/docs/admin-ui.md +35 -0
  49. package/dist/template/docs/device-targeting.md +129 -0
  50. package/dist/template/docs/doctor.md +137 -0
  51. package/dist/template/docs/expo-app-setup.md +174 -47
  52. package/dist/template/docs/getting-started.md +109 -146
  53. package/dist/template/docs/getting-started.ru.md +110 -193
  54. package/dist/template/docs/index.md +14 -33
  55. package/dist/template/docs/installation-telemetry.md +233 -0
  56. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +89 -0
  57. package/dist/template/docs/migrations/2026-10-19-device-heartbeat.sql +131 -0
  58. package/dist/template/docs/migrations/2026-10-22-device-analytics.sql +264 -0
  59. package/dist/template/docs/migrations/2026-10-22-device-targeting-activation.sql +108 -0
  60. package/dist/template/docs/migrations/2026-10-22-device-targeting.sql +130 -0
  61. package/dist/template/docs/migrations/2026-10-22-device-version-metadata.sql +154 -0
  62. package/dist/template/docs/migrations/schema.sql +868 -6
  63. package/dist/template/docs/npm-installer.md +15 -30
  64. package/dist/template/docs/operations-guide.md +87 -0
  65. package/dist/template/docs/ota-client-lifecycle.md +263 -0
  66. package/dist/template/docs/ota-update-policy.md +36 -2
  67. package/dist/template/docs/supabase-setup.md +4 -0
  68. package/dist/template/hooks/use-mobile.ts +21 -0
  69. package/dist/template/lib/utils.ts +6 -0
  70. package/dist/template/package-lock.json +2325 -101
  71. package/dist/template/package.json +13 -3
  72. package/dist/template/scripts/configure-expo-app.mjs +11 -15
  73. package/dist/template/scripts/doctor.mjs +51 -0
  74. package/dist/template/scripts/ensure-storage-bucket.mjs +25 -0
  75. package/dist/template/scripts/ensure-test-bucket.mjs +21 -0
  76. package/dist/template/scripts/lib/device-identity.test.ts +29 -0
  77. package/dist/template/scripts/lib/doctor-probes.mjs +139 -0
  78. package/dist/template/scripts/lib/doctor.mjs +208 -0
  79. package/dist/template/scripts/lib/doctor.test.mjs +262 -0
  80. package/dist/template/scripts/lib/expo-app-configurator.mjs +14 -9
  81. package/dist/template/scripts/lib/expo-app-configurator.test.ts +3 -2
  82. package/dist/template/scripts/lib/ota-client-configurator.mjs +69 -0
  83. package/dist/template/scripts/lib/ota-client-configurator.test.ts +88 -0
  84. package/dist/template/scripts/lib/ota-client.test.ts +55 -0
  85. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +173 -0
  86. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +123 -0
  87. package/dist/template/scripts/lib/ota-update-controller.test.ts +231 -0
  88. package/dist/template/scripts/tests/device-analytics-migrations.mjs +80 -0
  89. package/dist/template/scripts/tests/device-analytics.mjs +126 -0
  90. package/dist/template/scripts/tests/device-heartbeat.mjs +86 -0
  91. package/dist/template/scripts/tests/device-targeting-activation.mjs +111 -0
  92. package/dist/template/scripts/tests/device-targeting.mjs +39 -0
  93. package/dist/template/scripts/tests/device-version-metadata.mjs +31 -0
  94. package/dist/template/scripts/tests/manifest-identity-concurrency.mjs +106 -0
  95. package/dist/template/scripts/tests/manifest-identity.sql +204 -0
  96. package/dist/template/src/client/admin-types.ts +1 -0
  97. package/dist/template/src/client/devices/analytics.tsx +429 -0
  98. package/dist/template/src/client/devices/device-name.tsx +82 -0
  99. package/dist/template/src/client/devices/registry.tsx +399 -0
  100. package/dist/template/src/client/devices/use-devices-report.ts +108 -0
  101. package/dist/template/src/client/devices/version-adoption-chart.tsx +269 -0
  102. package/dist/template/src/client/devices/version-groups.test.ts +57 -0
  103. package/dist/template/src/client/devices/version-groups.ts +101 -0
  104. package/dist/template/src/client/distribution-control.tsx +135 -81
  105. package/dist/template/src/client/guard-action-combobox.tsx +13 -24
  106. package/dist/template/src/client/i18n/en.json +286 -0
  107. package/dist/template/src/client/i18n/messages.test.ts +42 -0
  108. package/dist/template/src/client/i18n/messages.ts +31 -0
  109. package/dist/template/src/client/i18n/provider.tsx +55 -0
  110. package/dist/template/src/client/i18n/use-formatters.ts +38 -0
  111. package/dist/template/src/client/logout-button.tsx +3 -1
  112. package/dist/template/src/client/update-audience-editor.tsx +285 -0
  113. package/dist/template/src/client/update-policy-editor.tsx +53 -41
  114. package/dist/template/src/server/controllers/manifest.test.ts +250 -7
  115. package/dist/template/src/server/controllers/manifest.ts +39 -31
  116. package/dist/template/src/server/devices/audience.test.ts +48 -0
  117. package/dist/template/src/server/devices/audience.ts +86 -0
  118. package/dist/template/src/server/devices/heartbeat.test.ts +39 -0
  119. package/dist/template/src/server/devices/heartbeat.ts +75 -0
  120. package/dist/template/src/server/devices/report-types.ts +114 -0
  121. package/dist/template/src/server/devices/report.test.ts +29 -0
  122. package/dist/template/src/server/devices/report.ts +27 -0
  123. package/dist/template/src/server/lib/admin-audience-activation.test.ts +94 -0
  124. package/dist/template/src/server/lib/admin-updates.ts +32 -168
  125. package/dist/template/src/server/lib/supabase.js +10 -10
  126. package/dist/template/src/server/lib/supabase.test.ts +11 -0
  127. package/dist/template/src/server/manifest/manifest-identity.test.ts +30 -0
  128. package/dist/template/src/server/manifest/manifest-identity.ts +24 -0
  129. package/dist/template/src/server/manifest/manifest-repository.ts +31 -102
  130. package/dist/template/src/server/manifest/manifest-response.ts +1 -1
  131. package/dist/template/templates/expo-app/ota-client/README.md +59 -0
  132. package/dist/template/templates/expo-app/ota-client/controller.js +60 -0
  133. package/dist/template/templates/expo-app/ota-client/device-identity.js +26 -0
  134. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +52 -0
  135. package/dist/template/templates/expo-app/ota-client/index.d.ts +47 -0
  136. package/dist/template/templates/expo-app/ota-client/index.js +3 -0
  137. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.js +48 -0
  138. package/dist/template/templates/expo-app/ota-client/model/update-controller.js +182 -0
  139. package/dist/template/templates/expo-app/ota-client/runtime.js +112 -0
  140. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.js +56 -0
  141. package/dist/template-manifest.json +592 -77
  142. package/lib/cli.mjs +4 -6
  143. package/package.json +1 -1
@@ -0,0 +1,269 @@
1
+ "use client";
2
+ import { useFormatters } from "@/src/client/i18n/use-formatters";
3
+ import { useI18n } from "@/src/client/i18n/provider";
4
+
5
+ import { useState } from "react";
6
+ import {
7
+ ChevronDown,
8
+ ChevronUp,
9
+ BarChart3,
10
+ Layers,
11
+ GitBranch,
12
+ } from "lucide-react";
13
+ import { Badge } from "@/components/ui/badge";
14
+ import { Button } from "@/components/ui/button";
15
+ import {
16
+ Card,
17
+ CardContent,
18
+ CardDescription,
19
+ CardHeader,
20
+ CardTitle,
21
+ } from "@/components/ui/card";
22
+ import type { DevicesReport } from "@/src/server/devices/report-types";
23
+ import { groupVersions, type VersionScope } from "./version-groups";
24
+ function useKindLabels() {
25
+ const { t } = useI18n();
26
+ return {
27
+ ota: "OTA",
28
+ embedded: "Embedded",
29
+ unknown: t("Не определён"),
30
+ };
31
+ }
32
+ const kindColors = {
33
+ ota: "var(--chart-1)",
34
+ embedded: "var(--chart-2)",
35
+ unknown: "var(--chart-3)",
36
+ };
37
+ const PREVIEW_ROWS = 6;
38
+ function ScopeChart({ scope }: { scope: VersionScope }) {
39
+ const { number, share } = useFormatters();
40
+ const { t } = useI18n();
41
+ const kindLabels = useKindLabels();
42
+
43
+ const [expanded, setExpanded] = useState(false);
44
+ const visible = expanded
45
+ ? scope.versions
46
+ : scope.versions.slice(0, PREVIEW_ROWS);
47
+ return (
48
+ <section
49
+ className="min-w-0 rounded-lg border p-4"
50
+ aria-label={t("Канал {0}, {1}", [scope.channel, scope.platform])}
51
+ >
52
+ <header className="mb-4 flex flex-wrap items-center justify-between gap-3">
53
+ <div className="flex min-w-0 flex-wrap items-center gap-2">
54
+ <GitBranch className="size-4 shrink-0 text-muted-foreground" />
55
+ <span className="text-xs text-muted-foreground">{t("Канал")}</span>
56
+ <h4 className="break-all text-sm font-semibold">{scope.channel}</h4>
57
+ <Badge variant="outline">
58
+ {scope.platform === "ios" ? "iOS" : "Android"}
59
+ </Badge>
60
+ </div>
61
+ <span className="text-xs text-muted-foreground">
62
+ {t("Активных установок:")}{" "}
63
+ <strong className="font-medium text-foreground">
64
+ {number(scope.total)}
65
+ </strong>
66
+ </span>
67
+ </header>
68
+ <div className="hidden grid-cols-[minmax(0,18rem)_minmax(0,1fr)_8rem] gap-5 border-b pb-3 text-xs text-muted-foreground md:grid">
69
+ <span>{t("Версия приложения / OTA")}</span>
70
+ <div className="flex justify-between">
71
+ <span>0%</span>
72
+ <span>100%</span>
73
+ </div>
74
+ <span className="text-right">{t("Количество · доля")}</span>
75
+ </div>
76
+ <ol
77
+ className={expanded ? "max-h-[560px] overflow-y-auto pr-2" : ""}
78
+ aria-label={t("Версии в {0}, {1}", [scope.channel, scope.platform])}
79
+ >
80
+ {visible.map((version) => (
81
+ <li
82
+ key={version.key}
83
+ className="grid min-w-0 gap-x-5 gap-y-3 border-b py-4 last:border-b-0 md:grid-cols-[minmax(0,18rem)_minmax(0,1fr)_8rem] md:items-center"
84
+ >
85
+ <div className="min-w-0">
86
+ <div className="flex flex-wrap items-center gap-2">
87
+ <p className="break-all text-base font-semibold">
88
+ {version.appVersion || t("Версия не указана")}
89
+ </p>
90
+ <Badge
91
+ variant="secondary"
92
+ className="font-mono text-[10px]"
93
+ title={version.updateId || undefined}
94
+ >
95
+ {kindLabels[version.kind]}
96
+ {version.updateId ? ` ${version.updateId.slice(0, 8)}` : ""}
97
+ </Badge>
98
+ </div>
99
+ <p className="mt-1 break-words text-xs leading-5 text-muted-foreground">
100
+ Native: {version.nativeBuilds.join(", ")}
101
+ </p>
102
+ {version.updateId && (
103
+ <details className="mt-1 text-xs text-muted-foreground">
104
+ <summary className="w-fit cursor-pointer">
105
+ {t("Полный ID")}
106
+ </summary>
107
+ <code className="mt-1 block break-all select-all">
108
+ {version.updateId}
109
+ </code>
110
+ </details>
111
+ )}
112
+ </div>
113
+ <div
114
+ className="flex h-8 items-center overflow-hidden rounded-md bg-muted/70"
115
+ role="img"
116
+ aria-label={t(
117
+ "{0}, {1} {2}: {3} установок, {4} внутри канала {5}, {6}",
118
+ [
119
+ version.appVersion || t("Версия не указана"),
120
+ kindLabels[version.kind],
121
+ version.updateId || "",
122
+ number(version.installations),
123
+ share(version.installations, scope.total),
124
+ scope.channel,
125
+ scope.platform,
126
+ ],
127
+ )}
128
+ >
129
+ <div
130
+ className="h-full rounded-md"
131
+ style={{
132
+ width: `${scope.total ? (version.installations / scope.total) * 100 : 0}%`,
133
+ minWidth: version.installations ? 2 : 0,
134
+ background: kindColors[version.kind],
135
+ }}
136
+ />
137
+ </div>
138
+ <div className="flex items-baseline justify-end gap-2 tabular-nums">
139
+ <strong className="text-xl font-semibold">
140
+ {number(version.installations)}
141
+ </strong>
142
+ <span className="text-sm text-muted-foreground">
143
+ {share(version.installations, scope.total)}
144
+ </span>
145
+ </div>
146
+ </li>
147
+ ))}
148
+ </ol>
149
+ {scope.versions.length > PREVIEW_ROWS && (
150
+ <div className="mt-3 flex flex-wrap items-center justify-between gap-3 border-t pt-3">
151
+ <p className="text-xs text-muted-foreground">
152
+ {t("Показано ")}
153
+ {visible.length} {t(" из ")}
154
+ {scope.versions.length} {t(" версий. Доли учитывают весь блок.")}
155
+ </p>
156
+ <Button
157
+ variant="outline"
158
+ size="sm"
159
+ aria-expanded={expanded}
160
+ onClick={() => setExpanded((value) => !value)}
161
+ >
162
+ {expanded ? <ChevronUp /> : <ChevronDown />}
163
+ {expanded
164
+ ? t("Свернуть")
165
+ : t("Показать все ({0})", [scope.versions.length])}
166
+ </Button>
167
+ </div>
168
+ )}
169
+ </section>
170
+ );
171
+ }
172
+ export function VersionAdoptionChart({ report }: { report: DevicesReport }) {
173
+ const kindLabels = useKindLabels();
174
+ const { number } = useFormatters();
175
+ const { t } = useI18n();
176
+ const runtimes = groupVersions(report.versions);
177
+ const period =
178
+ report.filters.days === 1
179
+ ? t("24 часа")
180
+ : t("{0} дней", [report.filters.days]);
181
+ return (
182
+ <Card
183
+ className="min-w-0 shadow-none"
184
+ aria-labelledby="version-adoption-title"
185
+ >
186
+ <CardHeader>
187
+ <div className="flex flex-wrap items-center justify-between gap-3">
188
+ <CardTitle id="version-adoption-title">
189
+ {t("Активные установки по версиям")}
190
+ </CardTitle>
191
+ <Badge variant="secondary">
192
+ {t("Всего: ")}
193
+ {number(report.total)}
194
+ </Badge>
195
+ </div>
196
+ <CardDescription>
197
+ {t("Активность за последние ")}
198
+ {period}
199
+ {t(
200
+ ". Runtime → канал → платформа → версия. Доля считается внутри каждого блока канала и платформы.",
201
+ )}
202
+ </CardDescription>
203
+ </CardHeader>
204
+ <CardContent>
205
+ {runtimes.length ? (
206
+ <>
207
+ <div className="mb-5 flex flex-wrap gap-x-5 gap-y-2 text-xs text-muted-foreground">
208
+ {Object.entries(kindLabels).map(([kind, label]) => (
209
+ <span key={kind} className="inline-flex items-center gap-2">
210
+ <i
211
+ className="size-2 rounded-full"
212
+ style={{
213
+ background: kindColors[kind as keyof typeof kindColors],
214
+ }}
215
+ />
216
+ {label}
217
+ </span>
218
+ ))}
219
+ </div>
220
+ <div className="space-y-7">
221
+ {runtimes.map((runtime) => (
222
+ <section
223
+ key={runtime.runtime}
224
+ aria-label={`Runtime ${runtime.runtime}`}
225
+ >
226
+ <header className="mb-3 flex flex-wrap items-center justify-between gap-3">
227
+ <h3 className="flex min-w-0 items-center gap-2 text-sm">
228
+ <Layers className="size-4 shrink-0 text-muted-foreground" />
229
+ <span className="shrink-0 text-muted-foreground">
230
+ Runtime
231
+ </span>
232
+ <span className="break-all font-semibold">
233
+ {runtime.runtime}
234
+ </span>
235
+ </h3>
236
+ <Badge variant="secondary">
237
+ {t("Установок: ")}
238
+ {number(runtime.total)}
239
+ </Badge>
240
+ </header>
241
+ <div className="space-y-3">
242
+ {runtime.scopes.map((scope) => (
243
+ <ScopeChart key={scope.key} scope={scope} />
244
+ ))}
245
+ </div>
246
+ </section>
247
+ ))}
248
+ </div>
249
+ <p className="mt-5 text-xs leading-5 text-muted-foreground">
250
+ {t(
251
+ "Версия берётся из опубликованной сборки. ID различает OTA с одинаковой версией; одна OTA на разных native-сборках объединяется в одну полосу. Учитываются установки приложения за период.",
252
+ )}
253
+ </p>
254
+ </>
255
+ ) : (
256
+ <div className="flex flex-col items-center gap-2 py-8 text-center">
257
+ <BarChart3 className="size-6 text-muted-foreground" />
258
+ <p className="text-sm font-medium">
259
+ {t("Нет активных установок в этой выборке")}
260
+ </p>
261
+ <p className="text-xs text-muted-foreground">
262
+ {t("Измените фильтры или дождитесь heartbeat от приложения.")}
263
+ </p>
264
+ </div>
265
+ )}
266
+ </CardContent>
267
+ </Card>
268
+ );
269
+ }
@@ -0,0 +1,57 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { groupVersions } from "./version-groups";
3
+ import type { DeviceVersion } from "@/src/server/devices/report-types";
4
+ const version = (patch: Partial<DeviceVersion> = {}): DeviceVersion => ({
5
+ app_version: "1.0.0",
6
+ update_id: "ota-1",
7
+ update_label: "This message must not become a version",
8
+ platform: "android",
9
+ channel: "production",
10
+ runtime_version: "runtime-1",
11
+ native_version: "1.0",
12
+ native_build: "1",
13
+ launch_kind: "ota",
14
+ installations: 2,
15
+ ...patch,
16
+ });
17
+ describe("version adoption groups", () => {
18
+ it("isolates runtime, channel and platform totals", () => {
19
+ const groups = groupVersions([
20
+ version(),
21
+ version({ runtime_version: "runtime-2", installations: 10 }),
22
+ version({ channel: "staging", installations: 5 }),
23
+ version({ platform: "ios", installations: 3 }),
24
+ ]);
25
+ expect(groups.map((g) => g.total).sort((a, b) => a - b)).toEqual([10, 10]);
26
+ const scopes = groups.find((g) => g.runtime === "runtime-1")!.scopes;
27
+ expect(scopes.map((s) => [s.channel, s.platform, s.total])).toEqual([
28
+ ["production", "android", 2],
29
+ ["production", "ios", 3],
30
+ ["staging", "android", 5],
31
+ ]);
32
+ });
33
+ it("merges native builds of one OTA but keeps different OTA IDs with same app version separate", () => {
34
+ const scope = groupVersions([
35
+ version(),
36
+ version({ native_build: "2", installations: 3 }),
37
+ version({ update_id: "ota-2", installations: 4 }),
38
+ ])[0].scopes[0];
39
+ expect(scope.total).toBe(9);
40
+ expect(scope.versions.map((v) => v.installations)).toEqual([5, 4]);
41
+ expect(scope.versions[0]).toMatchObject({
42
+ appVersion: "1.0.0",
43
+ updateId: "ota-1",
44
+ nativeBuilds: ["1.0 (1)", "1.0 (2)"],
45
+ });
46
+ expect(JSON.stringify(scope)).not.toContain("message");
47
+ });
48
+ it("keeps unidentified builds separate and never substitutes native version for missing OTA version", () => {
49
+ const scope = groupVersions([
50
+ version({ app_version: null, update_id: null }),
51
+ version({ app_version: null, update_id: null, native_build: "2" }),
52
+ ])[0].scopes[0];
53
+ expect(scope.versions).toHaveLength(2);
54
+ expect(scope.versions.every((v) => v.appVersion === null)).toBe(true);
55
+ expect(groupVersions([])).toEqual([]);
56
+ });
57
+ });
@@ -0,0 +1,101 @@
1
+ import type { DeviceVersion } from "@/src/server/devices/report-types";
2
+ export type ChartVersion = {
3
+ key: string;
4
+ appVersion: string | null;
5
+ updateId: string | null;
6
+ kind: DeviceVersion["launch_kind"];
7
+ installations: number;
8
+ nativeBuilds: string[];
9
+ };
10
+ export type VersionScope = {
11
+ key: string;
12
+ channel: string;
13
+ platform: DeviceVersion["platform"];
14
+ total: number;
15
+ versions: ChartVersion[];
16
+ };
17
+ export type RuntimeGroup = {
18
+ runtime: string;
19
+ total: number;
20
+ scopes: VersionScope[];
21
+ };
22
+ export function groupVersions(versions: DeviceVersion[]): RuntimeGroup[] {
23
+ const runtimes = new Map<
24
+ string,
25
+ {
26
+ total: number;
27
+ scopes: Map<
28
+ string,
29
+ { scope: VersionScope; versions: Map<string, ChartVersion> }
30
+ >;
31
+ }
32
+ >();
33
+ for (const version of versions) {
34
+ let runtime = runtimes.get(version.runtime_version);
35
+ if (!runtime) {
36
+ runtime = { total: 0, scopes: new Map() };
37
+ runtimes.set(version.runtime_version, runtime);
38
+ }
39
+ const scopeKey = JSON.stringify([version.channel, version.platform]);
40
+ let group = runtime.scopes.get(scopeKey);
41
+ if (!group) {
42
+ group = {
43
+ scope: {
44
+ key: scopeKey,
45
+ channel: version.channel,
46
+ platform: version.platform,
47
+ total: 0,
48
+ versions: [],
49
+ },
50
+ versions: new Map(),
51
+ };
52
+ runtime.scopes.set(scopeKey, group);
53
+ }
54
+ // Same OTA can run on several native builds; it remains one version bar.
55
+ // Without an artifact ID, keep native builds separate instead of inventing identity.
56
+ const key = JSON.stringify([
57
+ version.launch_kind,
58
+ version.update_id,
59
+ version.app_version ?? null,
60
+ ...(version.update_id
61
+ ? []
62
+ : [version.native_version, version.native_build]),
63
+ ]);
64
+ let row = group.versions.get(key);
65
+ if (!row) {
66
+ row = {
67
+ key,
68
+ appVersion: version.app_version ?? null,
69
+ updateId: version.update_id,
70
+ kind: version.launch_kind,
71
+ installations: 0,
72
+ nativeBuilds: [],
73
+ };
74
+ group.versions.set(key, row);
75
+ }
76
+ const native = `${version.native_version || "—"} (${version.native_build || "—"})`;
77
+ if (!row.nativeBuilds.includes(native)) row.nativeBuilds.push(native);
78
+ row.installations += version.installations;
79
+ group.scope.total += version.installations;
80
+ runtime.total += version.installations;
81
+ }
82
+ return [...runtimes]
83
+ .map(([runtime, data]) => ({
84
+ runtime,
85
+ total: data.total,
86
+ scopes: [...data.scopes.values()]
87
+ .map(({ scope, versions }) => ({
88
+ ...scope,
89
+ versions: [...versions.values()].sort(
90
+ (a, b) =>
91
+ b.installations - a.installations || a.key.localeCompare(b.key),
92
+ ),
93
+ }))
94
+ .sort(
95
+ (a, b) =>
96
+ a.channel.localeCompare(b.channel) ||
97
+ a.platform.localeCompare(b.platform),
98
+ ),
99
+ }))
100
+ .sort((a, b) => b.total - a.total || a.runtime.localeCompare(b.runtime));
101
+ }