create-expo-updates-server 0.3.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 (191) hide show
  1. package/README.md +4 -2
  2. package/dist/template/Dockerfile +4 -6
  3. package/dist/template/README.md +5 -10
  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 +382 -248
  18. package/dist/template/app/updates/devices/page.tsx +187 -145
  19. package/dist/template/app/updates/embedded/page.tsx +80 -34
  20. package/dist/template/app/updates/emergency-redirects/page.tsx +138 -71
  21. package/dist/template/app/updates/layout.tsx +2 -1
  22. package/dist/template/app/updates/page.tsx +385 -274
  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 -95
  26. package/dist/template/components/admin/confirm-action.tsx +123 -0
  27. package/dist/template/components/admin/language-switcher.tsx +27 -16
  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/dialog.tsx +13 -4
  39. package/dist/template/components/ui/empty.tsx +103 -0
  40. package/dist/template/components/ui/field.tsx +248 -0
  41. package/dist/template/components/ui/input-group.tsx +170 -0
  42. package/dist/template/components/ui/input.tsx +1 -1
  43. package/dist/template/components/ui/native-select.tsx +1 -1
  44. package/dist/template/components/ui/radio-group.tsx +41 -0
  45. package/dist/template/components/ui/sheet.tsx +6 -2
  46. package/dist/template/components/ui/sidebar.tsx +62 -17
  47. package/dist/template/components/ui/status-tones.ts +15 -0
  48. package/dist/template/components/ui/table.tsx +1 -1
  49. package/dist/template/components/ui/textarea.tsx +1 -1
  50. package/dist/template/docs/admin-ui.md +22 -24
  51. package/dist/template/docs/device-targeting.md +6 -12
  52. package/dist/template/docs/doctor.md +2 -2
  53. package/dist/template/docs/expo-app-setup.md +9 -27
  54. package/dist/template/docs/getting-started.md +3 -4
  55. package/dist/template/docs/getting-started.ru.md +11 -544
  56. package/dist/template/docs/index.md +1 -1
  57. package/dist/template/docs/installation-telemetry.md +6 -12
  58. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +5 -0
  59. package/dist/template/docs/migrations/2026-11-25-device-favorites.sql +53 -0
  60. package/dist/template/docs/migrations/2026-11-25-download-on-postpone.sql +5 -0
  61. package/dist/template/docs/migrations/2026-11-25-public-distribution-history.sql +283 -0
  62. package/dist/template/docs/migrations/schema.sql +349 -0
  63. package/dist/template/docs/npm-installer.md +3 -3
  64. package/dist/template/docs/operations-guide.md +9 -7
  65. package/dist/template/docs/ota-client-lifecycle.md +30 -68
  66. package/dist/template/docs/ota-emergency-recovery.md +34 -57
  67. package/dist/template/docs/ota-update-policy.md +9 -20
  68. package/dist/template/docs/ru/admin-ui.md +33 -0
  69. package/dist/template/docs/ru/device-targeting.md +59 -0
  70. package/dist/template/docs/ru/doctor.md +98 -0
  71. package/dist/template/docs/ru/expo-app-setup.md +497 -0
  72. package/dist/template/docs/ru/getting-started.md +391 -0
  73. package/dist/template/docs/ru/index.md +50 -0
  74. package/dist/template/docs/ru/installation-telemetry.md +125 -0
  75. package/dist/template/docs/ru/npm-installer.md +71 -0
  76. package/dist/template/docs/ru/operations-guide.md +322 -0
  77. package/dist/template/docs/ru/ota-client-lifecycle.md +162 -0
  78. package/dist/template/docs/ru/ota-emergency-recovery.md +108 -0
  79. package/dist/template/docs/ru/ota-update-policy.md +69 -0
  80. package/dist/template/docs/ru/publishing-npm-installer.md +49 -0
  81. package/dist/template/docs/ru/supabase-setup.md +120 -0
  82. package/dist/template/docs/uk/admin-ui.md +33 -0
  83. package/dist/template/docs/uk/device-targeting.md +59 -0
  84. package/dist/template/docs/uk/doctor.md +98 -0
  85. package/dist/template/docs/uk/expo-app-setup.md +497 -0
  86. package/dist/template/docs/uk/getting-started.md +391 -0
  87. package/dist/template/docs/uk/index.md +50 -0
  88. package/dist/template/docs/uk/installation-telemetry.md +125 -0
  89. package/dist/template/docs/uk/npm-installer.md +71 -0
  90. package/dist/template/docs/uk/operations-guide.md +322 -0
  91. package/dist/template/docs/uk/ota-client-lifecycle.md +162 -0
  92. package/dist/template/docs/uk/ota-emergency-recovery.md +108 -0
  93. package/dist/template/docs/uk/ota-update-policy.md +69 -0
  94. package/dist/template/docs/uk/publishing-npm-installer.md +49 -0
  95. package/dist/template/docs/uk/supabase-setup.md +120 -0
  96. package/dist/template/lib/audience-ids.ts +4 -0
  97. package/dist/template/package-lock.json +46 -0
  98. package/dist/template/package.json +4 -1
  99. package/dist/template/scripts/configure-expo-app.mjs +3 -3
  100. package/dist/template/scripts/create-access-token.mjs +1 -1
  101. package/dist/template/scripts/lib/doctor.mjs +10 -7
  102. package/dist/template/scripts/lib/doctor.test.mjs +7 -5
  103. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +22 -7
  104. package/dist/template/scripts/lib/expo-app-configurator.mjs +25 -22
  105. package/dist/template/scripts/lib/expo-app-configurator.test.ts +37 -2
  106. package/dist/template/scripts/lib/ota-client-configurator.mjs +152 -55
  107. package/dist/template/scripts/lib/ota-client-configurator.test.ts +60 -20
  108. package/dist/template/scripts/lib/ota-client-package.mjs +25 -0
  109. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +90 -32
  110. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +49 -6
  111. package/dist/template/scripts/tests/device-analytics-migrations.mjs +12 -1
  112. package/dist/template/scripts/tests/device-targeting-activation.mjs +2 -3
  113. package/dist/template/scripts/tests/e2e/client-lifecycle.mjs +161 -0
  114. package/dist/template/scripts/tests/e2e/client-lifecycle.scenario.jsx +465 -0
  115. package/dist/template/scripts/tests/e2e/client-vitest.mts +18 -0
  116. package/dist/template/scripts/tests/e2e/console.mjs +86 -0
  117. package/dist/template/scripts/tests/e2e/delivery.mjs +296 -0
  118. package/dist/template/scripts/tests/e2e/device-favorites.mjs +309 -0
  119. package/dist/template/scripts/tests/e2e/installations.mjs +132 -0
  120. package/dist/template/scripts/tests/e2e/policy.mjs +253 -0
  121. package/dist/template/scripts/tests/e2e/registrar-token.mjs +59 -0
  122. package/dist/template/scripts/tests/e2e/release-matrix.mjs +307 -0
  123. package/dist/template/scripts/tests/e2e/releases.mjs +162 -0
  124. package/dist/template/scripts/tests/e2e/run.mjs +224 -0
  125. package/dist/template/scripts/tests/manifest-identity.sql +7 -5
  126. package/dist/template/scripts/tests/public-distribution-history.mjs +102 -0
  127. package/dist/template/src/client/admin-api.ts +45 -10
  128. package/dist/template/src/client/admin-types.ts +35 -1
  129. package/dist/template/src/client/devices/analytics.tsx +169 -219
  130. package/dist/template/src/client/devices/cohorts.tsx +181 -0
  131. package/dist/template/src/client/devices/device-name.tsx +37 -21
  132. package/dist/template/src/client/devices/favorite-targets.tsx +381 -0
  133. package/dist/template/src/client/devices/registry.tsx +265 -233
  134. package/dist/template/src/client/devices/use-devices-report.test.tsx +98 -0
  135. package/dist/template/src/client/devices/use-devices-report.ts +53 -82
  136. package/dist/template/src/client/devices/version-adoption-chart.tsx +48 -38
  137. package/dist/template/src/client/distribution-control.tsx +98 -53
  138. package/dist/template/src/client/distribution-history.tsx +353 -0
  139. package/dist/template/src/client/guard-action-catalog.tsx +183 -0
  140. package/dist/template/src/client/guard-action-combobox.test.ts +16 -22
  141. package/dist/template/src/client/guard-action-combobox.tsx +134 -234
  142. package/dist/template/src/client/guard-action-options.ts +4 -6
  143. package/dist/template/src/client/i18n/en.json +335 -2
  144. package/dist/template/src/client/i18n/language-switching.test.tsx +233 -0
  145. package/dist/template/src/client/i18n/messages.test.ts +102 -19
  146. package/dist/template/src/client/i18n/messages.ts +31 -16
  147. package/dist/template/src/client/i18n/provider.tsx +6 -3
  148. package/dist/template/src/client/i18n/ru.json +619 -0
  149. package/dist/template/src/client/i18n/uk.json +619 -0
  150. package/dist/template/src/client/i18n/use-formatters.ts +5 -0
  151. package/dist/template/src/client/logout-button.tsx +3 -3
  152. package/dist/template/src/client/policy-draft.ts +138 -0
  153. package/dist/template/src/client/update-audience-editor.tsx +597 -199
  154. package/dist/template/src/client/update-policy-editor.test.ts +101 -29
  155. package/dist/template/src/client/update-policy-editor.tsx +466 -307
  156. package/dist/template/src/client/use-filtered-results.ts +55 -0
  157. package/dist/template/src/server/controllers/manifest.test.ts +23 -3
  158. package/dist/template/src/server/controllers/manifest.ts +15 -1
  159. package/dist/template/src/server/devices/audience.ts +9 -5
  160. package/dist/template/src/server/lib/admin-audience-activation.test.ts +37 -90
  161. package/dist/template/src/server/lib/admin-release-history.test.ts +138 -0
  162. package/dist/template/src/server/lib/admin-updates.ts +160 -534
  163. package/dist/template/src/server/lib/distribution-history.ts +97 -0
  164. package/dist/template/src/server/lib/supabase.js +24 -0
  165. package/dist/template/src/server/lib/supabase.test.ts +38 -1
  166. package/dist/template/src/server/lib/update-policy.test.ts +20 -0
  167. package/dist/template/src/server/lib/update-policy.ts +23 -10
  168. package/dist/template/src/server/manifest/manifest-repository.ts +11 -3
  169. package/dist/template/src/server/manifest/manifest-values.test.ts +2 -0
  170. package/dist/template/src/server/manifest/manifest-values.ts +21 -13
  171. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +7 -30
  172. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +2 -1
  173. package/dist/template/tsconfig.json +2 -2
  174. package/dist/template-manifest.json +571 -271
  175. package/package.json +1 -1
  176. package/dist/template/scripts/lib/device-identity.test.ts +0 -29
  177. package/dist/template/scripts/lib/ota-client.test.ts +0 -55
  178. package/dist/template/scripts/lib/ota-update-controller.test.ts +0 -232
  179. package/dist/template/scripts/lib/ota-updates-ui.test.tsx +0 -154
  180. package/dist/template/scripts/tests/ota-client-types.mjs +0 -69
  181. package/dist/template/templates/expo-app/ota-client/README.md +0 -73
  182. package/dist/template/templates/expo-app/ota-client/controller.ts +0 -107
  183. package/dist/template/templates/expo-app/ota-client/device-identity.ts +0 -48
  184. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +0 -61
  185. package/dist/template/templates/expo-app/ota-client/index.ts +0 -16
  186. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.ts +0 -97
  187. package/dist/template/templates/expo-app/ota-client/model/update-controller.ts +0 -293
  188. package/dist/template/templates/expo-app/ota-client/runtime.ts +0 -153
  189. package/dist/template/templates/expo-app/ota-client/types.ts +0 -85
  190. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.tsx +0 -25
  191. package/dist/template/templates/expo-app/ota-client/ui/ota-updates.tsx +0 -196
@@ -1,7 +1,17 @@
1
1
  "use client";
2
- import { useI18n } from "@/src/client/i18n/provider";
3
2
 
4
- 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";
5
15
  import {
6
16
  Card,
7
17
  CardContent,
@@ -9,18 +19,28 @@ import {
9
19
  CardHeader,
10
20
  CardTitle,
11
21
  } from "@/components/ui/card";
12
- import { Button } from "@/components/ui/button";
13
22
  import { Checkbox } from "@/components/ui/checkbox";
14
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";
15
26
  import { Textarea } from "@/components/ui/textarea";
16
- 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";
17
38
  import type { Audience } from "@/src/server/devices/audience";
18
39
 
19
40
  type Target = {
20
41
  installation_id: string;
21
42
  device_id: string | null;
22
43
  name: string | null;
23
- platform: string;
24
44
  runtime_version: string;
25
45
  channel: string;
26
46
  };
@@ -29,256 +49,634 @@ async function json(response: Response) {
29
49
  if (!response.ok) throw new Error(value.error || "Ошибка запроса");
30
50
  return value;
31
51
  }
32
- const splitIds = (value: string) => [
33
- ...new Set(value.split(/[\s,]+/).filter(Boolean)),
52
+ const recipients = (a: Audience) => [
53
+ ...a.audience_device_ids,
54
+ ...a.audience_installation_ids,
34
55
  ];
56
+ const sameIds = (a: string[], b: string[]) =>
57
+ a.length === b.length && a.every((id) => b.includes(id));
58
+
35
59
  export default function UpdateAudienceEditor({
36
60
  encodedKey,
37
61
  platform,
62
+ runtime,
63
+ channel,
64
+ active,
65
+ canWrite,
66
+ onSaved,
38
67
  }: {
39
68
  encodedKey: string;
40
69
  platform: string;
70
+ runtime: string;
71
+ channel: string;
72
+ active: boolean;
73
+ canWrite: boolean;
74
+ onSaved: (mode: Audience["audience_mode"]) => void;
41
75
  }) {
42
76
  const { t } = useI18n();
77
+ const field = useId();
78
+ const manualInput = useRef<HTMLTextAreaElement>(null);
43
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);
44
85
  const [devices, setDevices] = useState<Target[]>([]);
45
- const [mode, setMode] = useState("all");
46
- const [deviceIds, setDeviceIds] = useState("");
47
- const [installationIds, setInstallationIds] = useState("");
48
86
  const [search, setSearch] = useState("");
49
- const [busy, setBusy] = useState(false);
50
- const [error, setError] = useState("");
51
- const [success, setSuccess] = useState("");
87
+ const [loadingTargets, setLoadingTargets] = useState(false);
88
+ const [targetsError, setTargetsError] = useState("");
52
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("");
53
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
+ }
54
109
  useEffect(() => {
55
110
  const abort = new AbortController();
56
- Promise.all([
57
- fetch(url, { signal: abort.signal }).then(json),
58
- fetch(`/api/admin/device-targets?platform=${platform}`, {
59
- signal: abort.signal,
60
- }).then(json),
61
- ])
62
- .then(([a, d]) => {
63
- setAudience(a);
64
- setMode(a.audience_mode);
65
- setDeviceIds(a.audience_device_ids.join("\n"));
66
- setInstallationIds(a.audience_installation_ids.join("\n"));
67
- setDevices(d.devices);
68
- 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
+ }
69
120
  })
70
121
  .catch((e) => {
71
122
  if (!abort.signal.aborted) setError(e.message);
72
123
  });
73
124
  return () => abort.abort();
74
- }, [url, platform, revision]);
75
- const selectedDevices = splitIds(deviceIds);
76
- const selectedInstallations = splitIds(installationIds);
77
- const seen = new Set<string>();
78
- const options = devices
79
- .filter((d) => {
80
- const key = d.device_id || d.installation_id;
81
- if (seen.has(key)) return false;
82
- const matches = [
83
- d.name,
84
- key,
85
- d.installation_id,
86
- d.channel,
87
- d.runtime_version,
88
- ]
89
- .join(" ")
90
- .toLowerCase()
91
- .includes(search.toLowerCase());
92
- if (matches) seen.add(key);
93
- return matches;
94
- })
95
- .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
+ }
96
221
  async function save() {
97
- if (!audience) return;
98
- setBusy(true);
222
+ if (!audience || !canSave) return;
223
+ setSaving(true);
99
224
  setError("");
100
- setSuccess("");
225
+ setNotice("");
101
226
  try {
102
- const saved = await fetch(url, {
227
+ const saved: Audience = await fetch(url, {
103
228
  method: "PUT",
104
229
  headers: { "Content-Type": "application/json" },
105
230
  body: JSON.stringify({
231
+ ...audience,
106
232
  audience_mode: mode,
107
233
  audience_device_ids: selectedDevices,
108
234
  audience_installation_ids: selectedInstallations,
109
- audience_version: audience.audience_version,
110
235
  }),
111
236
  }).then(json);
237
+ restore(saved);
112
238
  setAudience(saved);
113
- setSuccess(
114
- t(
115
- "Аудитория сохранена. Для черновика отдельно включите распространение.",
116
- ),
117
- );
239
+ onSaved(saved.audience_mode);
240
+ setNotice("Audience saved.");
118
241
  } catch (e) {
119
242
  setError((e as Error).message);
120
243
  } finally {
121
- 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);
122
272
  }
123
273
  }
274
+ for (const f of favorites.favorites ?? []) names.set(f.target_id, f.name);
124
275
  return (
125
- <Card>
276
+ <Card className="min-w-0 shadow-none">
126
277
  <CardHeader>
127
- <CardTitle>{t("Аудитория обновления")}</CardTitle>
128
- <CardDescription>
129
- {t(
130
- "Сначала ограничьте аудиторию черновика, затем активируйте OTA. После smoke-теста выберите «Все» и сохраните. Runtime, канал и платформа по-прежнему должны совпадать.",
131
- )}
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
+ ])}
132
285
  </CardDescription>
133
286
  </CardHeader>
134
- <CardContent className="space-y-4">
135
- {error && (
136
- <p role="alert" className="text-sm text-destructive">
137
- {t(error)}
138
- </p>
139
- )}
140
- {success && (
141
- <p role="status" className="text-sm text-muted-foreground">
142
- {success}
143
- </p>
144
- )}
287
+ <CardContent className="space-y-5">
288
+ {!audience && !error && <p role="status">{t("Загрузка...")}</p>}
145
289
  {audience && (
146
290
  <>
147
- <label className="block space-y-2 text-sm">
148
- <span>{t("Кому доступен OTA")}</span>
149
- <NativeSelect
150
- value={mode}
151
- onChange={(e) => {
152
- setMode(e.target.value);
153
- setSuccess("");
154
- }}
155
- disabled={busy}
156
- >
157
- <option value="all">{t("Все совместимые устройства")}</option>
158
- <option value="devices">
159
- {t("Только выбранные устройства / установки")}
160
- </option>
161
- </NativeSelect>
162
- </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
+
163
326
  {mode === "devices" && (
164
- <div className="space-y-4">
165
- <Input
166
- aria-label={t("Поиск адресата")}
167
- placeholder={t("Имя, ID, runtime или канал")}
168
- value={search}
169
- onChange={(e) => setSearch(e.target.value)}
170
- />
171
- <p className="text-xs text-muted-foreground">
172
- {t("Последние 500 установок ")}
173
- {platform}
174
- {t(
175
- ", до 50 совпадений. Device ID сохраняет адресата при переустановке; Installation ID относится к одной установке.",
176
- )}
177
- </p>
178
- <div className="max-h-64 overflow-y-auto rounded-md border divide-y">
179
- {options.map((d) => {
180
- const id = d.device_id || d.installation_id;
181
- const selected = d.device_id
182
- ? selectedDevices
183
- : selectedInstallations;
184
- const set = d.device_id ? setDeviceIds : setInstallationIds;
185
- const kind = d.device_id ? t("Устройство") : t("Установка");
186
- return (
187
- <label
188
- key={id}
189
- 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"
190
347
  >
191
- <Checkbox
192
- aria-label={d.name || `${kind} ${id}`}
193
- className="mt-1"
194
- disabled={busy}
195
- checked={selected.includes(id)}
196
- onCheckedChange={(checked) =>
197
- set(
198
- (checked === true
199
- ? [...selected, id]
200
- : selected.filter((x) => x !== id)
201
- ).join("\n"),
202
- )
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
+ : "")
203
503
  }
504
+ onChange={(e) => {
505
+ setManual(e.target.value);
506
+ setAttempted(false);
507
+ setNotice("");
508
+ }}
204
509
  />
205
- <span className="min-w-0">
206
- <span className="font-medium">
207
- {d.name || `${kind} ${id.slice(0, 12)}…`}
208
- </span>
209
- <span className="block text-xs text-muted-foreground break-all">
210
- {d.runtime_version} · {d.channel} ·{" "}
211
- {d.installation_id}
212
- </span>
213
- </span>
214
- </label>
215
- );
216
- })}
217
- {options.length === 0 && (
218
- <p className="p-4 text-sm text-muted-foreground">
219
- {t(
220
- "Устройства не найдены. Запустите приложение с обновлённым OTA-клиентом или укажите ID ниже.",
221
- )}
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.")}
222
593
  </p>
223
594
  )}
224
- </div>
225
- <details>
226
- <summary className="cursor-pointer text-sm">
227
- {t("Выбранные ID и ручное добавление ·")}{" "}
228
- {selectedDevices.length + selectedInstallations.length}
229
- </summary>
230
- <div className="mt-3 grid gap-3 md:grid-cols-2">
231
- <label className="space-y-2 text-sm">
232
- <span>{t("Device ID (по одному в строке)")}</span>
233
- <Textarea
234
- aria-label="Device IDs"
235
- value={deviceIds}
236
- disabled={busy}
237
- onChange={(e) => setDeviceIds(e.target.value)}
238
- />
239
- </label>
240
- <label className="space-y-2 text-sm">
241
- <span>{t("Installation ID (по одному в строке)")}</span>
242
- <Textarea
243
- aria-label="Installation IDs"
244
- value={installationIds}
245
- disabled={busy}
246
- onChange={(e) => setInstallationIds(e.target.value)}
247
- />
248
- </label>
249
- </div>
250
- </details>
595
+ </section>
251
596
  </div>
252
597
  )}
253
- <p className="text-xs text-muted-foreground">
254
- {t(
255
- "Ограничение влияет на следующие проверки 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>
256
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)}
257
632
  </p>
258
- <Button
259
- className="mr-2"
260
- onClick={save}
261
- disabled={
262
- busy ||
263
- (mode === "devices" &&
264
- !selectedDevices.length &&
265
- !selectedInstallations.length)
266
- }
267
- >
268
- {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("Сохранить аудиторию")}
269
651
  </Button>
270
- </>
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>
271
679
  )}
272
- <Button
273
- variant="outline"
274
- disabled={busy}
275
- onClick={() => {
276
- setSuccess("");
277
- setRevision((n) => n + 1);
278
- }}
279
- >
280
- {t("Перечитать")}
281
- </Button>
282
680
  </CardContent>
283
681
  </Card>
284
682
  );