create-expo-updates-server 0.4.0 → 0.6.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 (175) hide show
  1. package/README.md +4 -2
  2. package/dist/template/Dockerfile +4 -6
  3. package/dist/template/README.md +4 -9
  4. package/dist/template/app/api/admin/device-favorites/route.test.ts +125 -0
  5. package/dist/template/app/api/admin/device-favorites/route.ts +91 -0
  6. package/dist/template/app/api/admin/distribution-history/restore/route.ts +39 -0
  7. package/dist/template/app/api/admin/distribution-history/route.test.ts +55 -0
  8. package/dist/template/app/api/admin/distribution-history/route.ts +24 -0
  9. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +2 -1
  10. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +2 -1
  11. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +2 -1
  12. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +2 -1
  13. package/dist/template/app/api/admin/updates/route.test.ts +21 -0
  14. package/dist/template/app/api/admin/updates/route.ts +12 -1
  15. package/dist/template/app/globals.css +65 -68
  16. package/dist/template/app/login/page.tsx +66 -42
  17. package/dist/template/app/updates/[encodedKey]/page.tsx +375 -263
  18. package/dist/template/app/updates/devices/page.tsx +188 -152
  19. package/dist/template/app/updates/embedded/page.tsx +63 -29
  20. package/dist/template/app/updates/emergency-redirects/page.tsx +93 -58
  21. package/dist/template/app/updates/layout.tsx +2 -1
  22. package/dist/template/app/updates/page.tsx +381 -283
  23. package/dist/template/app/updates/releases.test.tsx +156 -0
  24. package/dist/template/components/admin/admin-role.ts +5 -0
  25. package/dist/template/components/admin/admin-shell.tsx +166 -101
  26. package/dist/template/components/admin/confirm-action.tsx +123 -0
  27. package/dist/template/components/admin/language-switcher.tsx +25 -17
  28. package/dist/template/components/admin/release-badges.test.tsx +50 -0
  29. package/dist/template/components/admin/release-badges.tsx +101 -0
  30. package/dist/template/components/ui/alert.tsx +10 -3
  31. package/dist/template/components/ui/avatar.tsx +108 -0
  32. package/dist/template/components/ui/badge.tsx +2 -2
  33. package/dist/template/components/ui/breadcrumb.tsx +108 -0
  34. package/dist/template/components/ui/button.tsx +5 -5
  35. package/dist/template/components/ui/card.tsx +2 -2
  36. package/dist/template/components/ui/chart.tsx +1 -1
  37. package/dist/template/components/ui/collapsible.tsx +7 -0
  38. package/dist/template/components/ui/empty.tsx +103 -0
  39. package/dist/template/components/ui/field.tsx +248 -0
  40. package/dist/template/components/ui/input-group.tsx +170 -0
  41. package/dist/template/components/ui/input.tsx +1 -1
  42. package/dist/template/components/ui/native-select.tsx +1 -1
  43. package/dist/template/components/ui/radio-group.tsx +41 -0
  44. package/dist/template/components/ui/status-tones.ts +15 -0
  45. package/dist/template/components/ui/table.tsx +1 -1
  46. package/dist/template/components/ui/textarea.tsx +1 -1
  47. package/dist/template/docs/admin-ui.md +7 -3
  48. package/dist/template/docs/device-targeting.md +2 -7
  49. package/dist/template/docs/doctor.md +2 -2
  50. package/dist/template/docs/expo-app-setup.md +9 -27
  51. package/dist/template/docs/getting-started.md +3 -4
  52. package/dist/template/docs/installation-telemetry.md +2 -8
  53. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +5 -0
  54. package/dist/template/docs/migrations/2026-11-25-device-favorites.sql +53 -0
  55. package/dist/template/docs/migrations/2026-11-25-download-on-postpone.sql +5 -0
  56. package/dist/template/docs/migrations/2026-11-25-public-distribution-history.sql +283 -0
  57. package/dist/template/docs/migrations/schema.sql +349 -0
  58. package/dist/template/docs/npm-installer.md +3 -3
  59. package/dist/template/docs/operations-guide.md +8 -6
  60. package/dist/template/docs/ota-client-lifecycle.md +30 -68
  61. package/dist/template/docs/ota-update-policy.md +9 -20
  62. package/dist/template/docs/ru/admin-ui.md +7 -3
  63. package/dist/template/docs/ru/device-targeting.md +2 -2
  64. package/dist/template/docs/ru/doctor.md +2 -2
  65. package/dist/template/docs/ru/expo-app-setup.md +9 -9
  66. package/dist/template/docs/ru/getting-started.md +3 -3
  67. package/dist/template/docs/ru/installation-telemetry.md +2 -2
  68. package/dist/template/docs/ru/npm-installer.md +3 -3
  69. package/dist/template/docs/ru/operations-guide.md +8 -3
  70. package/dist/template/docs/ru/ota-client-lifecycle.md +30 -35
  71. package/dist/template/docs/ru/ota-update-policy.md +9 -7
  72. package/dist/template/docs/ru/publishing-npm-installer.md +2 -2
  73. package/dist/template/docs/uk/admin-ui.md +7 -3
  74. package/dist/template/docs/uk/device-targeting.md +2 -2
  75. package/dist/template/docs/uk/doctor.md +2 -2
  76. package/dist/template/docs/uk/expo-app-setup.md +9 -9
  77. package/dist/template/docs/uk/getting-started.md +3 -3
  78. package/dist/template/docs/uk/installation-telemetry.md +2 -2
  79. package/dist/template/docs/uk/npm-installer.md +3 -3
  80. package/dist/template/docs/uk/operations-guide.md +8 -3
  81. package/dist/template/docs/uk/ota-client-lifecycle.md +30 -35
  82. package/dist/template/docs/uk/ota-update-policy.md +9 -7
  83. package/dist/template/docs/uk/publishing-npm-installer.md +2 -2
  84. package/dist/template/lib/audience-ids.ts +4 -0
  85. package/dist/template/package-lock.json +46 -0
  86. package/dist/template/package.json +3 -1
  87. package/dist/template/scripts/configure-expo-app.mjs +3 -3
  88. package/dist/template/scripts/create-access-token.mjs +1 -1
  89. package/dist/template/scripts/lib/doctor.mjs +10 -7
  90. package/dist/template/scripts/lib/doctor.test.mjs +7 -5
  91. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +22 -7
  92. package/dist/template/scripts/lib/expo-app-configurator.mjs +25 -22
  93. package/dist/template/scripts/lib/expo-app-configurator.test.ts +37 -2
  94. package/dist/template/scripts/lib/ota-client-configurator.mjs +152 -55
  95. package/dist/template/scripts/lib/ota-client-configurator.test.ts +60 -20
  96. package/dist/template/scripts/lib/ota-client-package.mjs +25 -0
  97. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +90 -32
  98. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +49 -6
  99. package/dist/template/scripts/tests/device-analytics-migrations.mjs +12 -1
  100. package/dist/template/scripts/tests/device-targeting-activation.mjs +2 -3
  101. package/dist/template/scripts/tests/e2e/client-lifecycle.mjs +161 -0
  102. package/dist/template/scripts/tests/e2e/client-lifecycle.scenario.jsx +465 -0
  103. package/dist/template/scripts/tests/e2e/client-vitest.mts +18 -0
  104. package/dist/template/scripts/tests/e2e/console.mjs +86 -0
  105. package/dist/template/scripts/tests/e2e/delivery.mjs +296 -0
  106. package/dist/template/scripts/tests/e2e/device-favorites.mjs +309 -0
  107. package/dist/template/scripts/tests/e2e/installations.mjs +132 -0
  108. package/dist/template/scripts/tests/e2e/policy.mjs +253 -0
  109. package/dist/template/scripts/tests/e2e/registrar-token.mjs +59 -0
  110. package/dist/template/scripts/tests/e2e/release-matrix.mjs +307 -0
  111. package/dist/template/scripts/tests/e2e/releases.mjs +162 -0
  112. package/dist/template/scripts/tests/e2e/run.mjs +224 -0
  113. package/dist/template/scripts/tests/manifest-identity.sql +7 -5
  114. package/dist/template/scripts/tests/public-distribution-history.mjs +102 -0
  115. package/dist/template/src/client/admin-api.ts +45 -10
  116. package/dist/template/src/client/admin-types.ts +35 -1
  117. package/dist/template/src/client/devices/analytics.tsx +168 -231
  118. package/dist/template/src/client/devices/cohorts.tsx +181 -0
  119. package/dist/template/src/client/devices/device-name.tsx +37 -23
  120. package/dist/template/src/client/devices/favorite-targets.tsx +381 -0
  121. package/dist/template/src/client/devices/registry.tsx +258 -245
  122. package/dist/template/src/client/devices/use-devices-report.test.tsx +98 -0
  123. package/dist/template/src/client/devices/use-devices-report.ts +53 -82
  124. package/dist/template/src/client/devices/version-adoption-chart.tsx +47 -49
  125. package/dist/template/src/client/distribution-control.tsx +89 -57
  126. package/dist/template/src/client/distribution-history.tsx +353 -0
  127. package/dist/template/src/client/guard-action-catalog.tsx +183 -0
  128. package/dist/template/src/client/guard-action-combobox.test.ts +16 -22
  129. package/dist/template/src/client/guard-action-combobox.tsx +133 -239
  130. package/dist/template/src/client/guard-action-options.ts +4 -6
  131. package/dist/template/src/client/i18n/en.json +133 -2
  132. package/dist/template/src/client/i18n/language-switching.test.tsx +12 -3
  133. package/dist/template/src/client/i18n/ru.json +133 -2
  134. package/dist/template/src/client/i18n/uk.json +133 -2
  135. package/dist/template/src/client/logout-button.tsx +3 -3
  136. package/dist/template/src/client/policy-draft.ts +138 -0
  137. package/dist/template/src/client/update-audience-editor.tsx +597 -206
  138. package/dist/template/src/client/update-policy-editor.test.ts +101 -29
  139. package/dist/template/src/client/update-policy-editor.tsx +454 -320
  140. package/dist/template/src/client/use-filtered-results.ts +55 -0
  141. package/dist/template/src/server/controllers/manifest.test.ts +23 -3
  142. package/dist/template/src/server/controllers/manifest.ts +15 -1
  143. package/dist/template/src/server/devices/audience.ts +9 -5
  144. package/dist/template/src/server/lib/admin-audience-activation.test.ts +37 -90
  145. package/dist/template/src/server/lib/admin-release-history.test.ts +138 -0
  146. package/dist/template/src/server/lib/admin-updates.ts +160 -534
  147. package/dist/template/src/server/lib/distribution-history.ts +97 -0
  148. package/dist/template/src/server/lib/supabase.js +24 -0
  149. package/dist/template/src/server/lib/supabase.test.ts +38 -1
  150. package/dist/template/src/server/lib/update-policy.test.ts +20 -0
  151. package/dist/template/src/server/lib/update-policy.ts +23 -10
  152. package/dist/template/src/server/manifest/manifest-repository.ts +11 -3
  153. package/dist/template/src/server/manifest/manifest-values.test.ts +2 -0
  154. package/dist/template/src/server/manifest/manifest-values.ts +21 -13
  155. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +7 -30
  156. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +2 -1
  157. package/dist/template/tsconfig.json +2 -2
  158. package/dist/template-manifest.json +446 -301
  159. package/package.json +1 -1
  160. package/dist/template/scripts/lib/device-identity.test.ts +0 -29
  161. package/dist/template/scripts/lib/ota-client.test.ts +0 -55
  162. package/dist/template/scripts/lib/ota-update-controller.test.ts +0 -232
  163. package/dist/template/scripts/lib/ota-updates-ui.test.tsx +0 -154
  164. package/dist/template/scripts/tests/ota-client-types.mjs +0 -69
  165. package/dist/template/templates/expo-app/ota-client/README.md +0 -73
  166. package/dist/template/templates/expo-app/ota-client/controller.ts +0 -107
  167. package/dist/template/templates/expo-app/ota-client/device-identity.ts +0 -48
  168. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +0 -61
  169. package/dist/template/templates/expo-app/ota-client/index.ts +0 -16
  170. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.ts +0 -97
  171. package/dist/template/templates/expo-app/ota-client/model/update-controller.ts +0 -293
  172. package/dist/template/templates/expo-app/ota-client/runtime.ts +0 -153
  173. package/dist/template/templates/expo-app/ota-client/types.ts +0 -85
  174. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.tsx +0 -25
  175. package/dist/template/templates/expo-app/ota-client/ui/ota-updates.tsx +0 -196
@@ -1,8 +1,17 @@
1
1
  "use client";
2
- import type { TranslationMessage } from "@/src/client/i18n/messages";
3
- import { useI18n } from "@/src/client/i18n/provider";
4
2
 
5
- import { useEffect, useState } from "react";
3
+ import { useEffect, useId, useRef, useState } from "react";
4
+ import {
5
+ AlertTriangle,
6
+ Check,
7
+ Globe2,
8
+ Plus,
9
+ Search,
10
+ Users,
11
+ X,
12
+ } from "lucide-react";
13
+ import { Badge } from "@/components/ui/badge";
14
+ import { Button } from "@/components/ui/button";
6
15
  import {
7
16
  Card,
8
17
  CardContent,
@@ -10,18 +19,28 @@ import {
10
19
  CardHeader,
11
20
  CardTitle,
12
21
  } from "@/components/ui/card";
13
- import { Button } from "@/components/ui/button";
14
22
  import { Checkbox } from "@/components/ui/checkbox";
15
23
  import { Input } from "@/components/ui/input";
24
+ import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
25
+ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
16
26
  import { Textarea } from "@/components/ui/textarea";
17
- import { NativeSelect } from "@/components/ui/native-select";
27
+ import {
28
+ deviceIdPattern,
29
+ installationIdPattern,
30
+ maxIdsPerKind,
31
+ } from "@/lib/audience-ids";
32
+ import {
33
+ BookmarkTarget,
34
+ FavoriteList,
35
+ useFavoriteTargets,
36
+ } from "@/src/client/devices/favorite-targets";
37
+ import { useI18n } from "@/src/client/i18n/provider";
18
38
  import type { Audience } from "@/src/server/devices/audience";
19
39
 
20
40
  type Target = {
21
41
  installation_id: string;
22
42
  device_id: string | null;
23
43
  name: string | null;
24
- platform: string;
25
44
  runtime_version: string;
26
45
  channel: string;
27
46
  };
@@ -30,262 +49,634 @@ async function json(response: Response) {
30
49
  if (!response.ok) throw new Error(value.error || "Ошибка запроса");
31
50
  return value;
32
51
  }
33
- const splitIds = (value: string) => [
34
- ...new Set(value.split(/[\s,]+/).filter(Boolean)),
52
+ const recipients = (a: Audience) => [
53
+ ...a.audience_device_ids,
54
+ ...a.audience_installation_ids,
35
55
  ];
56
+ const sameIds = (a: string[], b: string[]) =>
57
+ a.length === b.length && a.every((id) => b.includes(id));
58
+
36
59
  export default function UpdateAudienceEditor({
37
60
  encodedKey,
38
61
  platform,
62
+ runtime,
63
+ channel,
64
+ active,
65
+ canWrite,
66
+ onSaved,
39
67
  }: {
40
68
  encodedKey: string;
41
69
  platform: string;
70
+ runtime: string;
71
+ channel: string;
72
+ active: boolean;
73
+ canWrite: boolean;
74
+ onSaved: (mode: Audience["audience_mode"]) => void;
42
75
  }) {
43
76
  const { t } = useI18n();
77
+ const field = useId();
78
+ const manualInput = useRef<HTMLTextAreaElement>(null);
44
79
  const [audience, setAudience] = useState<Audience | null>(null);
80
+ const [mode, setMode] = useState<Audience["audience_mode"]>("all");
81
+ const [ids, setIds] = useState<string[]>([]);
82
+ const [source, setSource] = useState("registry");
83
+ const [manual, setManual] = useState("");
84
+ const [attempted, setAttempted] = useState(false);
45
85
  const [devices, setDevices] = useState<Target[]>([]);
46
- const [mode, setMode] = useState("all");
47
- const [deviceIds, setDeviceIds] = useState("");
48
- const [installationIds, setInstallationIds] = useState("");
49
86
  const [search, setSearch] = useState("");
50
- const [busy, setBusy] = useState(false);
51
- const [error, setError] = useState("");
52
- const [success, setSuccess] = useState<TranslationMessage>("");
87
+ const [loadingTargets, setLoadingTargets] = useState(false);
88
+ const [targetsError, setTargetsError] = useState("");
53
89
  const [revision, setRevision] = useState(0);
90
+ const [registryRevision, setRegistryRevision] = useState(0);
91
+ const [saving, setSaving] = useState(false);
92
+ const [error, setError] = useState("");
93
+ const [notice, setNotice] = useState("");
54
94
  const url = `/api/admin/updates/${encodedKey}/audience`;
95
+ const favorites = useFavoriteTargets(
96
+ platform,
97
+ mode === "devices" && !!audience,
98
+ revision,
99
+ );
100
+
101
+ function restore(a: Audience) {
102
+ setMode(a.audience_mode);
103
+ setIds(recipients(a));
104
+ setManual("");
105
+ setAttempted(false);
106
+ setError("");
107
+ setNotice("");
108
+ }
55
109
  useEffect(() => {
56
110
  const abort = new AbortController();
57
- Promise.all([
58
- fetch(url, { signal: abort.signal }).then(json),
59
- fetch(`/api/admin/device-targets?platform=${platform}`, {
60
- signal: abort.signal,
61
- }).then(json),
62
- ])
63
- .then(([a, d]) => {
64
- setAudience(a);
65
- setMode(a.audience_mode);
66
- setDeviceIds(a.audience_device_ids.join("\n"));
67
- setInstallationIds(a.audience_installation_ids.join("\n"));
68
- setDevices(d.devices);
69
- setError("");
111
+ setAudience(null);
112
+ setError("");
113
+ fetch(url, { signal: abort.signal })
114
+ .then(json)
115
+ .then((a: Audience) => {
116
+ if (!abort.signal.aborted) {
117
+ restore(a);
118
+ setAudience(a);
119
+ }
70
120
  })
71
121
  .catch((e) => {
72
122
  if (!abort.signal.aborted) setError(e.message);
73
123
  });
74
124
  return () => abort.abort();
75
- }, [url, platform, revision]);
76
- const selectedDevices = splitIds(deviceIds);
77
- const selectedInstallations = splitIds(installationIds);
78
- const seen = new Set<string>();
79
- const options = devices
80
- .filter((d) => {
81
- const key = d.device_id || d.installation_id;
82
- if (seen.has(key)) return false;
83
- const matches = [
84
- d.name,
85
- key,
86
- d.installation_id,
87
- d.channel,
88
- d.runtime_version,
89
- ]
90
- .join(" ")
91
- .toLowerCase()
92
- .includes(search.toLowerCase());
93
- if (matches) seen.add(key);
94
- return matches;
95
- })
96
- .slice(0, 50);
125
+ }, [url, revision]);
126
+ useEffect(() => {
127
+ if (mode !== "devices") return;
128
+ const abort = new AbortController();
129
+ setLoadingTargets(true);
130
+ setTargetsError("");
131
+ fetch(
132
+ `/api/admin/device-targets?platform=${encodeURIComponent(platform)}`,
133
+ { signal: abort.signal },
134
+ )
135
+ .then(json)
136
+ .then((data) => {
137
+ if (!abort.signal.aborted) setDevices(data.devices);
138
+ })
139
+ .catch((e) => {
140
+ if (!abort.signal.aborted) setTargetsError(e.message);
141
+ })
142
+ .finally(() => {
143
+ if (!abort.signal.aborted) setLoadingTargets(false);
144
+ });
145
+ return () => abort.abort();
146
+ }, [mode, platform, registryRevision]);
147
+
148
+ const selectedDevices = ids.filter((id) => deviceIdPattern.test(id));
149
+ const selectedInstallations = ids.filter((id) =>
150
+ installationIdPattern.test(id),
151
+ );
152
+ const dirty =
153
+ !!audience &&
154
+ (mode !== audience.audience_mode ||
155
+ (mode === "devices" && !sameIds(ids, recipients(audience))));
156
+ const pending = mode === "devices" && !!manual.trim();
157
+ const canSave =
158
+ canWrite &&
159
+ !!audience &&
160
+ dirty &&
161
+ !saving &&
162
+ !pending &&
163
+ (mode === "all" || ids.length > 0);
164
+
165
+ const tokens = manual
166
+ .trim()
167
+ .split(/[\s,]+/)
168
+ .filter(Boolean)
169
+ .map((s) => s.toLowerCase());
170
+ const invalid = tokens.find(
171
+ (s) => !deviceIdPattern.test(s) && !installationIdPattern.test(s),
172
+ );
173
+ const combined = [...new Set([...ids, ...tokens])];
174
+ const overLimit =
175
+ combined.filter((s) => deviceIdPattern.test(s)).length > maxIdsPerKind ||
176
+ combined.filter((s) => installationIdPattern.test(s)).length >
177
+ maxIdsPerKind;
178
+ const limitMessage = t(
179
+ "Up to {0} device IDs and {0} installation IDs per audience.",
180
+ [maxIdsPerKind],
181
+ );
182
+ const manualError = invalid
183
+ ? t("Unrecognized ID “{0}”.", [invalid])
184
+ : overLimit
185
+ ? limitMessage
186
+ : "";
187
+ function select(next: string[]) {
188
+ setIds(next);
189
+ setError("");
190
+ setNotice("");
191
+ }
192
+ function remove(id: string) {
193
+ select(ids.filter((value) => value !== id));
194
+ }
195
+ function add(id: string) {
196
+ if (ids.includes(id)) return;
197
+ const group = deviceIdPattern.test(id)
198
+ ? selectedDevices
199
+ : selectedInstallations;
200
+ if (group.length >= maxIdsPerKind) {
201
+ setError(limitMessage);
202
+ return;
203
+ }
204
+ select([...ids, id]);
205
+ }
206
+ function addManual() {
207
+ setAttempted(true);
208
+ if (manualError || !tokens.length) {
209
+ manualInput.current?.focus();
210
+ return;
211
+ }
212
+ select(combined);
213
+ setNotice(
214
+ combined.length === ids.length
215
+ ? "These recipients are already selected."
216
+ : "Recipients added to the selection.",
217
+ );
218
+ setManual("");
219
+ setAttempted(false);
220
+ }
97
221
  async function save() {
98
- if (!audience) return;
99
- setBusy(true);
222
+ if (!audience || !canSave) return;
223
+ setSaving(true);
100
224
  setError("");
101
- setSuccess("");
225
+ setNotice("");
102
226
  try {
103
- const saved = await fetch(url, {
227
+ const saved: Audience = await fetch(url, {
104
228
  method: "PUT",
105
229
  headers: { "Content-Type": "application/json" },
106
230
  body: JSON.stringify({
231
+ ...audience,
107
232
  audience_mode: mode,
108
233
  audience_device_ids: selectedDevices,
109
234
  audience_installation_ids: selectedInstallations,
110
- audience_version: audience.audience_version,
111
235
  }),
112
236
  }).then(json);
237
+ restore(saved);
113
238
  setAudience(saved);
114
- setSuccess(
115
- "Аудитория сохранена. Для черновика отдельно включите распространение.",
116
- );
239
+ onSaved(saved.audience_mode);
240
+ setNotice("Audience saved.");
117
241
  } catch (e) {
118
242
  setError((e as Error).message);
119
243
  } finally {
120
- setBusy(false);
244
+ setSaving(false);
245
+ }
246
+ }
247
+
248
+ const platformName = platform === "ios" ? "iOS" : "Android";
249
+ const seen = new Set<string>();
250
+ const query = search.trim().toLowerCase();
251
+ const options = devices
252
+ .filter((d) => {
253
+ const id = d.device_id || d.installation_id;
254
+ if (
255
+ seen.has(id) ||
256
+ ![d.name, id, d.installation_id, d.channel, d.runtime_version]
257
+ .join(" ")
258
+ .toLowerCase()
259
+ .includes(query)
260
+ )
261
+ return false;
262
+ seen.add(id);
263
+ return true;
264
+ })
265
+ .slice(0, 50);
266
+ const names = new Map<string, string>();
267
+ for (const d of devices) {
268
+ if (d.name) {
269
+ names.set(d.installation_id, d.name);
270
+ if (d.device_id && !names.has(d.device_id))
271
+ names.set(d.device_id, d.name);
121
272
  }
122
273
  }
274
+ for (const f of favorites.favorites ?? []) names.set(f.target_id, f.name);
123
275
  return (
124
- <Card>
276
+ <Card className="min-w-0 shadow-none">
125
277
  <CardHeader>
126
- <CardTitle>{t("Аудитория обновления")}</CardTitle>
127
- <CardDescription>
128
- {t(
129
- "Сначала ограничьте аудиторию черновика, затем активируйте OTA. После smoke-теста выберите «Все» и сохраните. Runtime, канал и платформа по-прежнему должны совпадать.",
130
- )}
278
+ <CardTitle>{t("Who should receive this update?")}</CardTitle>
279
+ <CardDescription className="break-words">
280
+ {t("{0} · Channel {1} · Runtime {2}", [
281
+ platformName,
282
+ channel,
283
+ runtime,
284
+ ])}
131
285
  </CardDescription>
132
286
  </CardHeader>
133
- <CardContent className="space-y-4">
134
- {error && (
135
- <p role="alert" className="text-sm text-destructive">
136
- {t(error)}
137
- </p>
138
- )}
139
- {success && (
140
- <p role="status" className="text-sm text-muted-foreground">
141
- {t(success)}
142
- </p>
143
- )}
287
+ <CardContent className="space-y-5">
288
+ {!audience && !error && <p role="status">{t("Загрузка...")}</p>}
144
289
  {audience && (
145
290
  <>
146
- <label className="block space-y-2 text-sm">
147
- <span>{t("Кому доступен OTA")}</span>
148
- <NativeSelect
149
- value={mode}
150
- onChange={(e) => {
151
- setMode(e.target.value);
152
- setSuccess("");
153
- }}
154
- disabled={busy}
155
- >
156
- <option value="all">
157
- {t("Все совместимые устройства")}
158
- </option>
159
- <option value="devices">
160
- {t("Только выбранные устройства / установки")}
161
- </option>
162
- </NativeSelect>
163
- </label>
291
+ <RadioGroup
292
+ value={mode}
293
+ disabled={saving || !canWrite}
294
+ aria-label={t("Кому доступен OTA")}
295
+ className="grid gap-2 sm:grid-cols-2"
296
+ onValueChange={(choice) => {
297
+ if (choice === "all" || choice === "devices") {
298
+ setMode(choice);
299
+ setNotice("");
300
+ setError("");
301
+ }
302
+ }}
303
+ >
304
+ {(["all", "devices"] as const).map((choice) => {
305
+ const Icon = choice === "all" ? Globe2 : Users;
306
+ const title =
307
+ choice === "all"
308
+ ? t("Все совместимые устройства")
309
+ : t("Selected recipients");
310
+ return (
311
+ <label
312
+ key={choice}
313
+ className="flex cursor-pointer items-center gap-3 rounded-md border px-3 py-3 text-sm has-[[data-state=checked]]:border-primary has-[[data-state=checked]]:bg-muted/50 has-focus-visible:ring-2 has-focus-visible:ring-ring"
314
+ >
315
+ <Icon
316
+ aria-hidden="true"
317
+ className="size-4 shrink-0 text-muted-foreground"
318
+ />
319
+ <span className="flex-1 font-medium">{title}</span>
320
+ <RadioGroupItem value={choice} aria-label={title} />
321
+ </label>
322
+ );
323
+ })}
324
+ </RadioGroup>
325
+
164
326
  {mode === "devices" && (
165
- <div className="space-y-4">
166
- <Input
167
- aria-label={t("Поиск адресата")}
168
- placeholder={t("Имя, ID, runtime или канал")}
169
- value={search}
170
- onChange={(e) => setSearch(e.target.value)}
171
- />
172
- <p className="text-xs text-muted-foreground">
173
- {t("Последние 500 установок ")}
174
- {platform}
175
- {t(
176
- ", до 50 совпадений. Device ID сохраняет адресата при переустановке; Installation ID относится к одной установке.",
177
- )}
178
- </p>
179
- <div className="max-h-64 overflow-y-auto rounded-md border divide-y">
180
- {options.map((d) => {
181
- const id = d.device_id || d.installation_id;
182
- const selected = d.device_id
183
- ? selectedDevices
184
- : selectedInstallations;
185
- const set = d.device_id
186
- ? setDeviceIds
187
- : setInstallationIds;
188
- const kind = d.device_id
189
- ? t("Устройство")
190
- : t("Установка");
191
- return (
192
- <label
193
- key={id}
194
- className="flex cursor-pointer items-start gap-3 p-3 text-sm"
327
+ <div
328
+ className={
329
+ canWrite
330
+ ? "grid min-w-0 gap-4 xl:grid-cols-[minmax(0,3fr)_minmax(0,2fr)]"
331
+ : ""
332
+ }
333
+ >
334
+ {canWrite && (
335
+ <section className="min-w-0 space-y-3">
336
+ <h3 className="text-sm font-semibold">
337
+ {t("Add recipients")}
338
+ </h3>
339
+ <Tabs
340
+ value={source}
341
+ onValueChange={setSource}
342
+ className="min-w-0 gap-3"
343
+ >
344
+ <TabsList
345
+ aria-label={t("Add recipients")}
346
+ className="max-w-full"
195
347
  >
196
- <Checkbox
197
- aria-label={d.name || `${kind} ${id}`}
198
- className="mt-1"
199
- disabled={busy}
200
- checked={selected.includes(id)}
201
- onCheckedChange={(checked) =>
202
- set(
203
- (checked === true
204
- ? [...selected, id]
205
- : selected.filter((x) => x !== id)
206
- ).join("\n"),
207
- )
348
+ <TabsTrigger value="registry">
349
+ {t("Registry")}
350
+ </TabsTrigger>
351
+ <TabsTrigger value="favorites">
352
+ {t("Favorites")}
353
+ </TabsTrigger>
354
+ <TabsTrigger value="manual">
355
+ {t("Paste IDs")}
356
+ </TabsTrigger>
357
+ </TabsList>
358
+ <TabsContent value="registry" className="space-y-2">
359
+ {devices.length > 0 && (
360
+ <div className="relative">
361
+ <Search
362
+ aria-hidden="true"
363
+ className="pointer-events-none absolute left-3 top-2.5 size-4 text-muted-foreground"
364
+ />
365
+ <Input
366
+ aria-label={t("Поиск адресата")}
367
+ placeholder={t("Имя, ID, runtime или канал")}
368
+ value={search}
369
+ className="pl-9"
370
+ onChange={(e) => setSearch(e.target.value)}
371
+ />
372
+ </div>
373
+ )}
374
+ <div
375
+ aria-busy={loadingTargets}
376
+ className="max-h-64 divide-y overflow-y-auto rounded-md border"
377
+ >
378
+ {loadingTargets ? (
379
+ <p role="status" className="p-3 text-sm">
380
+ {t("Загрузка...")}
381
+ </p>
382
+ ) : targetsError ? (
383
+ <div className="p-3">
384
+ <p
385
+ role="alert"
386
+ className="text-sm text-destructive"
387
+ >
388
+ {t(targetsError)}
389
+ </p>
390
+ <Button
391
+ variant="outline"
392
+ size="sm"
393
+ onClick={() =>
394
+ setRegistryRevision((n) => n + 1)
395
+ }
396
+ >
397
+ {t("Retry")}
398
+ </Button>
399
+ </div>
400
+ ) : options.length ? (
401
+ options.map((d) => {
402
+ const id = d.device_id || d.installation_id;
403
+ const kind = d.device_id
404
+ ? t("Устройство")
405
+ : t("Установка");
406
+ return (
407
+ <div
408
+ key={id}
409
+ className="flex items-center gap-2 p-2.5"
410
+ >
411
+ <label className="flex min-w-0 flex-1 cursor-pointer items-center gap-2.5 text-sm">
412
+ <Checkbox
413
+ aria-label={d.name || id}
414
+ disabled={saving}
415
+ checked={ids.includes(id)}
416
+ onCheckedChange={(checked) =>
417
+ checked === true ? add(id) : remove(id)
418
+ }
419
+ />
420
+ <span className="min-w-0">
421
+ <span
422
+ className="block truncate font-medium"
423
+ title={id}
424
+ >
425
+ {d.name ||
426
+ `${kind} · ${id.slice(0, 12)}…`}
427
+ </span>
428
+ <span className="block truncate text-xs text-muted-foreground">
429
+ {d.channel} ·{" "}
430
+ {t("Runtime {0}", [d.runtime_version])}
431
+ </span>
432
+ </span>
433
+ </label>
434
+ <BookmarkTarget
435
+ platform={platform}
436
+ deviceId={d.device_id}
437
+ installationId={d.installation_id}
438
+ displayName={d.name}
439
+ favorites={favorites.favorites ?? []}
440
+ onChanged={favorites.refresh}
441
+ />
442
+ </div>
443
+ );
444
+ })
445
+ ) : (
446
+ <div className="space-y-2 p-3 text-sm">
447
+ <p>
448
+ {query
449
+ ? t("No recipients match your search.")
450
+ : t(
451
+ "No installations registered for {0} yet.",
452
+ [platformName],
453
+ )}
454
+ </p>
455
+ <Button
456
+ variant="outline"
457
+ size="sm"
458
+ onClick={() => setSource("manual")}
459
+ >
460
+ {t("Paste IDs")}
461
+ </Button>
462
+ </div>
463
+ )}
464
+ </div>
465
+ {options.length > 0 && (
466
+ <p className="text-xs text-muted-foreground">
467
+ {t(
468
+ "Showing up to 50 matches from the latest 500 installations.",
469
+ )}
470
+ </p>
471
+ )}
472
+ </TabsContent>
473
+ <TabsContent value="favorites">
474
+ <FavoriteList
475
+ favorites={favorites.favorites}
476
+ error={favorites.error}
477
+ platform={platform}
478
+ canWrite={canWrite}
479
+ busy={saving}
480
+ selectedDevices={selectedDevices}
481
+ selectedInstallations={selectedInstallations}
482
+ onChanged={favorites.refresh}
483
+ onAdd={(_, id) => add(id)}
484
+ />
485
+ </TabsContent>
486
+ <TabsContent value="manual" className="space-y-2">
487
+ <label htmlFor={field} className="text-sm font-medium">
488
+ {t("Device or installation IDs")}
489
+ </label>
490
+ <Textarea
491
+ id={field}
492
+ ref={manualInput}
493
+ className="min-h-20 font-mono text-sm"
494
+ value={manual}
495
+ disabled={saving}
496
+ aria-invalid={attempted && !!manualError}
497
+ aria-describedby={
498
+ field +
499
+ "-help" +
500
+ (attempted && manualError
501
+ ? " " + field + "-error"
502
+ : "")
208
503
  }
504
+ onChange={(e) => {
505
+ setManual(e.target.value);
506
+ setAttempted(false);
507
+ setNotice("");
508
+ }}
209
509
  />
210
- <span className="min-w-0">
211
- <span className="font-medium">
212
- {d.name || `${kind} ${id.slice(0, 12)}…`}
213
- </span>
214
- <span className="block text-xs text-muted-foreground break-all">
215
- {d.runtime_version} · {d.channel} ·{" "}
216
- {d.installation_id}
217
- </span>
218
- </span>
219
- </label>
220
- );
221
- })}
222
- {options.length === 0 && (
223
- <p className="p-4 text-sm text-muted-foreground">
224
- {t(
225
- "Устройства не найдены. Запустите приложение с обновлённым OTA-клиентом или укажите ID ниже.",
226
- )}
510
+ <p
511
+ id={field + "-help"}
512
+ className="text-xs text-muted-foreground"
513
+ >
514
+ {t(
515
+ "Device ID (64 hex characters) keeps targeting the device after reinstall. Installation ID (UUID) targets one installation. Separate IDs with a comma, space or new line.",
516
+ )}
517
+ </p>
518
+ {attempted && manualError && (
519
+ <p
520
+ role="alert"
521
+ id={field + "-error"}
522
+ className="break-all text-sm text-destructive"
523
+ >
524
+ {manualError}
525
+ </p>
526
+ )}
527
+ <Button
528
+ size="sm"
529
+ variant="outline"
530
+ disabled={saving || !tokens.length}
531
+ onClick={addManual}
532
+ >
533
+ <Plus />
534
+ {t("Add to selection")}
535
+ </Button>
536
+ </TabsContent>
537
+ </Tabs>
538
+ </section>
539
+ )}
540
+ <section
541
+ aria-label={t("Selected recipients")}
542
+ className="min-w-0 self-start rounded-md border bg-muted/25"
543
+ >
544
+ <div className="flex items-center justify-between gap-2 px-3 py-2.5">
545
+ <h3 className="text-sm font-semibold">
546
+ {t("Selected recipients")}
547
+ </h3>
548
+ <Badge variant={ids.length ? "info" : "neutral"}>
549
+ {ids.length}
550
+ </Badge>
551
+ </div>
552
+ {ids.length ? (
553
+ <ul className="max-h-64 divide-y overflow-y-auto border-t">
554
+ {ids.map((id) => {
555
+ const kind = deviceIdPattern.test(id)
556
+ ? t("Устройство")
557
+ : t("Установка");
558
+ return (
559
+ <li
560
+ key={id}
561
+ className="flex items-center gap-2 px-3 py-2"
562
+ >
563
+ <div className="min-w-0 flex-1">
564
+ <p className="break-words text-sm font-medium">
565
+ {names.get(id)
566
+ ? `${names.get(id)} · ${kind}`
567
+ : kind}
568
+ </p>
569
+ <p className="break-all font-mono text-xs text-muted-foreground">
570
+ {id}
571
+ </p>
572
+ </div>
573
+ {canWrite && (
574
+ <Button
575
+ variant="ghost"
576
+ size="icon"
577
+ aria-label={t("Remove {0} from selection", [
578
+ id,
579
+ ])}
580
+ disabled={saving}
581
+ onClick={() => remove(id)}
582
+ >
583
+ <X />
584
+ </Button>
585
+ )}
586
+ </li>
587
+ );
588
+ })}
589
+ </ul>
590
+ ) : (
591
+ <p className="border-t p-3 text-sm text-muted-foreground">
592
+ {t("Add at least one recipient to save this audience.")}
227
593
  </p>
228
594
  )}
229
- </div>
230
- <details>
231
- <summary className="cursor-pointer text-sm">
232
- {t("Выбранные ID и ручное добавление ·")}{" "}
233
- {selectedDevices.length + selectedInstallations.length}
234
- </summary>
235
- <div className="mt-3 grid gap-3 md:grid-cols-2">
236
- <label className="space-y-2 text-sm">
237
- <span>{t("Device ID (по одному в строке)")}</span>
238
- <Textarea
239
- aria-label={t("Device IDs")}
240
- value={deviceIds}
241
- disabled={busy}
242
- onChange={(e) => setDeviceIds(e.target.value)}
243
- />
244
- </label>
245
- <label className="space-y-2 text-sm">
246
- <span>
247
- {t("Installation ID (по одному в строке)")}
248
- </span>
249
- <Textarea
250
- aria-label={t("Installation IDs")}
251
- value={installationIds}
252
- disabled={busy}
253
- onChange={(e) => setInstallationIds(e.target.value)}
254
- />
255
- </label>
256
- </div>
257
- </details>
595
+ </section>
258
596
  </div>
259
597
  )}
260
- <p className="text-xs text-muted-foreground">
261
- {t(
262
- "Ограничение влияет на следующие проверки OTA. Уже скачанное обновление нельзя отозвать изменением аудитории. Force update действует только для её участников.",
598
+
599
+ {active &&
600
+ mode === "all" &&
601
+ audience.audience_mode === "devices" && (
602
+ <p
603
+ role="status"
604
+ className="flex items-start gap-2 rounded-md border border-amber-500/40 bg-amber-500/10 p-3 text-sm"
605
+ >
606
+ <AlertTriangle
607
+ aria-hidden="true"
608
+ className="mt-0.5 size-4 shrink-0"
609
+ />
610
+ {t(
611
+ "Saving makes this the public release for this platform, channel and runtime, replacing any current public release.",
612
+ )}
613
+ </p>
263
614
  )}
615
+ {canWrite && (
616
+ <div className="border-t pt-4 text-sm text-muted-foreground">
617
+ {active
618
+ ? t(
619
+ "Changes apply at the next update check. Previously downloaded updates stay on the device.",
620
+ )
621
+ : t(
622
+ "Saving sets the audience. Activate the release when you are ready to distribute it.",
623
+ )}
624
+ </div>
625
+ )}
626
+ </>
627
+ )}
628
+ {error && (
629
+ <div className="space-y-2">
630
+ <p role="alert" className="text-sm text-destructive">
631
+ {t(error)}
264
632
  </p>
265
- <Button
266
- className="mr-2"
267
- onClick={save}
268
- disabled={
269
- busy ||
270
- (mode === "devices" &&
271
- !selectedDevices.length &&
272
- !selectedInstallations.length)
273
- }
274
- >
275
- {t("Сохранить аудиторию")}
633
+ {(!audience ||
634
+ error ===
635
+ "Audience changed or distribution blocked. Refresh and try again.") && (
636
+ <Button
637
+ variant="outline"
638
+ size="sm"
639
+ disabled={saving}
640
+ onClick={() => setRevision((n) => n + 1)}
641
+ >
642
+ {t("Перечитать")}
643
+ </Button>
644
+ )}
645
+ </div>
646
+ )}
647
+ {audience && canWrite && (
648
+ <div className="flex flex-wrap items-center gap-2">
649
+ <Button disabled={!canSave} onClick={save}>
650
+ {saving ? t("Сохранение…") : t("Сохранить аудиторию")}
276
651
  </Button>
277
- </>
652
+ {(dirty || pending) && (
653
+ <Button
654
+ variant="ghost"
655
+ disabled={saving}
656
+ onClick={() => restore(audience)}
657
+ >
658
+ {t("Discard changes")}
659
+ </Button>
660
+ )}
661
+ {pending && (
662
+ <button
663
+ className="text-left text-xs text-muted-foreground underline"
664
+ onClick={() => {
665
+ setSource("manual");
666
+ requestAnimationFrame(() => manualInput.current?.focus());
667
+ }}
668
+ >
669
+ {t("Add the entered IDs or clear the field before saving.")}
670
+ </button>
671
+ )}
672
+ </div>
673
+ )}
674
+ {notice && (
675
+ <p role="status" className="flex items-center gap-2 text-sm">
676
+ <Check className="size-4 text-emerald-600" />
677
+ {t(notice)}
678
+ </p>
278
679
  )}
279
- <Button
280
- variant="outline"
281
- disabled={busy}
282
- onClick={() => {
283
- setSuccess("");
284
- setRevision((n) => n + 1);
285
- }}
286
- >
287
- {t("Перечитать")}
288
- </Button>
289
680
  </CardContent>
290
681
  </Card>
291
682
  );