@superwall/studio 1.2.0 → 1.3.0-beta.3

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 (71) hide show
  1. package/README.md +27 -14
  2. package/dist/assets/index-DyLb5fiB.js +11 -0
  3. package/dist/index.html +1 -1
  4. package/dist/lib/StudioApp.d.ts +2 -3
  5. package/dist/lib/components/PaywallToasts.d.ts +3 -0
  6. package/dist/lib/components/SurfaceCard.d.ts +4 -2
  7. package/dist/lib/components/icons.d.ts +0 -2
  8. package/dist/lib/components/{ActionMenu.d.ts → menus/ActionMenu.d.ts} +2 -1
  9. package/dist/lib/components/{CompareMenu.d.ts → menus/CompareMenu.d.ts} +1 -1
  10. package/dist/lib/components/{VariablesPanel.d.ts → panels/VariablesPanel.d.ts} +3 -3
  11. package/dist/lib/components/{DeviceChrome.d.ts → preview/DeviceChrome.d.ts} +1 -1
  12. package/dist/lib/components/{DeviceFrame.d.ts → preview/DeviceFrame.d.ts} +5 -2
  13. package/dist/lib/components/preview/HostedIframe.d.ts +7 -0
  14. package/dist/lib/index.d.ts +12 -6
  15. package/dist/lib/index.js +1467 -945
  16. package/dist/lib/lib/actions.d.ts +1 -1
  17. package/dist/lib/lib/contract.d.ts +33 -0
  18. package/dist/lib/lib/devices.d.ts +1 -1
  19. package/dist/lib/lib/host.d.ts +15 -0
  20. package/dist/lib/lib/notices.d.ts +9 -0
  21. package/dist/lib/lib/preview.d.ts +2 -4
  22. package/dist/lib/lib/project.d.ts +2 -1
  23. package/dist/lib/lib/steps.d.ts +5 -0
  24. package/dist/lib/lib/surfaces.d.ts +35 -0
  25. package/dist/lib/lib/variables.d.ts +12 -0
  26. package/dist/lib/products.js +26 -12
  27. package/dist/lib/screens/SurfaceEditor.d.ts +1 -1
  28. package/dist/lib/screens/SurfaceList.d.ts +1 -1
  29. package/package.json +5 -1
  30. package/src/StudioApp.tsx +6 -10
  31. package/src/components/PaywallToasts.tsx +162 -86
  32. package/src/components/SurfaceCard.tsx +9 -2
  33. package/src/components/icons.tsx +0 -13
  34. package/src/components/{ActionMenu.tsx → menus/ActionMenu.tsx} +3 -3
  35. package/src/components/{CompareMenu.tsx → menus/CompareMenu.tsx} +2 -2
  36. package/src/components/{SidePanel.tsx → panels/SidePanel.tsx} +1 -1
  37. package/src/components/{VariablesPanel.tsx → panels/VariablesPanel.tsx} +4 -4
  38. package/src/components/{DeviceChrome.tsx → preview/DeviceChrome.tsx} +1 -1
  39. package/src/components/{DeviceFrame.tsx → preview/DeviceFrame.tsx} +14 -55
  40. package/src/components/preview/HostedIframe.tsx +46 -0
  41. package/src/index.ts +24 -12
  42. package/src/lib/actions.ts +1 -1
  43. package/src/lib/contract.ts +30 -0
  44. package/src/lib/devices.ts +5 -4
  45. package/src/lib/host.ts +80 -0
  46. package/src/lib/notices.ts +63 -0
  47. package/src/lib/preview.ts +8 -40
  48. package/src/lib/products.ts +20 -1
  49. package/src/lib/project.ts +2 -1
  50. package/src/lib/steps.ts +20 -0
  51. package/src/lib/surfaces.ts +41 -0
  52. package/src/lib/time.ts +1 -2
  53. package/src/lib/variables.ts +13 -0
  54. package/src/main.tsx +3 -3
  55. package/src/screens/SurfaceEditor.tsx +77 -66
  56. package/src/screens/SurfaceList.tsx +17 -7
  57. package/src/vite-env.d.ts +1 -0
  58. package/dist/assets/index-CVKQ82YR.js +0 -11
  59. package/dist/lib/lib/events.d.ts +0 -27
  60. package/dist/lib/lib/types.d.ts +0 -77
  61. package/src/lib/events.ts +0 -86
  62. package/src/lib/types.ts +0 -87
  63. /package/dist/lib/components/{StudioShell.d.ts → chrome/StudioShell.d.ts} +0 -0
  64. /package/dist/lib/components/{Toolbar.d.ts → chrome/Toolbar.d.ts} +0 -0
  65. /package/dist/lib/components/{TopBar.d.ts → chrome/TopBar.d.ts} +0 -0
  66. /package/dist/lib/components/{MissingProducts.d.ts → panels/MissingProducts.d.ts} +0 -0
  67. /package/dist/lib/components/{SidePanel.d.ts → panels/SidePanel.d.ts} +0 -0
  68. /package/src/components/{StudioShell.tsx → chrome/StudioShell.tsx} +0 -0
  69. /package/src/components/{Toolbar.tsx → chrome/Toolbar.tsx} +0 -0
  70. /package/src/components/{TopBar.tsx → chrome/TopBar.tsx} +0 -0
  71. /package/src/components/{MissingProducts.tsx → panels/MissingProducts.tsx} +0 -0
@@ -6,12 +6,6 @@ const Icon = ({ children }: React.PropsWithChildren) => (
6
6
  </svg>
7
7
  );
8
8
 
9
- export const ChevronLeftIcon = () => (
10
- <Icon>
11
- <path d="m15 18-6-6 6-6" />
12
- </Icon>
13
- );
14
-
15
9
  export const RotateIcon = () => (
16
10
  <Icon>
17
11
  <path d="M21 12a9 9 0 1 1-2.64-6.36" />
@@ -40,13 +34,6 @@ export const MoonIcon = () => (
40
34
  </Icon>
41
35
  );
42
36
 
43
- export const ResetIcon = () => (
44
- <Icon>
45
- <path d="M3 12a9 9 0 1 0 2.64-6.36" />
46
- <path d="M3 3v6h6" />
47
- </Icon>
48
- );
49
-
50
37
  export const SlidersIcon = () => (
51
38
  <Icon>
52
39
  <path d="M21 4h-7M10 4H3M21 12h-9M8 12H3M21 20h-5M12 20H3" />
@@ -1,12 +1,12 @@
1
1
  import * as React from "react";
2
- import { ChevronDownIcon } from "./icons";
2
+ import { ChevronDownIcon } from "../icons";
3
3
  import {
4
4
  ACTION_STEPS,
5
5
  STEP_LABELS,
6
- type StudioActionName,
7
6
  type StudioActionStep,
8
7
  type StudioStepState,
9
- } from "../lib/types";
8
+ } from "../../lib/steps";
9
+ import { type StudioActionName } from "../../lib/surfaces";
10
10
 
11
11
  const ACTIONS: readonly { name: StudioActionName; label: string; hint: string }[] = [
12
12
  { name: "push", label: "Push", hint: "New version, nothing live changes" },
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
- import { ColumnsIcon } from "./icons";
3
- import type { CompareTarget } from "../lib/types";
2
+ import { ColumnsIcon } from "../icons";
3
+ import type { CompareTarget } from "../../lib/surfaces";
4
4
 
5
5
  export type CompareOption = {
6
6
  key: string;
@@ -1,5 +1,5 @@
1
1
  import * as React from "react";
2
- import { CloseIcon } from "./icons";
2
+ import { CloseIcon } from "../icons";
3
3
 
4
4
  export const SidePanel = ({
5
5
  open,
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
- import { ChevronDownIcon } from "./icons";
3
- import type { PreviewOverrides } from "../lib/types";
2
+ import { ChevronDownIcon } from "../icons";
3
+ import type { VariableOverrides } from "../../lib/variables";
4
4
 
5
5
  export type VariableGroup = "user" | "device" | "params" | "products";
6
6
 
@@ -157,9 +157,9 @@ export const VariablesPanel = ({
157
157
  groups,
158
158
  onChange,
159
159
  }: {
160
- overrides: PreviewOverrides;
160
+ overrides: VariableOverrides;
161
161
  groups: VariableGroups;
162
- onChange: (overrides: PreviewOverrides) => void;
162
+ onChange: (overrides: VariableOverrides) => void;
163
163
  }) => {
164
164
  const update = (group: VariableGroup, path: string, value: unknown) => {
165
165
  if (group !== "products") {
@@ -1,4 +1,4 @@
1
- import type { DeviceChromeSpec } from "../lib/devices";
1
+ import type { DeviceChromeSpec } from "../../lib/devices";
2
2
 
3
3
  const CellularIcon = () => (
4
4
  <svg width="19" height="12" viewBox="0 0 19 12" fill="currentColor" aria-hidden="true">
@@ -1,13 +1,13 @@
1
1
  import * as React from "react";
2
+ import type { HostDelegate, HostState } from "@superwall/host";
2
3
  import { DeviceChrome } from "./DeviceChrome";
3
- import type { DeviceChromeSpec } from "../lib/devices";
4
+ import { HostedIframe } from "./HostedIframe";
5
+ import type { DeviceChromeSpec } from "../../lib/devices";
4
6
 
5
7
  const FRAME_MARGIN = 96;
6
8
 
7
9
  const SWAP_DELAY_MS = 250;
8
10
 
9
- type PreviewWindow = Window & { __swPreviewApply?: (search: string) => void };
10
-
11
11
  const useFitScale = (
12
12
  container: React.RefObject<HTMLElement | null>,
13
13
  width: number,
@@ -63,32 +63,18 @@ export const DevicePlaceholder = ({
63
63
  };
64
64
 
65
65
  type Frame = {
66
- mountSrc: string;
67
- currentSrc: string;
66
+ src: string;
68
67
  generation: number;
69
68
  ready: boolean;
70
69
  };
71
70
 
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
71
  export const DeviceFrame = ({
88
72
  width,
89
73
  height,
90
74
  title,
91
75
  src,
76
+ hostState,
77
+ delegate,
92
78
  stageRef,
93
79
  reloadKey = 0,
94
80
  chrome,
@@ -99,6 +85,8 @@ export const DeviceFrame = ({
99
85
  height: number;
100
86
  title: string;
101
87
  src: string;
88
+ hostState: HostState;
89
+ delegate: HostDelegate;
102
90
  stageRef: React.RefObject<HTMLElement | null>;
103
91
  reloadKey?: number;
104
92
  chrome?: DeviceChromeSpec;
@@ -108,43 +96,16 @@ export const DeviceFrame = ({
108
96
  const scale = useFitScale(stageRef, width, height);
109
97
 
110
98
  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>());
99
+ const [frames, setFrames] = React.useState<Frame[]>([{ src, generation: 0, ready: true }]);
117
100
 
118
101
  const mounted = React.useRef(false);
119
- const lastReload = React.useRef(reloadKey);
120
102
  React.useEffect(() => {
121
103
  if (!mounted.current) {
122
104
  mounted.current = true;
123
105
  return;
124
106
  }
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
107
  generation.current += 1;
142
- const next: Frame = {
143
- mountSrc: src,
144
- currentSrc: src,
145
- generation: generation.current,
146
- ready: false,
147
- };
108
+ const next: Frame = { src, generation: generation.current, ready: false };
148
109
  setFrames((existing) => [...existing.filter((frame) => frame.ready).slice(-1), next]);
149
110
  }, [src, reloadKey]);
150
111
 
@@ -172,17 +133,15 @@ export const DeviceFrame = ({
172
133
  style={{ width: width * scale, height: height * scale, borderRadius: radius }}
173
134
  >
174
135
  {frames.map((frame) => (
175
- <iframe
136
+ <HostedIframe
176
137
  key={frame.generation}
177
- ref={(node) => {
178
- if (node) nodes.current.set(frame.generation, node);
179
- else nodes.current.delete(frame.generation);
180
- }}
138
+ hostState={hostState}
139
+ delegate={delegate}
181
140
  className={`absolute inset-0 origin-top-left border-0 bg-white transition-opacity duration-150 ${
182
141
  frame === visible ? "opacity-100" : "opacity-0"
183
142
  }`}
184
143
  title={title}
185
- src={frame.mountSrc}
144
+ src={frame.src}
186
145
  width={width}
187
146
  height={height}
188
147
  style={{ transform: `scale(${scale})` }}
@@ -0,0 +1,46 @@
1
+ import * as React from "react";
2
+ import {
3
+ createWindowHost,
4
+ diffHostState,
5
+ type HostDelegate,
6
+ type HostState,
7
+ type PaywallHost,
8
+ } from "@superwall/host";
9
+
10
+ type Hosted = { host: PaywallHost; detach: () => void; sent: HostState };
11
+
12
+ const useHostedWindow = (hostState: HostState, delegate: HostDelegate) => {
13
+ const hosted = React.useRef<Hosted | undefined>(undefined);
14
+ const initial = React.useRef({ hostState, delegate });
15
+
16
+ const ref = React.useCallback((node: HTMLIFrameElement | null) => {
17
+ hosted.current?.detach();
18
+ hosted.current = undefined;
19
+ if (!node?.contentWindow) return;
20
+ const { host, detach } = createWindowHost({
21
+ target: node.contentWindow,
22
+ state: initial.current.hostState,
23
+ delegate: initial.current.delegate,
24
+ });
25
+ hosted.current = { host, detach, sent: initial.current.hostState };
26
+ }, []);
27
+
28
+ React.useEffect(() => {
29
+ initial.current = { hostState, delegate };
30
+ const current = hosted.current;
31
+ if (!current) return;
32
+ current.host.update(diffHostState(current.sent, hostState));
33
+ current.sent = hostState;
34
+ }, [hostState, delegate]);
35
+
36
+ return ref;
37
+ };
38
+
39
+ export type HostedIframeProps = React.ComponentPropsWithoutRef<"iframe"> & {
40
+ hostState: HostState;
41
+ delegate: HostDelegate;
42
+ };
43
+
44
+ export const HostedIframe = ({ hostState, delegate, ...iframe }: HostedIframeProps) => (
45
+ <iframe {...iframe} ref={useHostedWindow(hostState, delegate)} />
46
+ );
package/src/index.ts CHANGED
@@ -3,26 +3,38 @@ import "./styles.css";
3
3
  export { StudioApp } from "./StudioApp";
4
4
  export { SurfaceEditor } from "./screens/SurfaceEditor";
5
5
  export { SurfaceList } from "./screens/SurfaceList";
6
- export { DeviceFrame } from "./components/DeviceFrame";
7
- export { StudioShell } from "./components/StudioShell";
8
- export { TopBar } from "./components/TopBar";
9
- export { VariablesPanel } from "./components/VariablesPanel";
6
+ export { DeviceFrame } from "./components/preview/DeviceFrame";
7
+ export { HostedIframe } from "./components/preview/HostedIframe";
8
+ export { PaywallToasts, createStudioDelegate, useStudioDelegate } from "./components/PaywallToasts";
9
+ export { StudioShell } from "./components/chrome/StudioShell";
10
+ export { TopBar } from "./components/chrome/TopBar";
11
+ export { VariablesPanel } from "./components/panels/VariablesPanel";
10
12
  export { Segmented, Select, Switch } from "./components/controls";
11
13
  export { DEFAULT_DEVICE_ID, deviceById, STUDIO_DEVICES } from "./lib/devices";
12
14
  export type { StudioDevice } from "./lib/devices";
15
+ export { hostStateFor, studioDefaults } from "./lib/host";
13
16
  export {
14
- buildPreviewPath,
15
- snapshotPreviewUrl,
16
- withOverrides,
17
- withPreviewOptIn,
18
- } from "./lib/preview";
17
+ createHostState,
18
+ diffHostState,
19
+ hasIntroductoryOffer,
20
+ silentDelegate,
21
+ type HostDelegate,
22
+ type HostProduct,
23
+ type HostState,
24
+ } from "@superwall/host";
25
+ export type { HostInputs } from "./lib/host";
26
+ export { buildPreviewPath, hostedUrl, snapshotPreviewUrl, surfacePath } from "./lib/preview";
19
27
  export { parseProjectPayload, type StudioProjectPayload } from "./lib/project";
20
28
  export type {
21
- PreviewOverrides,
29
+ AppScheme,
22
30
  PreviewUrlBuilder,
23
- SampleDefaults,
24
31
  Samples,
32
+ StudioActionName,
33
+ StudioActionProgress,
34
+ StudioActionResult,
25
35
  StudioSource,
36
+ StudioStatus,
26
37
  StudioSurface,
27
38
  StudioSurfaceKind,
28
- } from "./lib/types";
39
+ } from "./lib/surfaces";
40
+ export type { SampleDefaults, VariableOverrides } from "./lib/variables";
@@ -5,7 +5,7 @@ import type {
5
5
  StudioActionResult,
6
6
  StudioStatus,
7
7
  StudioSurface,
8
- } from "./types";
8
+ } from "./surfaces";
9
9
 
10
10
  export const runAction = async (
11
11
  surface: StudioSurface,
@@ -0,0 +1,30 @@
1
+ export type StudioActionName = "push" | "promote" | "publish";
2
+
3
+ export type StudioActionResult =
4
+ | { ok: true; version: number; live: boolean }
5
+ | { ok: false; error: string };
6
+
7
+ export type StudioActionProgress = (step: string, state: "start" | "done") => void;
8
+
9
+ export type StudioStatus = {
10
+ paywallId?: string;
11
+ identifier?: string;
12
+ liveUrl?: string;
13
+ active?: boolean;
14
+ updatedAt?: string;
15
+ unreachable?: string;
16
+ };
17
+
18
+ export type Samples = {
19
+ device?: Record<string, unknown>;
20
+ user?: Record<string, unknown>;
21
+ products?: Record<string, Record<string, unknown>>;
22
+ live?: { device: boolean; user: boolean };
23
+ unreachable?: string;
24
+ };
25
+
26
+ export type AppScheme = {
27
+ scheme?: string;
28
+ source?: "dashboard" | "app";
29
+ blocksLocalNetworking?: string;
30
+ };
@@ -25,7 +25,7 @@ const IPHONE_ISLAND: DeviceChromeSpec = {
25
25
  homeIndicator: true,
26
26
  };
27
27
 
28
- export const STUDIO_DEVICES: readonly StudioDevice[] = [
28
+ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
29
29
  {
30
30
  id: "iphone-se",
31
31
  name: "iPhone SE",
@@ -103,7 +103,8 @@ export const DEFAULT_DEVICE_ID = "iphone-17";
103
103
  export const RESPONSIVE_ID = "responsive";
104
104
  export const RESPONSIVE_DEFAULT = { width: 393, height: 852 };
105
105
 
106
+ const DEFAULT_DEVICE: StudioDevice =
107
+ STUDIO_DEVICES.find((device) => device.id === DEFAULT_DEVICE_ID) ?? STUDIO_DEVICES[0];
108
+
106
109
  export const deviceById = (id: string): StudioDevice =>
107
- STUDIO_DEVICES.find((device) => device.id === id) ??
108
- STUDIO_DEVICES.find((device) => device.id === DEFAULT_DEVICE_ID) ??
109
- STUDIO_DEVICES[0];
110
+ STUDIO_DEVICES.find((device) => device.id === id) ?? DEFAULT_DEVICE;
@@ -0,0 +1,80 @@
1
+ import {
2
+ createHostState,
3
+ DEFAULT_PARAMS,
4
+ DEFAULT_USER,
5
+ defaultDevice,
6
+ EXAMPLE_PRODUCT_VARIABLES,
7
+ type HostProduct,
8
+ type HostState,
9
+ type HostVariables,
10
+ } from "@superwall/host";
11
+ import type { Samples, StudioSurface } from "./surfaces";
12
+ import type { SampleDefaults, VariableOverrides } from "./variables";
13
+
14
+ export type HostInputs = {
15
+ surface: StudioSurface;
16
+ defaults: SampleDefaults;
17
+ sourceSamples?: SampleDefaults;
18
+ samples: Samples;
19
+ frame?: Record<string, unknown>;
20
+ theme: "light" | "dark";
21
+ locale?: string;
22
+ edited: VariableOverrides;
23
+ };
24
+
25
+ export const studioDefaults = (): SampleDefaults => ({
26
+ device: defaultDevice(),
27
+ user: DEFAULT_USER,
28
+ params: DEFAULT_PARAMS,
29
+ });
30
+
31
+ export const hostStateFor = ({
32
+ surface,
33
+ defaults,
34
+ sourceSamples,
35
+ samples,
36
+ frame,
37
+ theme,
38
+ locale,
39
+ edited,
40
+ }: HostInputs): HostState => {
41
+ const device: HostVariables = {
42
+ ...defaults.device,
43
+ ...sourceSamples?.device,
44
+ ...samples.device,
45
+ ...frame,
46
+ interfaceStyle: theme === "dark" ? "Dark" : "Light",
47
+ ...(locale ? { deviceLocale: locale, deviceLanguageCode: locale.split(/[-_]/)[0] } : {}),
48
+ ...edited.device,
49
+ };
50
+ const user: HostVariables = {
51
+ ...defaults.user,
52
+ ...sourceSamples?.user,
53
+ ...samples.user,
54
+ ...edited.user,
55
+ };
56
+ const params: HostVariables = {
57
+ ...defaults.params,
58
+ ...sourceSamples?.params,
59
+ ...edited.params,
60
+ };
61
+ const products: HostProduct[] = Object.entries(surface.products ?? {}).map(
62
+ ([reference, productId]) => ({
63
+ reference,
64
+ productId,
65
+ variables: {
66
+ ...(samples.products?.[productId] ?? EXAMPLE_PRODUCT_VARIABLES),
67
+ ...edited.products?.[reference],
68
+ },
69
+ }),
70
+ );
71
+ return createHostState({
72
+ paywallId: surface.paywallId ?? "0",
73
+ paywallIdentifier: surface.paywallIdentifier ?? surface.id,
74
+ device,
75
+ user,
76
+ params,
77
+ products,
78
+ introductoryOfferEligible: edited.introductoryOfferEligible,
79
+ });
80
+ };
@@ -0,0 +1,63 @@
1
+ import type { TIncomingMessage, TOutgoingMessage } from "@superwall/host";
2
+
3
+ export type PaywallEventName = TOutgoingMessage["event_name"];
4
+
5
+ export type PaywallMessageName = PaywallEventName | TIncomingMessage["event_name"];
6
+
7
+ export type EventNotice = {
8
+ action: PaywallEventName;
9
+ subject?: string;
10
+ tone: "info" | "success" | "warning";
11
+ };
12
+
13
+ const SILENT: ReadonlySet<PaywallEventName> = new Set<PaywallEventName>([
14
+ "ping",
15
+ "page_view",
16
+ "page_styles",
17
+ "has_set_dom",
18
+ "haptic_feedback",
19
+ "var_click",
20
+ "purchase",
21
+ "restore",
22
+ ]);
23
+
24
+ const SUBJECT: Partial<Record<PaywallEventName, readonly string[]>> = {
25
+ open_url: ["url"],
26
+ open_url_external: ["url"],
27
+ open_deep_link: ["link"],
28
+ custom_placement: ["name"],
29
+ custom: ["name", "data"],
30
+ request_permission: ["permission_type"],
31
+ request_callback: ["name"],
32
+ schedule_notification: ["title"],
33
+ discount_redemption_result: ["code"],
34
+ };
35
+
36
+ const WARNING: ReadonlySet<PaywallEventName> = new Set<PaywallEventName>(["close"]);
37
+
38
+ const SUCCESS: ReadonlySet<PaywallEventName> = new Set<PaywallEventName>([
39
+ "stripe_checkout_complete",
40
+ "post_checkout_complete",
41
+ ]);
42
+
43
+ const subjectOf = (event: TOutgoingMessage): string | undefined => {
44
+ for (const key of SUBJECT[event.event_name] ?? []) {
45
+ const value = Reflect.get(event, key);
46
+ if (typeof value === "string" && value.length > 0) return value;
47
+ }
48
+ return undefined;
49
+ };
50
+
51
+ export const noticeFor = (event: TOutgoingMessage): EventNotice | undefined => {
52
+ if (SILENT.has(event.event_name)) return undefined;
53
+
54
+ return {
55
+ action: event.event_name,
56
+ subject: subjectOf(event),
57
+ tone: WARNING.has(event.event_name)
58
+ ? "warning"
59
+ : SUCCESS.has(event.event_name)
60
+ ? "success"
61
+ : "info",
62
+ };
63
+ };
@@ -1,49 +1,17 @@
1
- import type { PreviewOverrides, PreviewUrlBuilder, StudioSurface } from "./types";
2
-
3
- const overrideParams = (overrides: PreviewOverrides): URLSearchParams => {
4
- const params = new URLSearchParams();
5
- if (overrides.theme) params.set("theme", overrides.theme);
6
- if (overrides.locale) params.set("locale", overrides.locale);
7
- for (const [key, group] of [
8
- ["sw_device", overrides.device],
9
- ["sw_user", overrides.user],
10
- ["sw_params", overrides.params],
11
- ["sw_products", overrides.products],
12
- ] as const) {
13
- if (group && Object.keys(group).length > 0) params.set(key, JSON.stringify(group));
14
- }
15
- if (overrides.trialEligible !== undefined) {
16
- params.set("sw_trial", overrides.trialEligible ? "1" : "0");
17
- }
18
- return params;
19
- };
1
+ import type { PreviewUrlBuilder, StudioSurface } from "./surfaces";
20
2
 
21
3
  export const surfacePath = (surface: StudioSurface): string =>
22
4
  `${surface.project ? `/${surface.project}` : ""}/${surface.kind}/${surface.id}`;
23
5
 
24
- export const devicePreviewPath = (surface: StudioSurface): string =>
25
- `/preview${surfacePath(surface)}?sw_preview=1`;
26
-
27
- export const buildPreviewPath: PreviewUrlBuilder = (
28
- surface: StudioSurface,
29
- overrides: PreviewOverrides,
30
- ): string => {
31
- const query = overrideParams(overrides).toString();
32
- return `/preview${surfacePath(surface)}${query ? `?${query}` : ""}`;
6
+ export const hostedUrl = (url: string): string => {
7
+ if (/[?&]transport=editor(?:&|$)/.test(url)) return url;
8
+ return `${url}${url.includes("?") ? "&" : "?"}transport=editor`;
33
9
  };
34
10
 
35
- export const withOverrides = (url: string, overrides: PreviewOverrides): string => {
36
- const query = overrideParams(overrides).toString();
37
- if (!query) return url;
38
- return `${url}${url.includes("?") ? "&" : "?"}${query}`;
39
- };
40
-
41
- export const withPreviewOptIn = (url: string): string => {
42
- if (/[?&]sw_preview=/.test(url)) return url;
43
- return `${url}${url.includes("?") ? "&" : "?"}sw_preview=1`;
44
- };
11
+ export const buildPreviewPath: PreviewUrlBuilder = (surface) =>
12
+ hostedUrl(`/preview${surfacePath(surface)}`);
45
13
 
46
14
  export const snapshotPreviewUrl =
47
15
  (urlOf: (surface: StudioSurface) => string): PreviewUrlBuilder =>
48
- (surface, overrides) =>
49
- withOverrides(withPreviewOptIn(urlOf(surface)), overrides);
16
+ (surface) =>
17
+ hostedUrl(urlOf(surface));
@@ -59,6 +59,25 @@ const PERIODLY: Record<string, string> = {
59
59
  year: "yearly",
60
60
  };
61
61
 
62
+ const PERIOD_UNITS: Record<string, { days: number; weeks: number; months: number; years: number }> =
63
+ {
64
+ day: { days: 1, weeks: 1 / 7, months: 1 / 30, years: 1 / 365 },
65
+ week: { days: 7, weeks: 1, months: 1 / 4, years: 1 / 52 },
66
+ month: { days: 30, weeks: 4, months: 1, years: 1 / 12 },
67
+ year: { days: 365, weeks: 52, months: 12, years: 1 },
68
+ };
69
+
70
+ const periodCounts = (period: string, count: number): Record<string, number> | undefined => {
71
+ const units = PERIOD_UNITS[period];
72
+ if (!units) return undefined;
73
+ return {
74
+ periodDays: units.days * count,
75
+ periodWeeks: Math.round(units.weeks * count),
76
+ periodMonths: Math.floor(units.months * count),
77
+ periodYears: Math.floor(units.years * count),
78
+ };
79
+ };
80
+
62
81
  export const productVariables = (product: DashboardProduct): Record<string, unknown> => {
63
82
  const variables: Record<string, unknown> = {};
64
83
  if (product.identifier) variables.identifier = product.identifier;
@@ -79,7 +98,7 @@ export const productVariables = (product: DashboardProduct): Record<string, unkn
79
98
  const count = product.subscription?.period_count ?? 1;
80
99
  variables.period = period;
81
100
  variables.periodly = PERIODLY[period] ?? `${period}ly`;
82
- variables[`period${period.charAt(0).toUpperCase()}${period.slice(1)}s`] = count;
101
+ Object.assign(variables, periodCounts(period, count) ?? {});
83
102
 
84
103
  const perYear = (PER_YEAR[period] ?? 1) / count;
85
104
  variables.yearlyPrice = money(value * perYear, currency);
@@ -1,4 +1,5 @@
1
- import type { SampleDefaults, StudioSurface, StudioSurfaceKind } from "./types";
1
+ import type { StudioSurface, StudioSurfaceKind } from "./surfaces";
2
+ import type { SampleDefaults } from "./variables";
2
3
 
3
4
  export type StudioProjectPayload = {
4
5
  title: string;
@@ -0,0 +1,20 @@
1
+ import type { StudioActionName } from "./contract";
2
+
3
+ export type StudioStepState = "pending" | "active" | "done" | "failed";
4
+
5
+ export type StudioActionStep = "link" | "build" | "assets" | "push" | "source" | "promote";
6
+
7
+ export const ACTION_STEPS: Record<StudioActionName, StudioActionStep[]> = {
8
+ push: ["link", "build", "assets", "push", "source"],
9
+ publish: ["link", "build", "assets", "push", "source", "promote"],
10
+ promote: ["link", "promote"],
11
+ };
12
+
13
+ export const STEP_LABELS: Record<StudioActionStep, string> = {
14
+ link: "Find paywall",
15
+ build: "Build snapshot",
16
+ assets: "Sync assets",
17
+ push: "Push version",
18
+ source: "Push source",
19
+ promote: "Promote to live",
20
+ };