create-expo-updates-server 0.4.0 → 0.6.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 (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 +303 -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 +25 -3
  142. package/dist/template/src/server/controllers/manifest.ts +1 -2
  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 +2 -2
  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 +445 -300
  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,62 +1,138 @@
1
1
  "use client";
2
+ import { AdminRoleContext } from "@/components/admin/admin-role";
3
+ import {
4
+ Collapsible,
5
+ CollapsibleTrigger,
6
+ CollapsibleContent,
7
+ } from "@/components/ui/collapsible";
8
+ import {
9
+ ChevronDown,
10
+ Download,
11
+ ShieldCheck,
12
+ Zap,
13
+ MousePointer2,
14
+ Store,
15
+ Braces,
16
+ LockKeyhole,
17
+ } from "lucide-react";
18
+ import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
19
+ import { Field, FieldLabel, FieldDescription } from "@/components/ui/field";
20
+ import { Label } from "@/components/ui/label";
21
+ import { Badge } from "@/components/ui/badge";
22
+ import { Alert, AlertDescription } from "@/components/ui/alert";
23
+ import {
24
+ Select,
25
+ SelectTrigger,
26
+ SelectValue,
27
+ SelectContent,
28
+ SelectItem,
29
+ } from "@/components/ui/select";
30
+ import { Skeleton } from "@/components/ui/skeleton";
31
+ import {
32
+ behaviors,
33
+ chooseBehavior,
34
+ draftFromPolicy,
35
+ isDeliveryProfile,
36
+ policyFromDraft,
37
+ setStoreLink,
38
+ storePayloadObject,
39
+ validStoreLink,
40
+ type PolicyDraft,
41
+ type PolicyBehavior,
42
+ } from "./policy-draft";
43
+ import GuardActionCatalog from "./guard-action-catalog";
2
44
  import type { TranslationMessage } from "@/src/client/i18n/messages";
3
45
  import { useI18n } from "@/src/client/i18n/provider";
4
46
 
5
47
  import { Textarea } from "@/components/ui/textarea";
6
48
 
7
- import { NativeSelect } from "@/components/ui/native-select";
8
-
9
49
  import { Button } from "@/components/ui/button";
10
50
  import { Card } from "@/components/ui/card";
11
51
  import { Input as TextInput } from "@/components/ui/input";
12
52
 
13
53
  import {
14
54
  createGuardAction,
15
- deleteGuardAction,
16
55
  getGuardActions,
17
56
  getUpdatePolicy,
18
57
  putUpdatePolicy,
19
58
  } from "@/src/client/admin-api";
20
- import {
21
- GuardActionItem,
22
- UpdatePolicy,
23
- UpdatePolicyGuard,
24
- } from "@/src/client/admin-types";
25
- import GuardActionCombobox from "@/src/client/guard-action-combobox";
59
+ import { GuardActionItem, UpdatePolicy } from "@/src/client/admin-types";
60
+ import GuardActionPicker from "@/src/client/guard-action-combobox";
26
61
  import {
27
62
  beginCatalogAttempt,
28
- GuardActionOption,
29
63
  isCurrentCatalogAttempt,
30
64
  mergeGuardActionOptions,
31
- normalizeCreatableGuardAction,
32
65
  } from "@/src/client/guard-action-options";
33
- import { FormEvent, useEffect, useRef, useState } from "react";
66
+ import { useContext, useEffect, useRef, useState } from "react";
34
67
 
35
- function payloadText(guard: UpdatePolicyGuard | null): string {
36
- return guard?.payload === undefined
37
- ? ""
38
- : JSON.stringify(guard.payload, null, 2);
39
- }
68
+ const profiles = {
69
+ manual: {
70
+ label: "Optional update",
71
+ description:
72
+ "Users can download the update, restart to apply it, or continue with their current version.",
73
+ icon: MousePointer2,
74
+ },
75
+ "manual-postpone": {
76
+ label: "Download on postpone",
77
+ description:
78
+ "Offer the update first. If the user postpones it, download silently and apply it on the next full launch.",
79
+ icon: Download,
80
+ },
81
+ background: {
82
+ label: "Background update",
83
+ description:
84
+ "The app downloads the update silently and applies it on the next launch.",
85
+ icon: Download,
86
+ },
87
+ required: {
88
+ label: "Required OTA update",
89
+ description:
90
+ "The app blocks access and downloads this update. Users restart to continue.",
91
+ icon: ShieldCheck,
92
+ },
93
+ immediate: {
94
+ label: "Immediate required update",
95
+ description:
96
+ "The app silently downloads this update and restarts as soon as it is active and ready. Users can retry if the update fails.",
97
+ icon: Zap,
98
+ },
99
+ store: {
100
+ label: "Require a store update",
101
+ description:
102
+ "The app blocks access and opens the configured store link when the user chooses to upgrade.",
103
+ icon: Store,
104
+ },
105
+ custom: {
106
+ label: "Custom guard",
107
+ description:
108
+ "The app runs the selected action. Access stays blocked unless the app’s handler allows it.",
109
+ icon: Braces,
110
+ },
111
+ };
40
112
 
41
113
  export default function UpdatePolicyEditor({
42
114
  encodedKey,
115
+ platform,
43
116
  onSaved,
44
117
  }: {
45
118
  encodedKey: string;
119
+ platform?: string;
46
120
  onSaved: () => Promise<void>;
47
121
  }) {
122
+ const isViewer = useContext(AdminRoleContext) === "viewer";
48
123
  const { t, formatLocale } = useI18n();
49
124
  const [policy, setPolicy] = useState<UpdatePolicy | null>(null);
50
- const [payload, setPayload] = useState("");
125
+ const [draftState, setDraft] = useState<PolicyDraft | null>(null);
126
+ const previousDrafts = useRef<Partial<Record<PolicyBehavior, PolicyDraft>>>(
127
+ {},
128
+ );
129
+ const [loadSequence, setLoadSequence] = useState(0);
51
130
  const [error, setError] = useState("");
52
131
  const [message, setMessage] = useState<TranslationMessage>("");
53
132
  const [saving, setSaving] = useState(false);
54
133
  const [guardActions, setGuardActions] = useState<GuardActionItem[]>([]);
55
134
  const [canWriteGuardActions, setCanWriteGuardActions] = useState(false);
56
135
  const [catalogError, setCatalogError] = useState("");
57
- const [catalogCreateValue, setCatalogCreateValue] = useState("");
58
- const [catalogCreateError, setCatalogCreateError] = useState("");
59
- const [catalogCreatePending, setCatalogCreatePending] = useState(false);
60
136
  const [actionError, setActionError] = useState("");
61
137
  const actionAttempt = useRef({ current: 0 });
62
138
 
@@ -74,36 +150,79 @@ export default function UpdatePolicyEditor({
74
150
  };
75
151
 
76
152
  useEffect(() => {
153
+ let active = true;
77
154
  void getUpdatePolicy(encodedKey)
78
155
  .then((loaded) => {
156
+ if (!active) return;
79
157
  beginCatalogAttempt(actionAttempt.current);
80
158
  setActionError("");
81
159
  setPolicy(loaded);
82
- setPayload(payloadText(loaded.guard));
160
+ previousDrafts.current = {};
161
+ setDraft(draftFromPolicy(loaded));
162
+ setError("");
83
163
  })
84
- .catch((loadError: unknown) =>
85
- setError((loadError as Error).message || "Failed to load policy"),
164
+ .catch(
165
+ (loadError: unknown) =>
166
+ active &&
167
+ setError((loadError as Error).message || "Failed to load policy"),
86
168
  );
87
- }, [encodedKey]);
169
+ return () => {
170
+ active = false;
171
+ };
172
+ }, [encodedKey, loadSequence]);
88
173
 
89
174
  useEffect(() => {
90
175
  void loadGuardActions();
91
176
  }, []);
92
177
 
93
- if (!policy) {
178
+ if (!policy || !draftState) {
94
179
  return (
95
- <Card className="rounded-xl border border-border bg-card p-5 text-sm shadow-none">
96
- {t(error || "Loading update policy…")}
180
+ <Card className="gap-3 p-5 shadow-none">
181
+ {error ? (
182
+ <>
183
+ <p role="alert">{t(error)}</p>
184
+ <Button
185
+ className="w-fit"
186
+ variant="outline"
187
+ size="sm"
188
+ onClick={() => setLoadSequence((n) => n + 1)}
189
+ >
190
+ {t("Retry")}
191
+ </Button>
192
+ </>
193
+ ) : (
194
+ <>
195
+ <span role="status" className="text-sm text-muted-foreground">
196
+ {t("Loading update policy…")}
197
+ </span>
198
+ <Skeleton className="h-16 w-full" />
199
+ </>
200
+ )}
97
201
  </Card>
98
202
  );
99
203
  }
100
-
101
- const readOnly = !policy.editable;
204
+ const draft = draftState;
205
+ const readOnly = !policy.editable || isViewer;
102
206
  const editingDisabled = readOnly || saving;
103
- const guardActionOptions = mergeGuardActionOptions(
104
- guardActions,
105
- policy.guard ? [policy.guard.action] : [],
106
- );
207
+ const catalogWritable = canWriteGuardActions && !isViewer;
208
+ const dirty =
209
+ JSON.stringify(draft) !== JSON.stringify(draftFromPolicy(policy));
210
+ const guarded = !isDeliveryProfile(draft.behavior);
211
+ const storeFields = storePayloadObject(draft.payload);
212
+ const profile = profiles[draft.behavior];
213
+ const guardActionOptions = mergeGuardActionOptions(guardActions, [
214
+ draft.action,
215
+ policy.guard?.action || "",
216
+ ]);
217
+ const edit = (next: PolicyDraft) => {
218
+ if (next.action !== draft.action || next.behavior !== draft.behavior) {
219
+ beginCatalogAttempt(actionAttempt.current);
220
+ setActionError("");
221
+ }
222
+ setMessage("");
223
+ setError("");
224
+ setDraft(next);
225
+ };
107
226
 
108
227
  const addGuardAction = (item: GuardActionItem) => {
109
228
  setGuardActions((current) => {
@@ -116,16 +235,6 @@ export default function UpdatePolicyEditor({
116
235
  });
117
236
  };
118
237
 
119
- const changeGuardAction = (action: string) => {
120
- beginCatalogAttempt(actionAttempt.current);
121
- setActionError("");
122
- setPolicy((current) =>
123
- current?.guard
124
- ? { ...current, guard: { ...current.guard, action } }
125
- : current,
126
- );
127
- };
128
-
129
238
  const persistSelectedAction = async (action: string) => {
130
239
  const attempt = beginCatalogAttempt(actionAttempt.current);
131
240
  try {
@@ -143,82 +252,30 @@ export default function UpdatePolicyEditor({
143
252
  }
144
253
  };
145
254
 
146
- const createCatalogOnlyAction = async (event: FormEvent) => {
147
- event.preventDefault();
148
- let action: string;
149
- try {
150
- action = normalizeCreatableGuardAction(catalogCreateValue);
151
- } catch (validationError: unknown) {
152
- setCatalogCreateError((validationError as Error).message);
153
- return;
154
- }
155
-
156
- setCatalogCreatePending(true);
157
- setCatalogCreateError("");
158
- try {
159
- addGuardAction(await createGuardAction(action));
160
- setCatalogCreateValue("");
161
- } catch (createError: unknown) {
162
- setCatalogCreateError(
163
- (createError as Error).message ||
164
- "Failed to add action to the catalog.",
165
- );
166
- } finally {
167
- setCatalogCreatePending(false);
168
- }
169
- };
170
-
171
- const removeGuardAction = async (option: GuardActionOption) => {
172
- if (
173
- !option.id ||
174
- !window.confirm(
175
- t(
176
- 'Delete "{0}" from the action catalog? Existing policies are unaffected.',
177
- [option.actionKey],
178
- ),
179
- )
180
- ) {
181
- return;
182
- }
183
- try {
184
- await deleteGuardAction(option.id);
185
- setGuardActions((current) =>
186
- current.filter((item) => item.id !== option.id),
187
- );
188
- setCatalogError("");
189
- } catch (deleteError: unknown) {
190
- setCatalogError(
191
- (deleteError as Error).message ||
192
- "Failed to delete action from catalog",
193
- );
194
- }
195
- };
196
-
197
255
  const save = async () => {
198
256
  setError("");
199
257
  setMessage("");
200
258
  setSaving(true);
201
259
  try {
202
- let guard: UpdatePolicyGuard | null = null;
203
- if (policy.guard) {
204
- if (!policy.guard.action) {
205
- throw new Error("Select or create a Guard action.");
206
- }
207
- const trimmedPayload = payload.trim();
208
- guard = {
209
- action: policy.guard.action,
210
- ...(trimmedPayload
211
- ? { payload: JSON.parse(trimmedPayload) }
212
- : {}),
213
- };
260
+ const input = policyFromDraft(draft);
261
+ const original = draftFromPolicy(policy);
262
+ if (
263
+ draft.behavior === "store" &&
264
+ (original.behavior !== "store" || draft.payload !== original.payload) &&
265
+ !validStoreLink(draft.payload, platform)
266
+ ) {
267
+ throw new Error(
268
+ "Enter a valid store link for this platform or a default store link.",
269
+ );
214
270
  }
215
271
  const saved = await putUpdatePolicy(
216
272
  encodedKey,
217
- { delivery: policy.delivery, guard },
273
+ input,
218
274
  policy.policyVersion,
219
275
  );
276
+ previousDrafts.current = {};
220
277
  setPolicy(saved);
221
- setPayload(payloadText(saved.guard));
278
+ setDraft(draftFromPolicy(saved));
222
279
  setMessage({
223
280
  key: "Policy saved as version {0}.",
224
281
  values: [saved.policyVersion],
@@ -236,238 +293,315 @@ export default function UpdatePolicyEditor({
236
293
  };
237
294
 
238
295
  return (
239
- <Card className="gap-4 p-5 shadow-none">
240
- <div className="flex flex-wrap items-center justify-between gap-3">
296
+ <Card className="gap-0 overflow-hidden py-0 shadow-none">
297
+ <div className="flex flex-wrap items-start justify-between gap-3 border-b px-5 py-4">
241
298
  <div>
242
299
  <h2 className="font-semibold">{t("Update policy")}</h2>
243
- <p className="text-xs text-muted-foreground">
244
- {t("Version {0}", [policy.policyVersion])}
245
- {policy.publishedAt
246
- ? t(" · published {0}", [
247
- new Date(policy.publishedAt).toLocaleString(formatLocale),
248
- ])
249
- : t(" · draft")}
300
+ <p className="mt-1 text-sm text-muted-foreground">
301
+ {t("Choose how this release reaches users.")}
250
302
  </p>
251
303
  </div>
252
- <label className="text-sm">
253
- {t("Delivery")}{" "}
254
- <NativeSelect
255
- value={policy.delivery}
256
- disabled={editingDisabled}
257
- onChange={(event) =>
258
- setPolicy({
259
- ...policy,
260
- delivery: event.target.value as UpdatePolicy["delivery"],
261
- })
262
- }
263
- className="rounded-md border border-input bg-background px-2 py-2 "
264
- >
265
- <option value="manual">{t("Manual")}</option>
266
- <option value="background">{t("Background")}</option>
267
- </NativeSelect>
268
- </label>
304
+ <Badge
305
+ variant={policy.publishedAt ? "info" : "warning"}
306
+ className="whitespace-normal font-normal"
307
+ >
308
+ {t("Version {0}", [policy.policyVersion])}
309
+ {policy.publishedAt
310
+ ? t(" · published {0}", [
311
+ new Date(policy.publishedAt).toLocaleString(formatLocale),
312
+ ])
313
+ : t(" · draft")}
314
+ </Badge>
269
315
  </div>
270
-
271
- {readOnly ? (
272
- <div className="rounded-md border border-border bg-muted/30 p-3 text-sm ">
316
+ {readOnly && (
317
+ <div className="flex items-start gap-2 border-b bg-muted/40 px-5 py-3 text-sm text-muted-foreground">
318
+ <LockKeyhole className="mt-0.5 size-4 shrink-0" />
273
319
  {t(
274
- "Published policies are read-only. Publish a new update ID to change policy.",
320
+ !policy.editable
321
+ ? "Published policies are read-only. Publish a new update ID to change policy."
322
+ : "Your role has read-only access to this policy.",
275
323
  )}
276
324
  </div>
277
- ) : null}
278
- {policy.guard && policy.delivery === "manual" ? (
279
- <div className="rounded-md border border-blue-300 bg-blue-50 p-3 text-sm text-blue-900 dark:border-blue-800 dark:bg-blue-950/30 dark:text-blue-200">
280
- <strong>{t("Note:")}</strong>{" "}
281
- {t(
282
- "a Guard is usually paired with Background delivery so the candidate can fetch silently. Activation is still allowed.",
325
+ )}
326
+ <div className="grid gap-6 p-5 md:grid-cols-[minmax(13rem,0.8fr)_minmax(0,1.4fr)]">
327
+ <section className="min-w-0">
328
+ <h3 id="policy-behavior" className="mb-3 text-sm font-medium">
329
+ {t("Update behavior")}
330
+ </h3>
331
+ <RadioGroup
332
+ value={draft.behavior}
333
+ disabled={editingDisabled}
334
+ aria-labelledby="policy-behavior"
335
+ className="gap-1.5"
336
+ onValueChange={(v) => {
337
+ const mode = v as PolicyBehavior;
338
+ previousDrafts.current[draft.behavior] = draft;
339
+ edit(previousDrafts.current[mode] ?? chooseBehavior(draft, mode));
340
+ }}
341
+ >
342
+ {behaviors.map((mode) => {
343
+ const Icon = profiles[mode].icon;
344
+ return (
345
+ <Label
346
+ key={mode}
347
+ htmlFor={`behavior-${mode}`}
348
+ className="flex min-h-10 cursor-pointer gap-2.5 rounded-md border px-3 py-2.5 text-sm font-normal has-[[data-state=checked]]:border-primary has-[[data-state=checked]]:bg-accent has-[[data-state=checked]]:font-medium has-[:disabled]:cursor-default"
349
+ >
350
+ <Icon
351
+ aria-hidden
352
+ className="size-4 shrink-0 text-muted-foreground"
353
+ />
354
+ <span className="flex-1">{t(profiles[mode].label)}</span>
355
+ <RadioGroupItem id={`behavior-${mode}`} value={mode} />
356
+ </Label>
357
+ );
358
+ })}
359
+ </RadioGroup>
360
+ </section>
361
+ <section className="min-w-0 space-y-4 border-t pt-4 md:border-t-0 md:border-l md:pt-0 md:pl-6">
362
+ <div>
363
+ <h3 className="font-semibold">{t(profile.label)}</h3>
364
+ <p className="mt-1 text-sm text-muted-foreground">
365
+ {t(profile.description)}
366
+ </p>
367
+ </div>
368
+ {draft.behavior === "custom" && (
369
+ <GuardActionPicker
370
+ value={draft.action}
371
+ options={guardActionOptions}
372
+ disabled={editingDisabled}
373
+ onChange={(action) => edit({ ...draft, action })}
374
+ onCreate={persistSelectedAction}
375
+ />
283
376
  )}
377
+ {draft.behavior === "store" &&
378
+ (storeFields ? (
379
+ <div className="grid gap-3">
380
+ {(
381
+ [
382
+ ["url", "Default store link"],
383
+ ["iosUrl", "iOS store link"],
384
+ ["androidUrl", "Android store link"],
385
+ ] as const
386
+ ).map(([key, label]) => (
387
+ <Field key={key} className="gap-1.5">
388
+ <FieldLabel htmlFor={`policy-store-${key}`}>
389
+ {t(label)}
390
+ </FieldLabel>
391
+ <TextInput
392
+ id={`policy-store-${key}`}
393
+ inputMode="url"
394
+ autoCapitalize="off"
395
+ spellCheck={false}
396
+ readOnly={editingDisabled}
397
+ value={String(storeFields[key] ?? "")}
398
+ onChange={(e) =>
399
+ edit({
400
+ ...draft,
401
+ payload: setStoreLink(
402
+ storeFields,
403
+ key,
404
+ e.target.value,
405
+ ),
406
+ })
407
+ }
408
+ />
409
+ </Field>
410
+ ))}
411
+ <FieldDescription>
412
+ {t(
413
+ "The app uses its platform link first, then the default link. Use https, market or itms-apps links.",
414
+ )}
415
+ </FieldDescription>
416
+ </div>
417
+ ) : (
418
+ <p className="text-sm text-muted-foreground">
419
+ {t(
420
+ "Edit the existing store payload in advanced settings to preserve its structure.",
421
+ )}
422
+ </p>
423
+ ))}
424
+ {guarded && (
425
+ <Collapsible
426
+ key={draft.behavior}
427
+ defaultOpen={
428
+ draft.behavior === "custom" ||
429
+ (draft.delivery === "background" &&
430
+ draft.behavior !== "immediate") ||
431
+ Boolean(draft.payload && draft.behavior !== "store") ||
432
+ (draft.behavior === "store" && !storeFields)
433
+ }
434
+ >
435
+ <CollapsibleTrigger asChild>
436
+ <Button
437
+ type="button"
438
+ size="sm"
439
+ variant="outline"
440
+ className="group"
441
+ >
442
+ {t("Advanced settings")}
443
+ <ChevronDown className="group-data-[state=open]:rotate-180" />
444
+ </Button>
445
+ </CollapsibleTrigger>
446
+ <CollapsibleContent className="pt-3">
447
+ <div className="space-y-4 rounded-md border bg-muted/20 p-3">
448
+ {draft.behavior !== "required" &&
449
+ draft.behavior !== "immediate" && (
450
+ <div className="grid gap-1.5">
451
+ <Label htmlFor="policy-delivery">{t("Delivery")}</Label>
452
+ <Select
453
+ value={draft.delivery}
454
+ disabled={editingDisabled}
455
+ onValueChange={(v) =>
456
+ edit({
457
+ ...draft,
458
+ delivery: v as UpdatePolicy["delivery"],
459
+ })
460
+ }
461
+ >
462
+ <SelectTrigger
463
+ id="policy-delivery"
464
+ aria-describedby="policy-delivery-hint policy-guard-hint"
465
+ className="w-full"
466
+ >
467
+ <SelectValue />
468
+ </SelectTrigger>
469
+ <SelectContent>
470
+ <SelectItem value="manual">
471
+ {t("Manual")}
472
+ </SelectItem>
473
+ <SelectItem value="background">
474
+ {t("Background")}
475
+ </SelectItem>
476
+ </SelectContent>
477
+ </Select>
478
+ <p
479
+ id="policy-delivery-hint"
480
+ className="text-xs leading-relaxed text-muted-foreground"
481
+ >
482
+ {draft.delivery === "background"
483
+ ? t(
484
+ "When OTA is allowed, the app downloads it silently during use. It takes effect on the next full launch. For an automatic restart, select Immediate required update.",
485
+ )
486
+ : t(
487
+ "When OTA is allowed, the default client lets the user choose when to download and restart. They can postpone the update.",
488
+ )}
489
+ </p>
490
+ <p
491
+ id="policy-guard-hint"
492
+ className="text-xs leading-relaxed text-muted-foreground"
493
+ >
494
+ {draft.behavior === "store"
495
+ ? t(
496
+ "This release requires an update from the store. Both delivery modes show the store button and block the OTA download.",
497
+ )
498
+ : t(
499
+ "Delivery starts only after the app’s custom action allows the update.",
500
+ )}
501
+ </p>
502
+ </div>
503
+ )}
504
+ <div className="grid gap-1.5">
505
+ <Label htmlFor="policy-payload">
506
+ {t("Optional custom JSON payload")}
507
+ </Label>
508
+ <Textarea
509
+ id="policy-payload"
510
+ spellCheck={false}
511
+ value={draft.payload}
512
+ readOnly={editingDisabled}
513
+ className="min-h-24 font-mono text-xs"
514
+ onChange={(e) =>
515
+ edit({ ...draft, payload: e.target.value })
516
+ }
517
+ />
518
+ </div>
519
+ </div>
520
+ </CollapsibleContent>
521
+ </Collapsible>
522
+ )}
523
+ {actionError && (
524
+ <div role="status" className="text-sm text-destructive">
525
+ {t(actionError)}{" "}
526
+ <Button
527
+ size="sm"
528
+ variant="outline"
529
+ onClick={() => void persistSelectedAction(draft.action)}
530
+ >
531
+ {t("Retry")}
532
+ </Button>
533
+ </div>
534
+ )}
535
+ </section>
536
+ </div>
537
+ {!readOnly && (
538
+ <div className="flex flex-wrap items-center gap-2 border-t bg-muted/20 px-5 py-3">
539
+ <div
540
+ className="min-w-0 flex-1 text-sm text-muted-foreground"
541
+ role="status"
542
+ >
543
+ {message ? t(message) : dirty ? t("Unsaved changes") : null}
544
+ </div>
545
+ {dirty && (
546
+ <Button
547
+ size="sm"
548
+ variant="outline"
549
+ disabled={saving}
550
+ onClick={() => {
551
+ previousDrafts.current = {};
552
+ edit(draftFromPolicy(policy));
553
+ }}
554
+ >
555
+ {t("Discard changes")}
556
+ </Button>
557
+ )}
558
+ <Button
559
+ size="sm"
560
+ disabled={saving || !dirty}
561
+ onClick={() => void save()}
562
+ >
563
+ {saving ? t("Saving…") : t("Save policy")}
564
+ </Button>
284
565
  </div>
285
- ) : null}
286
- {error ? <p className="text-sm text-red-600">{t(error)}</p> : null}
287
- {message ? (
288
- <p className="text-sm text-emerald-600">{t(message)}</p>
289
- ) : null}
290
- {catalogError ? (
566
+ )}
567
+ {error && (
568
+ <Alert variant="destructive" className="mx-5 my-3 w-auto">
569
+ <AlertDescription>
570
+ {t(error)}
571
+ <Button
572
+ type="button"
573
+ variant="link"
574
+ className="h-auto px-0"
575
+ onClick={() => setLoadSequence((n) => n + 1)}
576
+ >
577
+ {t("Reload saved policy")}
578
+ </Button>
579
+ </AlertDescription>
580
+ </Alert>
581
+ )}
582
+ {catalogError && (
291
583
  <div
292
584
  role="status"
293
- className="flex items-center gap-2 text-sm text-amber-700 dark:text-amber-300"
585
+ className="flex items-center gap-2 px-5 py-3 text-sm"
294
586
  >
295
- <span>
296
- {t("Action catalog:")} {t(catalogError)}
297
- </span>
587
+ {t(catalogError)}
298
588
  <Button
299
589
  variant="outline"
300
590
  size="sm"
301
- type="button"
302
591
  onClick={() => void loadGuardActions()}
303
592
  >
304
593
  {t("Retry")}
305
594
  </Button>
306
595
  </div>
307
- ) : null}
308
- {canWriteGuardActions ? (
309
- <form
310
- onSubmit={(event) => void createCatalogOnlyAction(event)}
311
- className="flex flex-wrap items-end gap-2 rounded-md border border-border p-3 "
312
- >
313
- <label className="min-w-56 flex-1 text-xs text-muted-foreground">
314
- {t("Add action to catalog")}
315
- <TextInput
316
- value={catalogCreateValue}
317
- maxLength={100}
318
- disabled={catalogCreatePending}
319
- onChange={(event) => {
320
- setCatalogCreateValue(event.target.value);
321
- setCatalogCreateError("");
322
- }}
323
- aria-describedby="guard-action-catalog-create-help"
324
- className="mt-1 w-full rounded border border-input bg-background p-2 text-sm "
325
- placeholder={t("Reusable action name")}
326
- />
327
- </label>
328
- <Button
329
- variant="outline"
330
- size="sm"
331
- type="submit"
332
- disabled={catalogCreatePending}
333
- >
334
- {catalogCreatePending ? t("Adding…") : t("Add to catalog")}
335
- </Button>
336
- <p
337
- id="guard-action-catalog-create-help"
338
- className="w-full text-xs text-muted-foreground"
339
- >
340
- {t(
341
- "Adds a reusable catalog item without changing this policy.",
342
- )}
343
- </p>
344
- {catalogCreateError ? (
345
- <div
346
- role="status"
347
- className="flex w-full items-center gap-2 text-xs text-amber-700 dark:text-amber-300"
348
- >
349
- <span>{t(catalogCreateError)}</span>
350
- <Button variant="outline" size="sm" type="submit">
351
- {t("Retry")}
352
- </Button>
353
- </div>
354
- ) : null}
355
- </form>
356
- ) : null}
357
- <div className="space-y-2 rounded-md border border-border p-3 ">
358
- <h3 className="text-sm font-medium">{t("Action catalog")}</h3>
359
- {guardActions.length > 0 ? (
360
- <ul
361
- aria-label={t("Guard action catalog entries")}
362
- className="max-h-40 space-y-1 overflow-auto"
363
- >
364
- {guardActions.map((item) => (
365
- <li
366
- key={item.id}
367
- className="flex items-center justify-between gap-2 rounded bg-muted/30 px-2 py-1 text-sm dark:bg-muted/20"
368
- >
369
- <span className="min-w-0 truncate" title={item.actionKey}>
370
- {item.actionKey}
371
- </span>
372
- {canWriteGuardActions ? (
373
- <Button
374
- variant="destructive"
375
- size="sm"
376
- type="button"
377
- aria-label={t("Delete {0} from action catalog", [
378
- item.actionKey,
379
- ])}
380
- onClick={() =>
381
- void removeGuardAction({
382
- id: item.id,
383
- actionKey: item.actionKey,
384
- persisted: true,
385
- })
386
- }
387
- >
388
- {t("Delete")}
389
- </Button>
390
- ) : null}
391
- </li>
392
- ))}
393
- </ul>
394
- ) : (
395
- <p className="text-xs text-muted-foreground">
396
- {t("No catalog actions found.")}
397
- </p>
398
- )}
399
- </div>
400
-
401
- <div className="space-y-3 rounded-lg border border-input p-3 ">
402
- <label className="flex items-center gap-2 text-sm font-medium">
403
- <input
404
- className="size-4 rounded border-input accent-primary"
405
- type="checkbox"
406
- checked={policy.guard !== null}
407
- disabled={editingDisabled}
408
- onChange={(event) => {
409
- beginCatalogAttempt(actionAttempt.current);
410
- setActionError("");
411
- setPolicy({
412
- ...policy,
413
- guard: event.target.checked
414
- ? { action: guardActions[0]?.actionKey || "" }
415
- : null,
416
- });
417
- if (!event.target.checked) setPayload("");
418
- }}
419
- />
420
- {t("Guard enabled")}
421
- </label>
422
- <p className="text-xs text-muted-foreground">
423
- {t(
424
- "When enabled, this Guard applies unconditionally whenever this OTA is selected for a client.",
425
- )}
426
- </p>
427
- {policy.guard ? (
428
- <>
429
- <GuardActionCombobox
430
- value={policy.guard.action}
431
- options={guardActionOptions}
432
- selectionDisabled={editingDisabled}
433
- canMutateCatalog={canWriteGuardActions}
434
- mutationError={actionError}
435
- onChange={changeGuardAction}
436
- onCreate={persistSelectedAction}
437
- onDelete={removeGuardAction}
438
- onRetry={
439
- actionError
440
- ? () => persistSelectedAction(policy.guard?.action || "")
441
- : undefined
442
- }
443
- />
444
- <label className="block text-xs text-muted-foreground">
445
- {t("Optional custom JSON payload")}
446
- <Textarea
447
- value={payload}
448
- disabled={editingDisabled}
449
- onChange={(event) => setPayload(event.target.value)}
450
- className="mt-1 h-24 w-full rounded border border-input bg-background p-2 font-mono text-xs "
451
- placeholder='{"message":"Optional"}'
452
- />
453
- </label>
454
- </>
455
- ) : (
456
- <p className="text-sm text-muted-foreground">
457
- {t("No Guard will be emitted in the update manifest.")}
458
- </p>
459
- )}
460
- </div>
461
-
462
- <Button
463
- variant="default"
464
- size="sm"
465
- type="button"
466
- disabled={editingDisabled}
467
- onClick={() => void save()}
468
- >
469
- {t("Save policy")}
470
- </Button>
596
+ )}
597
+ <GuardActionCatalog
598
+ items={guardActions}
599
+ writable={catalogWritable}
600
+ onAdd={addGuardAction}
601
+ onDelete={(id) =>
602
+ setGuardActions((items) => items.filter((item) => item.id !== id))
603
+ }
604
+ />
471
605
  </Card>
472
606
  );
473
607
  }