Repository navigation
Expand file tree
/
Copy pathfeatureFlags.js
More file actions
69 lines (62 loc) · 2.21 KB
/
Copy pathfeatureFlags.js
File metadata and controls
69 lines (62 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import piml from 'piml';
// Vite resolves public assets from the site root. `url` opts into its public
// asset resolver; `raw` loads that same file's contents for the first render.
import bundledFlags from '/feature-flag.piml?url&raw';
import { isSiteTheme } from './siteThemes';
import * as LocalStorageManager from './LocalStorageManager';
export const FALLBACK_THEME = 'studio';
export const parseFeatureFlags = (text) => {
try {
const parsed = piml.parse(text);
const flags = parsed.featureFlags?.flag || parsed.featureFlags || [];
return (Array.isArray(flags) ? flags : [flags]).filter(
(flag) => flag && typeof flag === 'object',
);
} catch {
return [];
}
};
export const BUNDLED_FEATURE_FLAGS = parseFeatureFlags(bundledFlags);
// The range is inclusive. Explicit offsets in PIML keep the campaign's dates
// independent of a visitor's local timezone.
export const defaultThemeFor = (flags, now = Date.now()) => {
const timestamp = Number(now);
const match = flags.find((flag) => {
const from = Date.parse(flag.from);
const to = Date.parse(flag.to);
return (
flag.type === 'default-theme' &&
String(flag.isActive).toLowerCase() === 'true' &&
isSiteTheme(flag.theme) &&
Number.isFinite(from) &&
Number.isFinite(to) &&
from <= to &&
timestamp >= from &&
timestamp <= to
);
});
return match?.theme || FALLBACK_THEME;
};
export const readSavedTheme = () => {
const theme = LocalStorageManager.get('fezcodex-theme', null);
return isSiteTheme(theme) ? theme : null;
};
export const readUrlTheme = () => {
if (typeof window === 'undefined') return null;
const theme = new URLSearchParams(window.location.search).get('fezTheme');
return isSiteTheme(theme) ? theme : null;
};
// Components above VisualSettingsProvider (notably Toast) use the published
// active value without turning a temporary campaign default into a preference.
export const readActiveTheme = () => {
const active =
typeof document === 'undefined'
? null
: document.documentElement.dataset.fezcodexTheme;
return (
readUrlTheme() ||
(isSiteTheme(active) ? active : null) ||
readSavedTheme() ||
defaultThemeFor(BUNDLED_FEATURE_FLAGS)
);
};