@superwall/studio 1.1.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.
- package/LICENSE +105 -0
- package/README.md +78 -0
- package/dist/assets/index-CVKGrx00.js +11 -0
- package/dist/assets/index-D4RIpSbg.css +1 -0
- package/dist/icon.svg +5 -0
- package/dist/index.html +23 -0
- package/package.json +66 -0
- package/src/StudioApp.tsx +78 -0
- package/src/components/ActionMenu.tsx +174 -0
- package/src/components/CompareMenu.tsx +147 -0
- package/src/components/DeviceChrome.tsx +124 -0
- package/src/components/DeviceFrame.tsx +210 -0
- package/src/components/MissingProducts.tsx +46 -0
- package/src/components/OpenInApp.tsx +68 -0
- package/src/components/PaywallToasts.tsx +208 -0
- package/src/components/SidePanel.tsx +36 -0
- package/src/components/SurfaceCard.tsx +93 -0
- package/src/components/Toolbar.tsx +16 -0
- package/src/components/TopBar.tsx +41 -0
- package/src/components/VariablesPanel.tsx +200 -0
- package/src/components/controls.tsx +70 -0
- package/src/components/icons.tsx +139 -0
- package/src/index.ts +20 -0
- package/src/lib/actions.ts +79 -0
- package/src/lib/devices.ts +109 -0
- package/src/lib/events.ts +77 -0
- package/src/lib/preview.ts +39 -0
- package/src/lib/project.ts +64 -0
- package/src/lib/time.ts +21 -0
- package/src/lib/types.ts +85 -0
- package/src/main.tsx +49 -0
- package/src/screens/SurfaceEditor.tsx +498 -0
- package/src/screens/SurfaceList.tsx +156 -0
- package/src/styles.css +221 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { DeviceChromeSpec } from "../lib/devices";
|
|
2
|
+
|
|
3
|
+
const CellularIcon = () => (
|
|
4
|
+
<svg width="19" height="12" viewBox="0 0 19 12" fill="currentColor" aria-hidden="true">
|
|
5
|
+
<rect x="0" y="7.5" width="3.5" height="4.5" rx="1" />
|
|
6
|
+
<rect x="5" y="5" width="3.5" height="7" rx="1" />
|
|
7
|
+
<rect x="10" y="2.5" width="3.5" height="9.5" rx="1" />
|
|
8
|
+
<rect x="15" y="0" width="3.5" height="12" rx="1" />
|
|
9
|
+
</svg>
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const BatteryIcon = () => (
|
|
13
|
+
<svg width="27" height="13" viewBox="0 0 27 13" aria-hidden="true">
|
|
14
|
+
<rect
|
|
15
|
+
x="0.5"
|
|
16
|
+
y="0.5"
|
|
17
|
+
width="23"
|
|
18
|
+
height="12"
|
|
19
|
+
rx="3.8"
|
|
20
|
+
fill="none"
|
|
21
|
+
stroke="currentColor"
|
|
22
|
+
strokeOpacity="0.35"
|
|
23
|
+
/>
|
|
24
|
+
<path d="M25 4.5v4a2.17 2.17 0 0 0 0-4Z" fill="currentColor" fillOpacity="0.4" />
|
|
25
|
+
<rect x="2" y="2" width="20" height="9" rx="2.5" fill="currentColor" />
|
|
26
|
+
</svg>
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const StatusCluster = () => (
|
|
30
|
+
<span className="flex items-center gap-[7px]">
|
|
31
|
+
<CellularIcon />
|
|
32
|
+
<span style={{ fontSize: 12.5, fontWeight: 700, letterSpacing: "0.02em" }}>SW</span>
|
|
33
|
+
<BatteryIcon />
|
|
34
|
+
</span>
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Drawn in device pixels over the preview iframe; the frame scales the whole
|
|
39
|
+
* layer. Everything is pointer-events-none so taps land on the paywall.
|
|
40
|
+
*/
|
|
41
|
+
export const DeviceChrome = ({
|
|
42
|
+
spec,
|
|
43
|
+
width,
|
|
44
|
+
height,
|
|
45
|
+
landscape,
|
|
46
|
+
theme,
|
|
47
|
+
}: {
|
|
48
|
+
spec: DeviceChromeSpec;
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
landscape: boolean;
|
|
52
|
+
theme: "light" | "dark";
|
|
53
|
+
}) => {
|
|
54
|
+
const ink = theme === "dark" ? "rgba(255,255,255,0.95)" : "rgba(0,0,0,0.95)";
|
|
55
|
+
const sidePadding = Math.max(28, spec.radius * 0.6);
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div
|
|
59
|
+
className="pointer-events-none absolute inset-0 select-none"
|
|
60
|
+
style={{ width, height, color: ink }}
|
|
61
|
+
data-testid="device-chrome"
|
|
62
|
+
>
|
|
63
|
+
{!landscape && (
|
|
64
|
+
<div
|
|
65
|
+
className="absolute inset-x-0 top-0 flex items-center justify-between"
|
|
66
|
+
style={{
|
|
67
|
+
height: spec.statusBarHeight,
|
|
68
|
+
paddingLeft: sidePadding,
|
|
69
|
+
paddingRight: sidePadding,
|
|
70
|
+
fontFamily:
|
|
71
|
+
'-apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif',
|
|
72
|
+
fontSize: 15,
|
|
73
|
+
fontWeight: 600,
|
|
74
|
+
letterSpacing: "-0.2px",
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
<span>9:41</span>
|
|
78
|
+
<StatusCluster />
|
|
79
|
+
</div>
|
|
80
|
+
)}
|
|
81
|
+
|
|
82
|
+
{spec.cutout === "island" &&
|
|
83
|
+
(landscape ? (
|
|
84
|
+
<div
|
|
85
|
+
className="absolute rounded-full bg-black"
|
|
86
|
+
style={{ left: 12, top: (height - 124) / 2, width: 36, height: 124 }}
|
|
87
|
+
/>
|
|
88
|
+
) : (
|
|
89
|
+
<div
|
|
90
|
+
className="absolute rounded-full bg-black"
|
|
91
|
+
style={{ left: (width - 124) / 2, top: 12, width: 124, height: 36 }}
|
|
92
|
+
/>
|
|
93
|
+
))}
|
|
94
|
+
|
|
95
|
+
{spec.cutout === "punch" &&
|
|
96
|
+
(landscape ? (
|
|
97
|
+
<div
|
|
98
|
+
className="absolute rounded-full bg-black"
|
|
99
|
+
style={{ left: 16, top: (height - 13) / 2, width: 13, height: 13 }}
|
|
100
|
+
/>
|
|
101
|
+
) : (
|
|
102
|
+
<div
|
|
103
|
+
className="absolute rounded-full bg-black"
|
|
104
|
+
style={{ left: (width - 13) / 2, top: 15, width: 13, height: 13 }}
|
|
105
|
+
/>
|
|
106
|
+
))}
|
|
107
|
+
|
|
108
|
+
{spec.homeIndicator && (
|
|
109
|
+
<div
|
|
110
|
+
className="absolute rounded-full"
|
|
111
|
+
style={{
|
|
112
|
+
width: Math.round(Math.min(width, height) * 0.36),
|
|
113
|
+
height: 5,
|
|
114
|
+
left: "50%",
|
|
115
|
+
transform: "translateX(-50%)",
|
|
116
|
+
bottom: 8,
|
|
117
|
+
background: ink,
|
|
118
|
+
opacity: 0.9,
|
|
119
|
+
}}
|
|
120
|
+
/>
|
|
121
|
+
)}
|
|
122
|
+
</div>
|
|
123
|
+
);
|
|
124
|
+
};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { DeviceChrome } from "./DeviceChrome";
|
|
3
|
+
import type { DeviceChromeSpec } from "../lib/devices";
|
|
4
|
+
|
|
5
|
+
const FRAME_MARGIN = 96;
|
|
6
|
+
|
|
7
|
+
const SWAP_DELAY_MS = 250;
|
|
8
|
+
|
|
9
|
+
type PreviewWindow = Window & { __swPreviewApply?: (search: string) => void };
|
|
10
|
+
|
|
11
|
+
const useFitScale = (
|
|
12
|
+
container: React.RefObject<HTMLElement | null>,
|
|
13
|
+
width: number,
|
|
14
|
+
height: number,
|
|
15
|
+
): number => {
|
|
16
|
+
const [scale, setScale] = React.useState(1);
|
|
17
|
+
|
|
18
|
+
React.useLayoutEffect(() => {
|
|
19
|
+
const element = container.current;
|
|
20
|
+
if (!element) return undefined;
|
|
21
|
+
|
|
22
|
+
const fit = () => {
|
|
23
|
+
const available = element.getBoundingClientRect();
|
|
24
|
+
const next = Math.min(
|
|
25
|
+
1,
|
|
26
|
+
(available.width - FRAME_MARGIN) / width,
|
|
27
|
+
(available.height - FRAME_MARGIN) / height,
|
|
28
|
+
);
|
|
29
|
+
setScale(Math.max(0.1, next));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
fit();
|
|
33
|
+
if (typeof ResizeObserver === "undefined") return undefined;
|
|
34
|
+
const observer = new ResizeObserver(fit);
|
|
35
|
+
observer.observe(element);
|
|
36
|
+
return () => observer.disconnect();
|
|
37
|
+
}, [container, width, height]);
|
|
38
|
+
|
|
39
|
+
return scale;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const DevicePlaceholder = ({
|
|
43
|
+
width,
|
|
44
|
+
height,
|
|
45
|
+
stageRef,
|
|
46
|
+
children,
|
|
47
|
+
}: React.PropsWithChildren<{
|
|
48
|
+
width: number;
|
|
49
|
+
height: number;
|
|
50
|
+
stageRef: React.RefObject<HTMLElement | null>;
|
|
51
|
+
}>) => {
|
|
52
|
+
const scale = useFitScale(stageRef, width, height);
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div
|
|
56
|
+
className="flex flex-none items-center justify-center overflow-hidden border border-dashed border-cream/20 p-6 text-center text-sm text-cream/55"
|
|
57
|
+
data-testid="device-placeholder"
|
|
58
|
+
style={{ width: width * scale, height: height * scale }}
|
|
59
|
+
>
|
|
60
|
+
{children}
|
|
61
|
+
</div>
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
type Frame = {
|
|
66
|
+
mountSrc: string;
|
|
67
|
+
currentSrc: string;
|
|
68
|
+
generation: number;
|
|
69
|
+
ready: boolean;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const instantApply = (node: HTMLIFrameElement | undefined, from: string, to: string): boolean => {
|
|
73
|
+
try {
|
|
74
|
+
const base = window.location.origin;
|
|
75
|
+
const fromUrl = new URL(from, base);
|
|
76
|
+
const toUrl = new URL(to, base);
|
|
77
|
+
if (fromUrl.origin !== toUrl.origin || fromUrl.pathname !== toUrl.pathname) return false;
|
|
78
|
+
const apply = (node?.contentWindow as PreviewWindow | null | undefined)?.__swPreviewApply;
|
|
79
|
+
if (typeof apply !== "function") return false;
|
|
80
|
+
apply(toUrl.search);
|
|
81
|
+
return true;
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const DeviceFrame = ({
|
|
88
|
+
width,
|
|
89
|
+
height,
|
|
90
|
+
title,
|
|
91
|
+
src,
|
|
92
|
+
stageRef,
|
|
93
|
+
reloadKey = 0,
|
|
94
|
+
chrome,
|
|
95
|
+
landscape = false,
|
|
96
|
+
theme = "light",
|
|
97
|
+
}: {
|
|
98
|
+
width: number;
|
|
99
|
+
height: number;
|
|
100
|
+
title: string;
|
|
101
|
+
src: string;
|
|
102
|
+
stageRef: React.RefObject<HTMLElement | null>;
|
|
103
|
+
reloadKey?: number;
|
|
104
|
+
chrome?: DeviceChromeSpec;
|
|
105
|
+
landscape?: boolean;
|
|
106
|
+
theme?: "light" | "dark";
|
|
107
|
+
}) => {
|
|
108
|
+
const scale = useFitScale(stageRef, width, height);
|
|
109
|
+
|
|
110
|
+
const generation = React.useRef(0);
|
|
111
|
+
const [frames, setFrames] = React.useState<Frame[]>([
|
|
112
|
+
{ mountSrc: src, currentSrc: src, generation: 0, ready: true },
|
|
113
|
+
]);
|
|
114
|
+
const framesRef = React.useRef(frames);
|
|
115
|
+
framesRef.current = frames;
|
|
116
|
+
const nodes = React.useRef(new Map<number, HTMLIFrameElement>());
|
|
117
|
+
|
|
118
|
+
const mounted = React.useRef(false);
|
|
119
|
+
const lastReload = React.useRef(reloadKey);
|
|
120
|
+
React.useEffect(() => {
|
|
121
|
+
if (!mounted.current) {
|
|
122
|
+
mounted.current = true;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const forced = reloadKey !== lastReload.current;
|
|
126
|
+
lastReload.current = reloadKey;
|
|
127
|
+
|
|
128
|
+
if (!forced) {
|
|
129
|
+
const visible = framesRef.current.findLast((frame) => frame.ready);
|
|
130
|
+
const node = visible ? nodes.current.get(visible.generation) : undefined;
|
|
131
|
+
if (visible && instantApply(node, visible.currentSrc, src)) {
|
|
132
|
+
setFrames((existing) =>
|
|
133
|
+
existing.map((frame) =>
|
|
134
|
+
frame.generation === visible.generation ? { ...frame, currentSrc: src } : frame,
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
generation.current += 1;
|
|
142
|
+
const next: Frame = {
|
|
143
|
+
mountSrc: src,
|
|
144
|
+
currentSrc: src,
|
|
145
|
+
generation: generation.current,
|
|
146
|
+
ready: false,
|
|
147
|
+
};
|
|
148
|
+
setFrames((existing) => [...existing.filter((frame) => frame.ready).slice(-1), next]);
|
|
149
|
+
}, [src, reloadKey]);
|
|
150
|
+
|
|
151
|
+
const commit = (committed: number) => {
|
|
152
|
+
window.setTimeout(() => {
|
|
153
|
+
setFrames((existing) => {
|
|
154
|
+
if (!existing.some((frame) => frame.generation === committed)) return existing;
|
|
155
|
+
return existing
|
|
156
|
+
.map((frame) => (frame.generation === committed ? { ...frame, ready: true } : frame))
|
|
157
|
+
.filter((frame) => frame.generation >= committed);
|
|
158
|
+
});
|
|
159
|
+
}, SWAP_DELAY_MS);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const visible = frames.findLast((frame) => frame.ready);
|
|
163
|
+
|
|
164
|
+
const radius = chrome ? chrome.radius * scale : 0;
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<div
|
|
168
|
+
className={`bg-panel relative flex-none overflow-hidden ${
|
|
169
|
+
chrome ? "shadow-[0_0_0_1px] shadow-cream/25" : "border border-cream/20"
|
|
170
|
+
}`}
|
|
171
|
+
data-testid="device-frame"
|
|
172
|
+
style={{ width: width * scale, height: height * scale, borderRadius: radius }}
|
|
173
|
+
>
|
|
174
|
+
{frames.map((frame) => (
|
|
175
|
+
<iframe
|
|
176
|
+
key={frame.generation}
|
|
177
|
+
ref={(node) => {
|
|
178
|
+
if (node) nodes.current.set(frame.generation, node);
|
|
179
|
+
else nodes.current.delete(frame.generation);
|
|
180
|
+
}}
|
|
181
|
+
className={`absolute inset-0 origin-top-left border-0 bg-white transition-opacity duration-150 ${
|
|
182
|
+
frame === visible ? "opacity-100" : "opacity-0"
|
|
183
|
+
}`}
|
|
184
|
+
title={title}
|
|
185
|
+
src={frame.mountSrc}
|
|
186
|
+
width={width}
|
|
187
|
+
height={height}
|
|
188
|
+
style={{ transform: `scale(${scale})` }}
|
|
189
|
+
onLoad={() => {
|
|
190
|
+
if (!frame.ready) commit(frame.generation);
|
|
191
|
+
}}
|
|
192
|
+
/>
|
|
193
|
+
))}
|
|
194
|
+
{chrome && (
|
|
195
|
+
<div
|
|
196
|
+
className="pointer-events-none absolute inset-0 origin-top-left"
|
|
197
|
+
style={{ width, height, transform: `scale(${scale})` }}
|
|
198
|
+
>
|
|
199
|
+
<DeviceChrome
|
|
200
|
+
spec={chrome}
|
|
201
|
+
width={width}
|
|
202
|
+
height={height}
|
|
203
|
+
landscape={landscape}
|
|
204
|
+
theme={theme}
|
|
205
|
+
/>
|
|
206
|
+
</div>
|
|
207
|
+
)}
|
|
208
|
+
</div>
|
|
209
|
+
);
|
|
210
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export const MissingProducts = ({
|
|
2
|
+
paywall,
|
|
3
|
+
missing,
|
|
4
|
+
onDismiss,
|
|
5
|
+
}: {
|
|
6
|
+
paywall: string;
|
|
7
|
+
missing: { reference: string; identifier: string }[];
|
|
8
|
+
onDismiss: () => void;
|
|
9
|
+
}) => (
|
|
10
|
+
<div
|
|
11
|
+
className="fixed inset-0 z-50 flex items-center justify-center bg-ink/70 p-6"
|
|
12
|
+
role="dialog"
|
|
13
|
+
aria-modal="true"
|
|
14
|
+
aria-labelledby="sw-missing-products"
|
|
15
|
+
>
|
|
16
|
+
<div className="bg-panel flex w-full max-w-md flex-col gap-4 border border-cream/20 p-5">
|
|
17
|
+
<div className="flex flex-col gap-1.5">
|
|
18
|
+
<span id="sw-missing-products" className="text-[0.9375rem] font-medium text-cream">
|
|
19
|
+
{missing.length === 1 ? "A product is missing" : `${missing.length} products are missing`}
|
|
20
|
+
</span>
|
|
21
|
+
<span className="text-[0.8125rem] leading-relaxed text-cream/60">
|
|
22
|
+
{`${paywall} declares ${missing.length === 1 ? "a product" : "products"} Superwall has no record of. Every variable on ${missing.length === 1 ? "it" : "them"} will be undefined here and on device.`}
|
|
23
|
+
</span>
|
|
24
|
+
</div>
|
|
25
|
+
|
|
26
|
+
<ul className="flex flex-col gap-1.5 border-y border-cream/10 py-3">
|
|
27
|
+
{missing.map((entry) => (
|
|
28
|
+
<li key={entry.reference} className="flex items-baseline justify-between gap-3">
|
|
29
|
+
<span className="font-mono text-[11px] text-cream/70">{entry.reference}</span>
|
|
30
|
+
<span className="truncate font-mono text-[11px] text-cream/40">{entry.identifier}</span>
|
|
31
|
+
</li>
|
|
32
|
+
))}
|
|
33
|
+
</ul>
|
|
34
|
+
|
|
35
|
+
<span className="text-xs text-cream/45">
|
|
36
|
+
Add {missing.length === 1 ? "it" : "them"} in the Superwall dashboard, or point the config
|
|
37
|
+
at{missing.length === 1 ? " a product" : " products"} that exists. Pushing is refused until
|
|
38
|
+
then.
|
|
39
|
+
</span>
|
|
40
|
+
|
|
41
|
+
<button type="button" className="sw-btn sw-btn--primary self-end" onClick={onDismiss}>
|
|
42
|
+
View anyway
|
|
43
|
+
</button>
|
|
44
|
+
</div>
|
|
45
|
+
</div>
|
|
46
|
+
);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
const STORAGE_KEY = "superwall-device-scheme";
|
|
4
|
+
|
|
5
|
+
const readStored = (): string | undefined => {
|
|
6
|
+
try {
|
|
7
|
+
return window.localStorage.getItem(STORAGE_KEY) ?? undefined;
|
|
8
|
+
} catch {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const store = (value: string) => {
|
|
14
|
+
try {
|
|
15
|
+
window.localStorage.setItem(STORAGE_KEY, value);
|
|
16
|
+
} catch {
|
|
17
|
+
/* private browsing */
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const linkFor = (scheme: string, origin: string): string =>
|
|
22
|
+
`${scheme}://?superwall_dev=${encodeURIComponent(origin)}`;
|
|
23
|
+
|
|
24
|
+
export const OpenInApp = ({ origin, scheme }: { origin: string; scheme?: string }) => {
|
|
25
|
+
const [typed, setTyped] = React.useState(readStored);
|
|
26
|
+
const known = typed ?? scheme;
|
|
27
|
+
|
|
28
|
+
const ask = (initial: string): string | undefined => {
|
|
29
|
+
const answer = window.prompt("Your app's URL scheme, so this can open it:", initial);
|
|
30
|
+
const trimmed = answer?.trim();
|
|
31
|
+
if (trimmed === undefined || trimmed === "") return undefined;
|
|
32
|
+
setTyped(trimmed);
|
|
33
|
+
store(trimmed);
|
|
34
|
+
return trimmed;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const open = (event: React.MouseEvent) => {
|
|
38
|
+
if (known !== undefined) return;
|
|
39
|
+
event.preventDefault();
|
|
40
|
+
const answer = ask("");
|
|
41
|
+
if (answer !== undefined) window.location.assign(linkFor(answer, origin));
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<div className="flex items-center gap-2">
|
|
46
|
+
{known !== undefined && (
|
|
47
|
+
<button
|
|
48
|
+
className="sw-btn sw-btn--icon"
|
|
49
|
+
type="button"
|
|
50
|
+
aria-label="Edit app URL scheme"
|
|
51
|
+
onClick={() => ask(known)}
|
|
52
|
+
>
|
|
53
|
+
<svg className="sw-icon" viewBox="0 0 24 24" aria-hidden="true">
|
|
54
|
+
<path d="M12 20h9" />
|
|
55
|
+
<path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4Z" />
|
|
56
|
+
</svg>
|
|
57
|
+
</button>
|
|
58
|
+
)}
|
|
59
|
+
<a
|
|
60
|
+
className="sw-btn sw-btn--primary"
|
|
61
|
+
href={known === undefined ? "#" : linkFor(known, origin)}
|
|
62
|
+
onClick={open}
|
|
63
|
+
>
|
|
64
|
+
Open In App
|
|
65
|
+
</a>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { toast, Toaster } from "sonner";
|
|
3
|
+
import {
|
|
4
|
+
AlertTriangleIcon,
|
|
5
|
+
BellIcon,
|
|
6
|
+
BoltIcon,
|
|
7
|
+
CartIcon,
|
|
8
|
+
CheckCircleIcon,
|
|
9
|
+
CloseIcon,
|
|
10
|
+
ExternalIcon,
|
|
11
|
+
InfoCircleIcon,
|
|
12
|
+
RotateIcon,
|
|
13
|
+
StarIcon,
|
|
14
|
+
UserIcon,
|
|
15
|
+
} from "./icons";
|
|
16
|
+
import { isBridgeMessage, noticeFor, STUDIO_CHANNEL, type EventNotice } from "../lib/events";
|
|
17
|
+
|
|
18
|
+
const TINT: Record<EventNotice["tone"], string> = {
|
|
19
|
+
info: "text-cream/45",
|
|
20
|
+
success: "text-accent",
|
|
21
|
+
warning: "text-red",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const ICON: Record<string, () => React.ReactElement> = {
|
|
25
|
+
purchase: CartIcon,
|
|
26
|
+
transaction_complete: CheckCircleIcon,
|
|
27
|
+
freeTrial_start: CheckCircleIcon,
|
|
28
|
+
transaction_abandon: AlertTriangleIcon,
|
|
29
|
+
transaction_fail: AlertTriangleIcon,
|
|
30
|
+
close: CloseIcon,
|
|
31
|
+
restore: RotateIcon,
|
|
32
|
+
open_url: ExternalIcon,
|
|
33
|
+
open_url_external: ExternalIcon,
|
|
34
|
+
open_deep_link: ExternalIcon,
|
|
35
|
+
initiate_web_checkout: CartIcon,
|
|
36
|
+
open_payment_sheet: CartIcon,
|
|
37
|
+
custom_placement: BoltIcon,
|
|
38
|
+
custom: BoltIcon,
|
|
39
|
+
request_permission: BoltIcon,
|
|
40
|
+
request_callback: BoltIcon,
|
|
41
|
+
schedule_notification: BellIcon,
|
|
42
|
+
request_store_review: StarIcon,
|
|
43
|
+
user_attribute_updated: UserIcon,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const Pill = ({ children }: React.PropsWithChildren) => (
|
|
47
|
+
<span className="bg-accent/15 text-accent px-1.5 py-px font-mono text-xs whitespace-nowrap">
|
|
48
|
+
{children}
|
|
49
|
+
</span>
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The icon sits in a box the height of one line of title text, so it centres on
|
|
54
|
+
* the first line whatever the message wraps to.
|
|
55
|
+
*/
|
|
56
|
+
const Toast = ({
|
|
57
|
+
action,
|
|
58
|
+
tone,
|
|
59
|
+
children,
|
|
60
|
+
actions,
|
|
61
|
+
}: React.PropsWithChildren<{
|
|
62
|
+
action: string;
|
|
63
|
+
tone: EventNotice["tone"];
|
|
64
|
+
actions?: React.ReactNode;
|
|
65
|
+
}>) => {
|
|
66
|
+
const Icon = ICON[action] ?? InfoCircleIcon;
|
|
67
|
+
return (
|
|
68
|
+
<div className="bg-panel font-sans w-[344px] border border-cream/15 p-3 shadow-[0_8px_24px_-8px] shadow-black/60">
|
|
69
|
+
<div className="grid grid-cols-[14px_1fr] items-start gap-x-2.5">
|
|
70
|
+
<span className={`flex h-5 items-center justify-center ${TINT[tone]}`}>
|
|
71
|
+
<Icon />
|
|
72
|
+
</span>
|
|
73
|
+
<span className="text-[0.8125rem] leading-5 font-medium text-cream">{children}</span>
|
|
74
|
+
{actions && <div className="col-start-2 mt-2.5 flex justify-end gap-2">{actions}</div>}
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const Notice = ({ notice }: { notice: EventNotice }) => (
|
|
81
|
+
<Toast action={notice.action} tone={notice.tone}>
|
|
82
|
+
Tap triggered a <Pill>{notice.action}</Pill>
|
|
83
|
+
{notice.subject ? (
|
|
84
|
+
<>
|
|
85
|
+
{" for "}
|
|
86
|
+
<Pill>{notice.subject}</Pill>
|
|
87
|
+
</>
|
|
88
|
+
) : (
|
|
89
|
+
" action"
|
|
90
|
+
)}
|
|
91
|
+
.
|
|
92
|
+
</Toast>
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const OUTCOME: Record<string, { action: string; tone: EventNotice["tone"]; label: string }> = {
|
|
96
|
+
completed: { action: "transaction_complete", tone: "success", label: "purchase completed." },
|
|
97
|
+
abandoned: { action: "transaction_abandon", tone: "warning", label: "purchase abandoned." },
|
|
98
|
+
granted: { action: "request_permission", tone: "success", label: "granted." },
|
|
99
|
+
denied: { action: "request_permission", tone: "warning", label: "denied." },
|
|
100
|
+
success: { action: "request_callback", tone: "success", label: "succeeded." },
|
|
101
|
+
failure: { action: "request_callback", tone: "warning", label: "failed." },
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const Outcome = ({ outcome, subject }: { outcome: string; subject: string }) => {
|
|
105
|
+
const spec = OUTCOME[outcome] ?? OUTCOME.completed;
|
|
106
|
+
return (
|
|
107
|
+
<Toast action={spec?.action ?? "purchase"} tone={spec?.tone ?? "info"}>
|
|
108
|
+
<Pill>{subject}</Pill> {spec?.label}
|
|
109
|
+
</Toast>
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const ASK: Record<string, { action: string; verb: string; no: string; yes: string }> = {
|
|
114
|
+
purchase: { action: "purchase", verb: "purchase", no: "Abandon", yes: "Complete" },
|
|
115
|
+
permission: { action: "request_permission", verb: "permission", no: "Deny", yes: "Grant" },
|
|
116
|
+
callback: { action: "request_callback", verb: "callback", no: "Fail", yes: "Succeed" },
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const OUTCOMES: Record<string, readonly [string, string]> = {
|
|
120
|
+
purchase: ["abandoned", "completed"],
|
|
121
|
+
permission: ["denied", "granted"],
|
|
122
|
+
callback: ["failure", "success"],
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const Ask = ({
|
|
126
|
+
kind,
|
|
127
|
+
subject,
|
|
128
|
+
onAnswer,
|
|
129
|
+
}: {
|
|
130
|
+
kind: string;
|
|
131
|
+
subject: string;
|
|
132
|
+
onAnswer: (outcome: string) => void;
|
|
133
|
+
}) => {
|
|
134
|
+
const spec = ASK[kind] ?? ASK.purchase;
|
|
135
|
+
const [no, yes] = OUTCOMES[kind] ?? ["abandoned", "completed"];
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<Toast
|
|
139
|
+
action={spec?.action ?? "purchase"}
|
|
140
|
+
tone="info"
|
|
141
|
+
actions={
|
|
142
|
+
<>
|
|
143
|
+
<button type="button" className="sw-btn" onClick={() => onAnswer(no)}>
|
|
144
|
+
{spec?.no}
|
|
145
|
+
</button>
|
|
146
|
+
<button type="button" className="sw-btn sw-btn--primary" onClick={() => onAnswer(yes)}>
|
|
147
|
+
{spec?.yes}
|
|
148
|
+
</button>
|
|
149
|
+
</>
|
|
150
|
+
}
|
|
151
|
+
>
|
|
152
|
+
Tap triggered a <Pill>{spec?.verb}</Pill> for <Pill>{subject}</Pill>.
|
|
153
|
+
</Toast>
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export const PaywallToasts = ({
|
|
158
|
+
surfaceKey,
|
|
159
|
+
raised = false,
|
|
160
|
+
}: {
|
|
161
|
+
surfaceKey: string;
|
|
162
|
+
raised?: boolean;
|
|
163
|
+
}) => {
|
|
164
|
+
React.useEffect(() => {
|
|
165
|
+
toast.dismiss();
|
|
166
|
+
}, [surfaceKey]);
|
|
167
|
+
|
|
168
|
+
React.useEffect(() => {
|
|
169
|
+
const onMessage = (event: MessageEvent) => {
|
|
170
|
+
if (!isBridgeMessage(event.data)) return;
|
|
171
|
+
|
|
172
|
+
if (event.data.type === "ask") {
|
|
173
|
+
const { id, kind, subject } = event.data;
|
|
174
|
+
const answer = (outcome: string) => {
|
|
175
|
+
(event.source as Window | null)?.postMessage(
|
|
176
|
+
{ channel: STUDIO_CHANNEL, type: "answer", id, outcome },
|
|
177
|
+
"*",
|
|
178
|
+
);
|
|
179
|
+
toast.dismiss(id);
|
|
180
|
+
toast.custom(() => <Outcome outcome={outcome} subject={subject} />);
|
|
181
|
+
};
|
|
182
|
+
toast.custom(() => <Ask kind={kind} subject={subject} onAnswer={answer} />, {
|
|
183
|
+
id,
|
|
184
|
+
duration: Number.POSITIVE_INFINITY,
|
|
185
|
+
});
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const notice = noticeFor(event.data.message);
|
|
190
|
+
if (notice) toast.custom(() => <Notice notice={notice} />);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
window.addEventListener("message", onMessage);
|
|
194
|
+
return () => window.removeEventListener("message", onMessage);
|
|
195
|
+
}, []);
|
|
196
|
+
|
|
197
|
+
return (
|
|
198
|
+
<Toaster
|
|
199
|
+
position="top-center"
|
|
200
|
+
swipeDirections={["top"]}
|
|
201
|
+
offset={raised ? 84 : 52}
|
|
202
|
+
gap={8}
|
|
203
|
+
visibleToasts={3}
|
|
204
|
+
style={{ zIndex: 5 }}
|
|
205
|
+
toastOptions={{ unstyled: true, classNames: { toast: "w-full" } }}
|
|
206
|
+
/>
|
|
207
|
+
);
|
|
208
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { CloseIcon } from "./icons";
|
|
3
|
+
|
|
4
|
+
export const SidePanel = ({
|
|
5
|
+
open,
|
|
6
|
+
title,
|
|
7
|
+
onClose,
|
|
8
|
+
children,
|
|
9
|
+
}: React.PropsWithChildren<{
|
|
10
|
+
open: boolean;
|
|
11
|
+
title: string;
|
|
12
|
+
onClose: () => void;
|
|
13
|
+
}>) => (
|
|
14
|
+
<aside
|
|
15
|
+
className={`flex-none overflow-hidden transition-[width] duration-200 ease-out motion-reduce:transition-none ${
|
|
16
|
+
open ? "w-72" : "w-0"
|
|
17
|
+
}`}
|
|
18
|
+
aria-hidden={!open}
|
|
19
|
+
inert={!open}
|
|
20
|
+
>
|
|
21
|
+
<div className="bg-bar flex h-full w-72 flex-col border-l border-cream/10">
|
|
22
|
+
<div className="flex h-11 flex-none items-center justify-between border-b border-cream/10 px-3">
|
|
23
|
+
<span className="text-[0.8125rem] font-medium text-cream">{title}</span>
|
|
24
|
+
<button
|
|
25
|
+
type="button"
|
|
26
|
+
className="sw-btn sw-btn--icon border-transparent"
|
|
27
|
+
aria-label={`Close ${title.toLowerCase()}`}
|
|
28
|
+
onClick={onClose}
|
|
29
|
+
>
|
|
30
|
+
<CloseIcon />
|
|
31
|
+
</button>
|
|
32
|
+
</div>
|
|
33
|
+
<div className="flex-1 overflow-y-auto px-4 py-2">{children}</div>
|
|
34
|
+
</div>
|
|
35
|
+
</aside>
|
|
36
|
+
);
|