@micha.bigler/ui-core-micha 4.31.0 → 4.32.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.
@@ -0,0 +1,222 @@
1
+ import { useSyncExternalStore } from 'react';
2
+ const registrations = new Set();
3
+ const listeners = new Set();
4
+ let grantedPurposes = new Map();
5
+ let generationByPurpose = new Map();
6
+ let version = 0;
7
+ function notify() {
8
+ version += 1;
9
+ listeners.forEach((listener) => listener());
10
+ }
11
+ function subscribe(listener) {
12
+ listeners.add(listener);
13
+ return () => listeners.delete(listener);
14
+ }
15
+ function isGranted(purposeId, serviceId) {
16
+ const services = grantedPurposes.get(purposeId);
17
+ if (!services)
18
+ return false;
19
+ return serviceId === undefined || services.has(serviceId);
20
+ }
21
+ function bumpGeneration(purposeId) {
22
+ generationByPurpose.set(purposeId, (generationByPurpose.get(purposeId) || 0) + 1);
23
+ }
24
+ function cleanupOwnedStorage(ownedStorage = {}) {
25
+ var _a, _b;
26
+ const cookieNames = Array.isArray(ownedStorage.cookies) ? ownedStorage.cookies : [];
27
+ if (typeof document !== 'undefined' && cookieNames.length) {
28
+ const visibleNames = new Set((document.cookie || '').split(';').map((part) => part.trim().split('=')[0]));
29
+ // A cookie is removed only on the path it was set with: try the root, every prefix of the current
30
+ // path and any path the adapter declares in `ownedStorage.cookiePaths`.
31
+ const segments = (typeof window !== 'undefined' ? ((_a = window.location) === null || _a === void 0 ? void 0 : _a.pathname) || '/' : '/').split('/').filter(Boolean);
32
+ const paths = new Set(['/']);
33
+ segments.reduce((prefix, segment) => {
34
+ const next = `${prefix}/${segment}`;
35
+ paths.add(next);
36
+ return next;
37
+ }, '');
38
+ (Array.isArray(ownedStorage.cookiePaths) ? ownedStorage.cookiePaths : []).forEach((path) => {
39
+ if (typeof path === 'string' && path.startsWith('/') && !/[;\s]/.test(path))
40
+ paths.add(path);
41
+ });
42
+ cookieNames.forEach((name) => {
43
+ if (typeof name !== 'string' || !name || !visibleNames.has(name))
44
+ return;
45
+ paths.forEach((path) => {
46
+ document.cookie = `${encodeURIComponent(name)}=; Max-Age=0; Path=${path}; SameSite=Lax`;
47
+ });
48
+ });
49
+ }
50
+ const removeKeys = (storageName, keys) => {
51
+ if (!Array.isArray(keys) || typeof window === 'undefined')
52
+ return;
53
+ try {
54
+ const storage = window[storageName];
55
+ keys.forEach((key) => { if (typeof key === 'string' && key)
56
+ storage === null || storage === void 0 ? void 0 : storage.removeItem(key); });
57
+ }
58
+ catch (_a) {
59
+ // Storage can be disabled in private or restricted browser contexts.
60
+ }
61
+ };
62
+ removeKeys('localStorage', ownedStorage.localStorage);
63
+ removeKeys('sessionStorage', ownedStorage.sessionStorage);
64
+ if (typeof window !== 'undefined' && Array.isArray(ownedStorage.caches) && ((_b = window.caches) === null || _b === void 0 ? void 0 : _b.delete)) {
65
+ ownedStorage.caches.forEach((name) => {
66
+ if (typeof name === 'string' && name)
67
+ Promise.resolve(window.caches.delete(name)).catch(() => { });
68
+ });
69
+ }
70
+ if (typeof indexedDB !== 'undefined' && Array.isArray(ownedStorage.indexedDB)) {
71
+ ownedStorage.indexedDB.forEach((name) => {
72
+ if (typeof name === 'string' && name) {
73
+ try {
74
+ indexedDB.deleteDatabase(name);
75
+ }
76
+ catch (_a) { /* IndexedDB may be unavailable. */ }
77
+ }
78
+ });
79
+ }
80
+ }
81
+ function deactivateEntry(entry) {
82
+ var _a;
83
+ if (!entry.active)
84
+ return;
85
+ entry.active = false;
86
+ try {
87
+ (_a = entry.deactivate) === null || _a === void 0 ? void 0 : _a.call(entry);
88
+ }
89
+ catch (_b) { /* A provider teardown must not prevent other cleanups. */ }
90
+ cleanupOwnedStorage(entry.ownedStorage);
91
+ }
92
+ function finishRevokedActivation(entry) {
93
+ var _a;
94
+ entry.active = false;
95
+ try {
96
+ (_a = entry.deactivate) === null || _a === void 0 ? void 0 : _a.call(entry);
97
+ }
98
+ catch (_b) { /* Late activation cleanup is best effort. */ }
99
+ cleanupOwnedStorage(entry.ownedStorage);
100
+ }
101
+ function activateEntry(entry) {
102
+ var _a;
103
+ if (entry.active || !isGranted(entry.purposeId, entry.serviceId))
104
+ return;
105
+ entry.active = true;
106
+ const epoch = generationByPurpose.get(entry.purposeId) || 0;
107
+ // Each activation owns a token: a late completion of a superseded activation (refuse, then re-grant)
108
+ // must never tear down the newer, currently granted activation of the same registration.
109
+ const token = {};
110
+ entry.token = token;
111
+ const guard = createPrivacyGuard(entry.purposeId);
112
+ let result;
113
+ try {
114
+ result = (_a = entry.activate) === null || _a === void 0 ? void 0 : _a.call(entry, guard);
115
+ }
116
+ catch (_b) {
117
+ deactivateEntry(entry);
118
+ return;
119
+ }
120
+ if (result && typeof result.then === 'function') {
121
+ Promise.resolve(result).then(() => {
122
+ if (entry.token !== token)
123
+ return;
124
+ if (!entry.active || epoch !== (generationByPurpose.get(entry.purposeId) || 0)) {
125
+ finishRevokedActivation(entry);
126
+ }
127
+ }).catch(() => {
128
+ if (entry.token !== token)
129
+ return;
130
+ if (entry.active && epoch === (generationByPurpose.get(entry.purposeId) || 0))
131
+ deactivateEntry(entry);
132
+ else
133
+ finishRevokedActivation(entry);
134
+ });
135
+ }
136
+ }
137
+ /**
138
+ * Update the registry from server-confirmed choices. `locallyDisabled` is an
139
+ * in-memory deny list used while a refusal write is pending or failed.
140
+ */
141
+ export function setPrivacyState(manifest, choices, locallyDisabled = new Set()) {
142
+ const next = new Map();
143
+ const disabled = locallyDisabled instanceof Set ? locallyDisabled : new Set(locallyDisabled || []);
144
+ const purposes = Array.isArray(manifest === null || manifest === void 0 ? void 0 : manifest.purposes) ? manifest.purposes : [];
145
+ purposes.forEach((purpose) => {
146
+ var _a, _b;
147
+ if (purpose.required || disabled.has(purpose.id))
148
+ return;
149
+ if (((_b = (_a = choices === null || choices === void 0 ? void 0 : choices.choices) === null || _a === void 0 ? void 0 : _a[purpose.id]) === null || _b === void 0 ? void 0 : _b.state) !== 'granted')
150
+ return;
151
+ next.set(purpose.id, new Set((purpose.services || []).map((service) => service.id)));
152
+ });
153
+ const changedPurposes = new Set([...grantedPurposes.keys(), ...next.keys()]);
154
+ changedPurposes.forEach((purposeId) => {
155
+ const wasGranted = grantedPurposes.has(purposeId);
156
+ const nowGranted = next.has(purposeId);
157
+ if (wasGranted !== nowGranted)
158
+ bumpGeneration(purposeId);
159
+ });
160
+ grantedPurposes = next;
161
+ registrations.forEach((entry) => {
162
+ if (isGranted(entry.purposeId, entry.serviceId))
163
+ activateEntry(entry);
164
+ else
165
+ deactivateEntry(entry);
166
+ });
167
+ notify();
168
+ }
169
+ /** Register one app-owned adapter and the exact storage entries it owns. */
170
+ export function registerPrivacyService({ purposeId, serviceId, activate, deactivate, ownedStorage = {}, } = {}) {
171
+ if (typeof purposeId !== 'string' || !purposeId || typeof serviceId !== 'string' || !serviceId) {
172
+ throw new TypeError('registerPrivacyService requires purposeId and serviceId strings');
173
+ }
174
+ const entry = { purposeId, serviceId, activate, deactivate, ownedStorage, active: false };
175
+ registrations.add(entry);
176
+ activateEntry(entry);
177
+ return () => {
178
+ if (!registrations.delete(entry))
179
+ return;
180
+ deactivateEntry(entry);
181
+ };
182
+ }
183
+ /** A callback guard becomes permanently stale after a purpose is withdrawn. */
184
+ export function createPrivacyGuard(purposeId) {
185
+ const generation = generationByPurpose.get(purposeId) || 0;
186
+ const isCurrent = () => isGranted(purposeId) && generation === (generationByPurpose.get(purposeId) || 0);
187
+ return {
188
+ isCurrent,
189
+ run(fn) {
190
+ if (typeof fn !== 'function' || !isCurrent())
191
+ return undefined;
192
+ return fn();
193
+ },
194
+ };
195
+ }
196
+ /** Resolve an optional chunk only for a currently granted purpose. */
197
+ export async function guardedImport(purposeId, loader, serviceId) {
198
+ if (typeof loader !== 'function' || !isGranted(purposeId, serviceId)) {
199
+ throw new Error('privacy_choice_required');
200
+ }
201
+ const guard = createPrivacyGuard(purposeId);
202
+ const module = await loader();
203
+ if (!guard.isCurrent() || !isGranted(purposeId, serviceId))
204
+ throw new Error('privacy_choice_withdrawn');
205
+ return module;
206
+ }
207
+ /** React hook for adapter code; it is false during loading, errors and refusal. */
208
+ export function useGrantedPurpose(purposeId, serviceId) {
209
+ const snapshot = () => `${version}:${isGranted(purposeId, serviceId)}`;
210
+ const value = useSyncExternalStore(subscribe, snapshot, () => '0:false');
211
+ return value.endsWith(':true');
212
+ }
213
+ export function isPurposeGranted(purposeId, serviceId) {
214
+ return isGranted(purposeId, serviceId);
215
+ }
216
+ export function resetPrivacyServicesForTests() {
217
+ registrations.forEach(deactivateEntry);
218
+ registrations.clear();
219
+ grantedPurposes = new Map();
220
+ generationByPurpose = new Map();
221
+ notify();
222
+ }
@@ -35,6 +35,7 @@ import { SIGN_UP_PROVIDER_ALERT_SX, SIGN_UP_PROVIDER_DIVIDER_SX } from '../compo
35
35
  import { PROFILE_STEP_CHECKBOX_SX, PROFILE_STEP_OPTION_SX } from '../components/ProfileStepContent';
36
36
  import { SETUP_WIZARD_CARD_SX } from '../onboarding/SetupWizardShell';
37
37
  import { REAUTH_MODAL_ALERT_SX, REAUTH_MODAL_TEXT_FIELD_SX } from '../auth/ReauthModal';
38
+ import { PRIVACY_BANNER_CLOSE_SX, PRIVACY_BANNER_PAPER_SX } from '../privacy/PrivacyBanner';
38
39
  import { createKitSxEntry } from './kitSxEntry';
39
40
  import { ACCESS_ENDED_BANNER_ALERT_SX } from '../billing/AccessEndedBanner';
40
41
  import { BILLING_ACCESS_BADGE_SX } from '../billing/BillingAccessCell';
@@ -51,6 +52,8 @@ import { RECEIPT_CAPTURE_ACTION_SX, RECEIPT_CAPTURE_ALERT_SX, RECEIPT_CAPTURE_CA
51
52
  import { RECEIPT_PICKER_ACTION_SX, RECEIPT_PICKER_CARD_SX } from '../receipts/ReceiptPicker';
52
53
  import { RECEIPT_REVIEW_ACTIONS_SX, RECEIPT_REVIEW_ACTION_BUTTON_SX, RECEIPT_REVIEW_CHIP_SX, RECEIPT_REVIEW_DIALOG_PAPER_SX, RECEIPT_REVIEW_SHEET_PAPER_SX } from '../receipts/ReceiptReviewDialog';
53
54
  const SX_EXPORTS = {
55
+ PRIVACY_BANNER_CLOSE_SX,
56
+ PRIVACY_BANNER_PAPER_SX,
54
57
  ACCESS_CODE_ACTION_SX,
55
58
  ACCESS_ENDED_BANNER_ALERT_SX,
56
59
  BILLING_ACCESS_BADGE_SX,
@@ -174,6 +177,8 @@ export { createKitSxEntry };
174
177
  * checks rather than being represented here as if they were fully inspected.
175
178
  */
176
179
  export const KIT_COMPONENT_SX_REGISTRY = [
180
+ entry('PrivacyBanner.paper', 'MuiPaper', PRIVACY_BANNER_PAPER_SX),
181
+ entry('PrivacyBanner.close', 'MuiButton', PRIVACY_BANNER_CLOSE_SX),
177
182
  entry('AccessCodeManager.action', 'MuiButton', ACCESS_CODE_ACTION_SX),
178
183
  entry('AccessCodeManager.primaryAction', 'MuiButton', ACCESS_CODE_PRIMARY_ACTION_SX),
179
184
  entry('AccessCodeManager.alert', 'MuiAlert', ACCESS_CODE_ALERT_SX),
@@ -1,22 +1,27 @@
1
1
  /** Identity used to bind editable profile draft fields to one account. */
2
2
  export const profileIdentity = (user) => { var _a, _b, _c; return user ? ((_c = (_b = (_a = user.id) !== null && _a !== void 0 ? _a : user.email) !== null && _b !== void 0 ? _b : user.username) !== null && _c !== void 0 ? _c : null) : null; };
3
3
  /** Refresh untouched profile values while retaining edits for the same identity. */
4
- export function refreshProfileDraft(user, previousIdentity, editedFields = new Set(), draft = {}) {
4
+ export function refreshProfileDraft(user, previousIdentity, editedFields = new Set(), draft = {}, privacyMode = 'legacy') {
5
5
  var _a, _b, _c, _d;
6
+ const includeLegacyPrivacy = privacyMode !== 'central';
6
7
  const identity = profileIdentity(user);
7
8
  const samePerson = previousIdentity === identity;
8
- const retained = samePerson ? editedFields : new Set();
9
+ const previousEdits = samePerson ? editedFields : new Set();
10
+ const retained = includeLegacyPrivacy
11
+ ? previousEdits
12
+ : new Set([...previousEdits].filter((key) => key !== 'acceptedPrivacy' && key !== 'acceptedCookies'));
9
13
  if (!user)
10
- return { identity, editedFields: retained, values: { username: '', email: '', firstName: '', lastName: '', acceptedPrivacy: false, acceptedCookies: false } };
14
+ return {
15
+ identity,
16
+ editedFields: retained,
17
+ values: Object.assign({ username: '', email: '', firstName: '', lastName: '' }, (includeLegacyPrivacy ? { acceptedPrivacy: false, acceptedCookies: false } : {})),
18
+ };
11
19
  return {
12
20
  identity,
13
21
  editedFields: retained,
14
- values: {
15
- username: (_a = user.username) !== null && _a !== void 0 ? _a : '', email: (_b = user.email) !== null && _b !== void 0 ? _b : '',
16
- firstName: retained.has('firstName') ? draft.firstName : ((_c = user.first_name) !== null && _c !== void 0 ? _c : ''),
17
- lastName: retained.has('lastName') ? draft.lastName : ((_d = user.last_name) !== null && _d !== void 0 ? _d : ''),
22
+ values: Object.assign({ username: (_a = user.username) !== null && _a !== void 0 ? _a : '', email: (_b = user.email) !== null && _b !== void 0 ? _b : '', firstName: retained.has('firstName') ? draft.firstName : ((_c = user.first_name) !== null && _c !== void 0 ? _c : ''), lastName: retained.has('lastName') ? draft.lastName : ((_d = user.last_name) !== null && _d !== void 0 ? _d : '') }, (includeLegacyPrivacy ? {
18
23
  acceptedPrivacy: retained.has('acceptedPrivacy') ? draft.acceptedPrivacy : Boolean(user.accepted_privacy_statement),
19
24
  acceptedCookies: retained.has('acceptedCookies') ? draft.acceptedCookies : Boolean(user.accepted_convenience_cookies),
20
- },
25
+ } : {})),
21
26
  };
22
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micha.bigler/ui-core-micha",
3
- "version": "4.31.0",
3
+ "version": "4.32.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "exports": {