@farcaster/snap 1.15.4 → 1.16.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/dist/constants.d.ts +8 -0
- package/dist/constants.js +9 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/react/components/badge.js +2 -3
- package/dist/react/index.d.ts +9 -4
- package/dist/react/index.js +9 -228
- package/dist/react/snap-view-core.d.ts +11 -0
- package/dist/react/snap-view-core.js +224 -0
- package/dist/react/v1/snap-view.d.ts +14 -0
- package/dist/react/v1/snap-view.js +9 -0
- package/dist/react/v2/snap-view.d.ts +21 -0
- package/dist/react/v2/snap-view.js +76 -0
- package/dist/react-native/components/snap-badge.js +3 -3
- package/dist/react-native/index.d.ts +15 -45
- package/dist/react-native/index.js +10 -166
- package/dist/react-native/snap-view-core.d.ts +11 -0
- package/dist/react-native/snap-view-core.js +153 -0
- package/dist/react-native/types.d.ts +41 -0
- package/dist/react-native/types.js +1 -0
- package/dist/react-native/v1/snap-view.d.ts +22 -0
- package/dist/react-native/v1/snap-view.js +31 -0
- package/dist/react-native/v2/snap-view.d.ts +31 -0
- package/dist/react-native/v2/snap-view.js +101 -0
- package/dist/schemas.d.ts +15 -9
- package/dist/schemas.js +7 -8
- package/dist/server/parseRequest.d.ts +7 -0
- package/dist/server/parseRequest.js +27 -0
- package/dist/ui/schema.js +1 -1
- package/dist/validator.d.ts +3 -2
- package/dist/validator.js +193 -2
- package/llms.txt +9 -0
- package/package.json +1 -1
- package/src/constants.ts +11 -1
- package/src/index.ts +8 -0
- package/src/react/accent-context.tsx +1 -1
- package/src/react/components/badge.tsx +2 -3
- package/src/react/index.tsx +37 -330
- package/src/react/snap-view-core.tsx +340 -0
- package/src/react/v1/snap-view.tsx +50 -0
- package/src/react/v2/snap-view.tsx +168 -0
- package/src/react-native/components/snap-badge.tsx +3 -3
- package/src/react-native/index.tsx +47 -267
- package/src/react-native/snap-view-core.tsx +209 -0
- package/src/react-native/types.ts +37 -0
- package/src/react-native/v1/snap-view.tsx +108 -0
- package/src/react-native/v2/snap-view.tsx +239 -0
- package/src/schemas.ts +9 -10
- package/src/server/parseRequest.ts +39 -0
- package/src/ui/schema.ts +1 -1
- package/src/validator.ts +240 -2
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useMemo } from "react";
|
|
4
|
+
import { validateSnapResponse } from "../../validator.js";
|
|
5
|
+
import { SnapViewCore } from "../snap-view-core.js";
|
|
6
|
+
const SNAP_MAX_HEIGHT = 500;
|
|
7
|
+
const SNAP_WARNING_HEIGHT = 700;
|
|
8
|
+
// ─── Default validation error fallback ────────────────
|
|
9
|
+
function SnapValidationFallback({ appearance, message, }) {
|
|
10
|
+
const isDark = appearance === "dark";
|
|
11
|
+
return (_jsx("div", { style: {
|
|
12
|
+
width: "100%",
|
|
13
|
+
padding: 16,
|
|
14
|
+
display: "flex",
|
|
15
|
+
alignItems: "center",
|
|
16
|
+
justifyContent: "center",
|
|
17
|
+
color: isDark ? "rgba(255,255,255,0.5)" : "rgba(0,0,0,0.4)",
|
|
18
|
+
fontSize: 14,
|
|
19
|
+
}, children: _jsx("span", { children: message ? `Unable to render snap: ${message}` : "Unable to render snap" }) }));
|
|
20
|
+
}
|
|
21
|
+
// ─── SnapViewV2 ──────────────────────────────────────
|
|
22
|
+
export function SnapViewV2({ snap, handlers, loading = false, appearance = "dark", onValidationError, validationErrorFallback, }) {
|
|
23
|
+
const validation = useMemo(() => validateSnapResponse(snap), [snap]);
|
|
24
|
+
const valid = validation.valid;
|
|
25
|
+
const validationMessage = validation.issues[0]?.message;
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!valid) {
|
|
28
|
+
if (onValidationError) {
|
|
29
|
+
onValidationError(validation);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
// eslint-disable-next-line no-console
|
|
33
|
+
console.warn("[Snap] validation issues:", validation.issues);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}, [valid, validation, onValidationError]);
|
|
37
|
+
if (!valid) {
|
|
38
|
+
if (validationErrorFallback === null)
|
|
39
|
+
return null;
|
|
40
|
+
return _jsx(_Fragment, { children: validationErrorFallback ?? _jsx(SnapValidationFallback, { appearance: appearance, message: validationMessage }) });
|
|
41
|
+
}
|
|
42
|
+
return (_jsx(SnapViewCore, { snap: snap, handlers: handlers, loading: loading, appearance: appearance }));
|
|
43
|
+
}
|
|
44
|
+
// ─── SnapCardV2 ──────────────────────────────────────
|
|
45
|
+
export function SnapCardV2({ snap, handlers, loading = false, appearance = "dark", maxWidth = 480, showOverflowWarning = false, onValidationError, validationErrorFallback, }) {
|
|
46
|
+
const maxHeight = showOverflowWarning ? SNAP_WARNING_HEIGHT : SNAP_MAX_HEIGHT;
|
|
47
|
+
const bg = appearance === "dark" ? "rgba(0,0,0,0.85)" : "rgba(255,255,255,0.9)";
|
|
48
|
+
return (_jsxs("div", { style: {
|
|
49
|
+
position: "relative",
|
|
50
|
+
width: "100%",
|
|
51
|
+
maxWidth,
|
|
52
|
+
maxHeight,
|
|
53
|
+
overflow: "hidden",
|
|
54
|
+
}, children: [_jsx(SnapViewV2, { snap: snap, handlers: handlers, loading: loading, appearance: appearance, onValidationError: onValidationError, validationErrorFallback: validationErrorFallback }), showOverflowWarning && (_jsxs("div", { style: {
|
|
55
|
+
position: "absolute",
|
|
56
|
+
top: SNAP_MAX_HEIGHT,
|
|
57
|
+
left: 0,
|
|
58
|
+
right: 0,
|
|
59
|
+
bottom: 0,
|
|
60
|
+
pointerEvents: "none",
|
|
61
|
+
zIndex: 10,
|
|
62
|
+
}, children: [_jsx("div", { style: { borderTop: "1px dashed rgba(255,100,100,0.6)", position: "relative" }, children: _jsxs("span", { style: {
|
|
63
|
+
position: "absolute",
|
|
64
|
+
top: -10,
|
|
65
|
+
right: 0,
|
|
66
|
+
fontSize: 10,
|
|
67
|
+
fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace",
|
|
68
|
+
color: "rgba(255,100,100,0.7)",
|
|
69
|
+
background: bg,
|
|
70
|
+
padding: "1px 4px",
|
|
71
|
+
borderRadius: 3,
|
|
72
|
+
}, children: [SNAP_MAX_HEIGHT, "px"] }) }), _jsx("div", { style: {
|
|
73
|
+
height: "100%",
|
|
74
|
+
background: "repeating-linear-gradient(-45deg, transparent, transparent 8px, rgba(255,100,100,0.06) 8px, rgba(255,100,100,0.06) 16px)",
|
|
75
|
+
} })] }))] }));
|
|
76
|
+
}
|
|
@@ -15,11 +15,11 @@ export function SnapBadge({ element: { props }, }) {
|
|
|
15
15
|
return (_jsxs(View, { style: [
|
|
16
16
|
styles.badge,
|
|
17
17
|
isFilled
|
|
18
|
-
? { backgroundColor: resolvedColor, borderColor:
|
|
18
|
+
? { backgroundColor: resolvedColor + "20", borderColor: "transparent" }
|
|
19
19
|
: { borderColor: resolvedColor },
|
|
20
|
-
], children: [Icon && (_jsx(Icon, { size: 12, color:
|
|
20
|
+
], children: [Icon && (_jsx(Icon, { size: 12, color: resolvedColor })), _jsx(Text, { style: [
|
|
21
21
|
styles.label,
|
|
22
|
-
{ color:
|
|
22
|
+
{ color: resolvedColor },
|
|
23
23
|
], children: label })] }));
|
|
24
24
|
}
|
|
25
25
|
const styles = StyleSheet.create({
|
|
@@ -1,54 +1,24 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { ValidationResult } from "@farcaster/snap";
|
|
3
|
+
import type { SnapNativeColors } from "./theme.js";
|
|
4
|
+
import type { SnapPage, SnapActionHandlers } from "./types.js";
|
|
5
|
+
import { useSnapTheme } from "./theme.js";
|
|
3
6
|
import { hexToRgba } from "./use-snap-palette.js";
|
|
4
|
-
export type JsonValue
|
|
5
|
-
[key: string]: JsonValue;
|
|
6
|
-
};
|
|
7
|
-
export type SnapPage = {
|
|
8
|
-
version: string;
|
|
9
|
-
theme?: {
|
|
10
|
-
accent?: string;
|
|
11
|
-
};
|
|
12
|
-
effects?: string[];
|
|
13
|
-
ui: Spec;
|
|
14
|
-
};
|
|
15
|
-
export type SnapActionHandlers = {
|
|
16
|
-
submit: (target: string, inputs: Record<string, JsonValue>) => void;
|
|
17
|
-
open_url: (target: string, options?: {
|
|
18
|
-
isSnap?: boolean;
|
|
19
|
-
}) => void;
|
|
20
|
-
open_mini_app: (target: string) => void;
|
|
21
|
-
view_cast: (params: {
|
|
22
|
-
hash: string;
|
|
23
|
-
}) => void;
|
|
24
|
-
view_profile: (params: {
|
|
25
|
-
fid: number;
|
|
26
|
-
}) => void;
|
|
27
|
-
compose_cast: (params: {
|
|
28
|
-
text?: string;
|
|
29
|
-
channelKey?: string;
|
|
30
|
-
embeds?: string[];
|
|
31
|
-
}) => void;
|
|
32
|
-
view_token: (params: {
|
|
33
|
-
token: string;
|
|
34
|
-
}) => void;
|
|
35
|
-
send_token: (params: {
|
|
36
|
-
token: string;
|
|
37
|
-
amount?: string;
|
|
38
|
-
recipientFid?: number;
|
|
39
|
-
recipientAddress?: string;
|
|
40
|
-
}) => void;
|
|
41
|
-
swap_token: (params: {
|
|
42
|
-
sellToken?: string;
|
|
43
|
-
buyToken?: string;
|
|
44
|
-
}) => void;
|
|
45
|
-
};
|
|
7
|
+
export type { JsonValue, SnapPage, SnapActionHandlers } from "./types.js";
|
|
46
8
|
export { useSnapTheme, hexToRgba };
|
|
47
9
|
export type { SnapNativeColors };
|
|
48
|
-
export declare function
|
|
10
|
+
export declare function SnapCard({ snap, handlers, loading, appearance, colors, borderRadius, showOverflowWarning, onValidationError, validationErrorFallback, }: {
|
|
49
11
|
snap: SnapPage;
|
|
50
12
|
handlers: SnapActionHandlers;
|
|
51
13
|
loading?: boolean;
|
|
52
14
|
appearance?: "light" | "dark";
|
|
53
15
|
colors?: Partial<SnapNativeColors>;
|
|
16
|
+
/** Border radius of the card (default 16). */
|
|
17
|
+
borderRadius?: number;
|
|
18
|
+
/** When true (v2 only), extends to 700px and shows a warning overlay below 500px. When false, clips at 500px. */
|
|
19
|
+
showOverflowWarning?: boolean;
|
|
20
|
+
/** Called when snap validation fails (v2 only). */
|
|
21
|
+
onValidationError?: (result: ValidationResult) => void;
|
|
22
|
+
/** Custom fallback rendered when validation fails (v2 only). */
|
|
23
|
+
validationErrorFallback?: ReactNode;
|
|
54
24
|
}): import("react").JSX.Element;
|
|
@@ -1,171 +1,15 @@
|
|
|
1
|
-
import { jsx as _jsx
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { SnapThemeProvider, useSnapTheme, } from "./theme.js";
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { SPEC_VERSION_2 } from "@farcaster/snap";
|
|
3
|
+
import { useSnapTheme } from "./theme.js";
|
|
5
4
|
import { hexToRgba } from "./use-snap-palette.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { ConfettiOverlay } from "./confetti-overlay.js";
|
|
9
|
-
import { DEFAULT_THEME_ACCENT, PALETTE_LIGHT_HEX, PALETTE_DARK_HEX, } from "@farcaster/snap";
|
|
5
|
+
import { SnapCardV1 } from "./v1/snap-view.js";
|
|
6
|
+
import { SnapCardV2 } from "./v2/snap-view.js";
|
|
10
7
|
// ─── Re-exports ───────────────────────────────────────
|
|
11
8
|
export { useSnapTheme, hexToRgba };
|
|
12
|
-
// ───
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
: Object.entries(changes);
|
|
17
|
-
for (const [path, value] of entries) {
|
|
18
|
-
const trimmed = path.startsWith("/") ? path : `/${path}`;
|
|
19
|
-
const parts = trimmed.split("/").filter(Boolean);
|
|
20
|
-
if (parts.length < 2)
|
|
21
|
-
continue;
|
|
22
|
-
const [top, ...rest] = parts;
|
|
23
|
-
if (top === "inputs") {
|
|
24
|
-
if (typeof model.inputs !== "object" || model.inputs === null) {
|
|
25
|
-
model.inputs = {};
|
|
26
|
-
}
|
|
27
|
-
const inputs = model.inputs;
|
|
28
|
-
if (rest.length === 1) {
|
|
29
|
-
inputs[rest[0]] = value;
|
|
30
|
-
}
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
if (top === "theme") {
|
|
34
|
-
if (typeof model.theme !== "object" || model.theme === null) {
|
|
35
|
-
model.theme = {};
|
|
36
|
-
}
|
|
37
|
-
const theme = model.theme;
|
|
38
|
-
if (rest.length === 1) {
|
|
39
|
-
theme[rest[0]] = value;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
9
|
+
// ─── SnapCard (version-switching) ─────────────────────
|
|
10
|
+
export function SnapCard({ snap, handlers, loading = false, appearance = "dark", colors, borderRadius = 16, showOverflowWarning = false, onValidationError, validationErrorFallback, }) {
|
|
11
|
+
if (snap.version === SPEC_VERSION_2) {
|
|
12
|
+
return (_jsx(SnapCardV2, { snap: snap, handlers: handlers, loading: loading, appearance: appearance, colors: colors, borderRadius: borderRadius, showOverflowWarning: showOverflowWarning, onValidationError: onValidationError, validationErrorFallback: validationErrorFallback }));
|
|
42
13
|
}
|
|
14
|
+
return (_jsx(SnapCardV1, { snap: snap, handlers: handlers, loading: loading, appearance: appearance, colors: colors, borderRadius: borderRadius }));
|
|
43
15
|
}
|
|
44
|
-
function resolveAccentHex(accent, appearance) {
|
|
45
|
-
const map = appearance === "dark" ? PALETTE_DARK_HEX : PALETTE_LIGHT_HEX;
|
|
46
|
-
const name = accent && Object.hasOwn(map, accent)
|
|
47
|
-
? accent
|
|
48
|
-
: DEFAULT_THEME_ACCENT;
|
|
49
|
-
return map[name];
|
|
50
|
-
}
|
|
51
|
-
// ─── SnapView ─────────────────────────────────────────
|
|
52
|
-
function SnapViewInner({ snap, handlers, loading = false, }) {
|
|
53
|
-
const { mode } = useSnapTheme();
|
|
54
|
-
const spec = snap.ui;
|
|
55
|
-
const accentHex = resolveAccentHex(snap.theme?.accent, mode);
|
|
56
|
-
const showConfetti = snap.effects?.includes("confetti");
|
|
57
|
-
// Increment key each time a new snap with confetti arrives so the overlay
|
|
58
|
-
// unmounts/remounts and restarts its animation on every trigger.
|
|
59
|
-
const confettiEpochRef = useRef(0);
|
|
60
|
-
const lastConfettiSnapRef = useRef(null);
|
|
61
|
-
if (showConfetti && snap !== lastConfettiSnapRef.current) {
|
|
62
|
-
confettiEpochRef.current++;
|
|
63
|
-
lastConfettiSnapRef.current = snap;
|
|
64
|
-
}
|
|
65
|
-
const initialState = useMemo(() => ({
|
|
66
|
-
...(spec.state ?? {}),
|
|
67
|
-
inputs: { ...(spec.state?.inputs ?? {}) },
|
|
68
|
-
theme: {
|
|
69
|
-
...(spec.state?.theme ?? {}),
|
|
70
|
-
...(snap.theme ? { accent: snap.theme.accent } : {}),
|
|
71
|
-
},
|
|
72
|
-
}), [spec, snap.theme]);
|
|
73
|
-
const stateRef = useRef(initialState);
|
|
74
|
-
useEffect(() => {
|
|
75
|
-
stateRef.current = {
|
|
76
|
-
inputs: {
|
|
77
|
-
...(initialState.inputs ?? {}),
|
|
78
|
-
},
|
|
79
|
-
theme: {
|
|
80
|
-
...(initialState.theme ?? {}),
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
}, [initialState]);
|
|
84
|
-
useEffect(() => {
|
|
85
|
-
const result = snapJsonRenderCatalog.validate(spec);
|
|
86
|
-
if (!result.success) {
|
|
87
|
-
// eslint-disable-next-line no-console
|
|
88
|
-
console.warn("[SnapView] catalog validation issues:", result.error);
|
|
89
|
-
}
|
|
90
|
-
}, [spec]);
|
|
91
|
-
const [pageKey, setPageKey] = useState(0);
|
|
92
|
-
useEffect(() => {
|
|
93
|
-
setPageKey((k) => k + 1);
|
|
94
|
-
}, [spec]);
|
|
95
|
-
const handlersRef = useRef(handlers);
|
|
96
|
-
handlersRef.current = handlers;
|
|
97
|
-
const handleAction = useCallback((name, params) => {
|
|
98
|
-
const inputs = (stateRef.current.inputs ?? {});
|
|
99
|
-
const p = (params ?? {});
|
|
100
|
-
const h = handlersRef.current;
|
|
101
|
-
switch (name) {
|
|
102
|
-
case "submit":
|
|
103
|
-
h.submit(String(p.target ?? ""), inputs);
|
|
104
|
-
break;
|
|
105
|
-
case "open_url":
|
|
106
|
-
h.open_url(String(p.target ?? ""), {
|
|
107
|
-
isSnap: p.isSnap === true,
|
|
108
|
-
});
|
|
109
|
-
break;
|
|
110
|
-
case "open_mini_app":
|
|
111
|
-
h.open_mini_app(String(p.target ?? ""));
|
|
112
|
-
break;
|
|
113
|
-
case "view_cast":
|
|
114
|
-
h.view_cast({ hash: String(p.hash ?? "") });
|
|
115
|
-
break;
|
|
116
|
-
case "view_profile":
|
|
117
|
-
h.view_profile({ fid: Number(p.fid ?? 0) });
|
|
118
|
-
break;
|
|
119
|
-
case "compose_cast":
|
|
120
|
-
h.compose_cast({
|
|
121
|
-
text: p.text ? String(p.text) : undefined,
|
|
122
|
-
channelKey: p.channelKey ? String(p.channelKey) : undefined,
|
|
123
|
-
embeds: Array.isArray(p.embeds) ? p.embeds : undefined,
|
|
124
|
-
});
|
|
125
|
-
break;
|
|
126
|
-
case "view_token":
|
|
127
|
-
h.view_token({ token: String(p.token ?? "") });
|
|
128
|
-
break;
|
|
129
|
-
case "send_token":
|
|
130
|
-
h.send_token({
|
|
131
|
-
token: String(p.token ?? ""),
|
|
132
|
-
amount: p.amount ? String(p.amount) : undefined,
|
|
133
|
-
recipientFid: p.recipientFid ? Number(p.recipientFid) : undefined,
|
|
134
|
-
recipientAddress: p.recipientAddress
|
|
135
|
-
? String(p.recipientAddress)
|
|
136
|
-
: undefined,
|
|
137
|
-
});
|
|
138
|
-
break;
|
|
139
|
-
case "swap_token":
|
|
140
|
-
h.swap_token({
|
|
141
|
-
sellToken: p.sellToken ? String(p.sellToken) : undefined,
|
|
142
|
-
buyToken: p.buyToken ? String(p.buyToken) : undefined,
|
|
143
|
-
});
|
|
144
|
-
break;
|
|
145
|
-
default:
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
}, []);
|
|
149
|
-
return (_jsxs(View, { style: styles.container, children: [loading ? (_jsx(View, { style: [
|
|
150
|
-
styles.overlay,
|
|
151
|
-
{
|
|
152
|
-
backgroundColor: mode === "dark" ? "rgba(0,0,0,0.1)" : "rgba(255,255,255,0.2)",
|
|
153
|
-
},
|
|
154
|
-
], children: _jsx(ActivityIndicator, { size: "large", color: accentHex }) })) : null, showConfetti ? (_jsx(ConfettiOverlay, {}, `confetti-${confettiEpochRef.current}`)) : null, _jsx(SnapCatalogView, { spec: spec, state: initialState, loading: false, onStateChange: (changes) => {
|
|
155
|
-
applyStatePaths(stateRef.current, changes);
|
|
156
|
-
}, onAction: handleAction }, pageKey)] }));
|
|
157
|
-
}
|
|
158
|
-
export function SnapView({ snap, handlers, loading = false, appearance = "dark", colors, }) {
|
|
159
|
-
return (_jsx(SnapThemeProvider, { appearance: appearance, colors: colors, children: _jsx(SnapViewInner, { snap: snap, handlers: handlers, loading: loading }) }));
|
|
160
|
-
}
|
|
161
|
-
const styles = StyleSheet.create({
|
|
162
|
-
container: {
|
|
163
|
-
width: "100%",
|
|
164
|
-
},
|
|
165
|
-
overlay: {
|
|
166
|
-
...StyleSheet.absoluteFillObject,
|
|
167
|
-
alignItems: "center",
|
|
168
|
-
justifyContent: "center",
|
|
169
|
-
zIndex: 10,
|
|
170
|
-
},
|
|
171
|
-
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SnapPage, SnapActionHandlers } from "./types.js";
|
|
2
|
+
export declare function applyStatePaths(model: Record<string, unknown>, changes: {
|
|
3
|
+
path: string;
|
|
4
|
+
value: unknown;
|
|
5
|
+
}[] | Record<string, unknown>): void;
|
|
6
|
+
export declare function resolveAccentHex(accent: string | undefined, appearance: "light" | "dark"): string;
|
|
7
|
+
export declare function SnapViewCoreInner({ snap, handlers, loading, }: {
|
|
8
|
+
snap: SnapPage;
|
|
9
|
+
handlers: SnapActionHandlers;
|
|
10
|
+
loading?: boolean;
|
|
11
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { snapJsonRenderCatalog } from "@farcaster/snap/ui";
|
|
3
|
+
import { SnapCatalogView } from "./catalog-renderer.js";
|
|
4
|
+
import { useSnapTheme } from "./theme.js";
|
|
5
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
6
|
+
import { ActivityIndicator, StyleSheet, View } from "react-native";
|
|
7
|
+
import { DEFAULT_THEME_ACCENT, PALETTE_LIGHT_HEX, PALETTE_DARK_HEX, } from "@farcaster/snap";
|
|
8
|
+
// ─── Shared helpers ──────────────────────────────────
|
|
9
|
+
export function applyStatePaths(model, changes) {
|
|
10
|
+
const entries = Array.isArray(changes)
|
|
11
|
+
? changes.map((c) => [c.path, c.value])
|
|
12
|
+
: Object.entries(changes);
|
|
13
|
+
for (const [path, value] of entries) {
|
|
14
|
+
const trimmed = path.startsWith("/") ? path : `/${path}`;
|
|
15
|
+
const parts = trimmed.split("/").filter(Boolean);
|
|
16
|
+
if (parts.length < 2)
|
|
17
|
+
continue;
|
|
18
|
+
const [top, ...rest] = parts;
|
|
19
|
+
if (top === "inputs") {
|
|
20
|
+
if (typeof model.inputs !== "object" || model.inputs === null) {
|
|
21
|
+
model.inputs = {};
|
|
22
|
+
}
|
|
23
|
+
const inputs = model.inputs;
|
|
24
|
+
if (rest.length === 1) {
|
|
25
|
+
inputs[rest[0]] = value;
|
|
26
|
+
}
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (top === "theme") {
|
|
30
|
+
if (typeof model.theme !== "object" || model.theme === null) {
|
|
31
|
+
model.theme = {};
|
|
32
|
+
}
|
|
33
|
+
const theme = model.theme;
|
|
34
|
+
if (rest.length === 1) {
|
|
35
|
+
theme[rest[0]] = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function resolveAccentHex(accent, appearance) {
|
|
41
|
+
const map = appearance === "dark" ? PALETTE_DARK_HEX : PALETTE_LIGHT_HEX;
|
|
42
|
+
const name = accent && Object.hasOwn(map, accent)
|
|
43
|
+
? accent
|
|
44
|
+
: DEFAULT_THEME_ACCENT;
|
|
45
|
+
return map[name];
|
|
46
|
+
}
|
|
47
|
+
// ─── Core rendering component (no validation) ────────
|
|
48
|
+
export function SnapViewCoreInner({ snap, handlers, loading = false, }) {
|
|
49
|
+
const { mode } = useSnapTheme();
|
|
50
|
+
const spec = snap.ui;
|
|
51
|
+
const accentHex = resolveAccentHex(snap.theme?.accent, mode);
|
|
52
|
+
const initialState = useMemo(() => ({
|
|
53
|
+
...(spec.state ?? {}),
|
|
54
|
+
inputs: { ...(spec.state?.inputs ?? {}) },
|
|
55
|
+
theme: {
|
|
56
|
+
...(spec.state?.theme ?? {}),
|
|
57
|
+
...(snap.theme ? { accent: snap.theme.accent } : {}),
|
|
58
|
+
},
|
|
59
|
+
}), [spec, snap.theme]);
|
|
60
|
+
const stateRef = useRef(initialState);
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
stateRef.current = {
|
|
63
|
+
inputs: {
|
|
64
|
+
...(initialState.inputs ?? {}),
|
|
65
|
+
},
|
|
66
|
+
theme: {
|
|
67
|
+
...(initialState.theme ?? {}),
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}, [initialState]);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const catalogResult = snapJsonRenderCatalog.validate(spec);
|
|
73
|
+
if (!catalogResult.success) {
|
|
74
|
+
// eslint-disable-next-line no-console
|
|
75
|
+
console.warn("[Snap] catalog validation issues:", catalogResult.error);
|
|
76
|
+
}
|
|
77
|
+
}, [spec]);
|
|
78
|
+
const [pageKey, setPageKey] = useState(0);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
setPageKey((k) => k + 1);
|
|
81
|
+
}, [spec]);
|
|
82
|
+
const handlersRef = useRef(handlers);
|
|
83
|
+
handlersRef.current = handlers;
|
|
84
|
+
const handleAction = useCallback((name, params) => {
|
|
85
|
+
const inputs = (stateRef.current.inputs ?? {});
|
|
86
|
+
const p = (params ?? {});
|
|
87
|
+
const h = handlersRef.current;
|
|
88
|
+
switch (name) {
|
|
89
|
+
case "submit":
|
|
90
|
+
h.submit(String(p.target ?? ""), inputs);
|
|
91
|
+
break;
|
|
92
|
+
case "open_url":
|
|
93
|
+
h.open_url(String(p.target ?? ""));
|
|
94
|
+
break;
|
|
95
|
+
case "open_mini_app":
|
|
96
|
+
h.open_mini_app(String(p.target ?? ""));
|
|
97
|
+
break;
|
|
98
|
+
case "view_cast":
|
|
99
|
+
h.view_cast({ hash: String(p.hash ?? "") });
|
|
100
|
+
break;
|
|
101
|
+
case "view_profile":
|
|
102
|
+
h.view_profile({ fid: Number(p.fid ?? 0) });
|
|
103
|
+
break;
|
|
104
|
+
case "compose_cast":
|
|
105
|
+
h.compose_cast({
|
|
106
|
+
text: p.text ? String(p.text) : undefined,
|
|
107
|
+
channelKey: p.channelKey ? String(p.channelKey) : undefined,
|
|
108
|
+
embeds: Array.isArray(p.embeds) ? p.embeds : undefined,
|
|
109
|
+
});
|
|
110
|
+
break;
|
|
111
|
+
case "view_token":
|
|
112
|
+
h.view_token({ token: String(p.token ?? "") });
|
|
113
|
+
break;
|
|
114
|
+
case "send_token":
|
|
115
|
+
h.send_token({
|
|
116
|
+
token: String(p.token ?? ""),
|
|
117
|
+
amount: p.amount ? String(p.amount) : undefined,
|
|
118
|
+
recipientFid: p.recipientFid ? Number(p.recipientFid) : undefined,
|
|
119
|
+
recipientAddress: p.recipientAddress
|
|
120
|
+
? String(p.recipientAddress)
|
|
121
|
+
: undefined,
|
|
122
|
+
});
|
|
123
|
+
break;
|
|
124
|
+
case "swap_token":
|
|
125
|
+
h.swap_token({
|
|
126
|
+
sellToken: p.sellToken ? String(p.sellToken) : undefined,
|
|
127
|
+
buyToken: p.buyToken ? String(p.buyToken) : undefined,
|
|
128
|
+
});
|
|
129
|
+
break;
|
|
130
|
+
default:
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}, []);
|
|
134
|
+
return (_jsxs(View, { style: styles.container, children: [loading ? (_jsx(View, { style: [
|
|
135
|
+
styles.overlay,
|
|
136
|
+
{
|
|
137
|
+
backgroundColor: mode === "dark" ? "rgba(0,0,0,0.1)" : "rgba(255,255,255,0.2)",
|
|
138
|
+
},
|
|
139
|
+
], children: _jsx(ActivityIndicator, { size: "large", color: accentHex }) })) : null, _jsx(SnapCatalogView, { spec: spec, state: initialState, loading: false, onStateChange: (changes) => {
|
|
140
|
+
applyStatePaths(stateRef.current, changes);
|
|
141
|
+
}, onAction: handleAction }, pageKey)] }));
|
|
142
|
+
}
|
|
143
|
+
const styles = StyleSheet.create({
|
|
144
|
+
container: {
|
|
145
|
+
width: "100%",
|
|
146
|
+
},
|
|
147
|
+
overlay: {
|
|
148
|
+
...StyleSheet.absoluteFillObject,
|
|
149
|
+
alignItems: "center",
|
|
150
|
+
justifyContent: "center",
|
|
151
|
+
zIndex: 10,
|
|
152
|
+
},
|
|
153
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Spec } from "@json-render/core";
|
|
2
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
3
|
+
[key: string]: JsonValue;
|
|
4
|
+
};
|
|
5
|
+
export type SnapPage = {
|
|
6
|
+
version: string;
|
|
7
|
+
theme?: {
|
|
8
|
+
accent?: string;
|
|
9
|
+
};
|
|
10
|
+
effects?: string[];
|
|
11
|
+
ui: Spec;
|
|
12
|
+
};
|
|
13
|
+
export type SnapActionHandlers = {
|
|
14
|
+
submit: (target: string, inputs: Record<string, JsonValue>) => void;
|
|
15
|
+
open_url: (target: string) => void;
|
|
16
|
+
open_mini_app: (target: string) => void;
|
|
17
|
+
view_cast: (params: {
|
|
18
|
+
hash: string;
|
|
19
|
+
}) => void;
|
|
20
|
+
view_profile: (params: {
|
|
21
|
+
fid: number;
|
|
22
|
+
}) => void;
|
|
23
|
+
compose_cast: (params: {
|
|
24
|
+
text?: string;
|
|
25
|
+
channelKey?: string;
|
|
26
|
+
embeds?: string[];
|
|
27
|
+
}) => void;
|
|
28
|
+
view_token: (params: {
|
|
29
|
+
token: string;
|
|
30
|
+
}) => void;
|
|
31
|
+
send_token: (params: {
|
|
32
|
+
token: string;
|
|
33
|
+
amount?: string;
|
|
34
|
+
recipientFid?: number;
|
|
35
|
+
recipientAddress?: string;
|
|
36
|
+
}) => void;
|
|
37
|
+
swap_token: (params: {
|
|
38
|
+
sellToken?: string;
|
|
39
|
+
buyToken?: string;
|
|
40
|
+
}) => void;
|
|
41
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type SnapNativeColors } from "../theme.js";
|
|
2
|
+
import type { SnapPage, SnapActionHandlers } from "../types.js";
|
|
3
|
+
export declare function SnapViewV1Inner({ snap, handlers, loading, }: {
|
|
4
|
+
snap: SnapPage;
|
|
5
|
+
handlers: SnapActionHandlers;
|
|
6
|
+
loading?: boolean;
|
|
7
|
+
}): import("react").JSX.Element;
|
|
8
|
+
export declare function SnapViewV1({ snap, handlers, loading, appearance, colors, }: {
|
|
9
|
+
snap: SnapPage;
|
|
10
|
+
handlers: SnapActionHandlers;
|
|
11
|
+
loading?: boolean;
|
|
12
|
+
appearance?: "light" | "dark";
|
|
13
|
+
colors?: Partial<SnapNativeColors>;
|
|
14
|
+
}): import("react").JSX.Element;
|
|
15
|
+
export declare function SnapCardV1({ snap, handlers, loading, appearance, colors, borderRadius, }: {
|
|
16
|
+
snap: SnapPage;
|
|
17
|
+
handlers: SnapActionHandlers;
|
|
18
|
+
loading?: boolean;
|
|
19
|
+
appearance?: "light" | "dark";
|
|
20
|
+
colors?: Partial<SnapNativeColors>;
|
|
21
|
+
borderRadius?: number;
|
|
22
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { View, StyleSheet } from "react-native";
|
|
3
|
+
import { SnapThemeProvider, useSnapTheme } from "../theme.js";
|
|
4
|
+
import { SnapViewCoreInner } from "../snap-view-core.js";
|
|
5
|
+
// ─── SnapViewV1 (no validation, no height limits) ────
|
|
6
|
+
export function SnapViewV1Inner({ snap, handlers, loading = false, }) {
|
|
7
|
+
return (_jsx(SnapViewCoreInner, { snap: snap, handlers: handlers, loading: loading }));
|
|
8
|
+
}
|
|
9
|
+
export function SnapViewV1({ snap, handlers, loading = false, appearance = "dark", colors, }) {
|
|
10
|
+
return (_jsx(SnapThemeProvider, { appearance: appearance, colors: colors, children: _jsx(SnapViewV1Inner, { snap: snap, handlers: handlers, loading: loading }) }));
|
|
11
|
+
}
|
|
12
|
+
// ─── SnapCardV1 (card frame, no height limits) ───────
|
|
13
|
+
function SnapCardV1Inner({ snap, handlers, loading = false, borderRadius, }) {
|
|
14
|
+
const { colors } = useSnapTheme();
|
|
15
|
+
return (_jsx(View, { style: cardStyles.frameRing, children: _jsx(View, { style: [
|
|
16
|
+
cardStyles.card,
|
|
17
|
+
{
|
|
18
|
+
borderRadius,
|
|
19
|
+
borderColor: colors.border,
|
|
20
|
+
backgroundColor: colors.surface,
|
|
21
|
+
},
|
|
22
|
+
], children: _jsx(View, { style: cardStyles.body, children: _jsx(SnapViewV1Inner, { snap: snap, handlers: handlers, loading: loading }) }) }) }));
|
|
23
|
+
}
|
|
24
|
+
export function SnapCardV1({ snap, handlers, loading = false, appearance = "dark", colors, borderRadius = 16, }) {
|
|
25
|
+
return (_jsx(SnapThemeProvider, { appearance: appearance, colors: colors, children: _jsx(SnapCardV1Inner, { snap: snap, handlers: handlers, loading: loading, borderRadius: borderRadius }) }));
|
|
26
|
+
}
|
|
27
|
+
const cardStyles = StyleSheet.create({
|
|
28
|
+
frameRing: { alignSelf: "stretch" },
|
|
29
|
+
card: { overflow: "hidden", borderWidth: 1, minHeight: 120 },
|
|
30
|
+
body: { paddingHorizontal: 16, paddingVertical: 16 },
|
|
31
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { type SnapNativeColors } from "../theme.js";
|
|
3
|
+
import { type ValidationResult } from "@farcaster/snap";
|
|
4
|
+
import type { SnapPage, SnapActionHandlers } from "../types.js";
|
|
5
|
+
export declare function SnapViewV2Inner({ snap, handlers, loading, onValidationError, validationErrorFallback, }: {
|
|
6
|
+
snap: SnapPage;
|
|
7
|
+
handlers: SnapActionHandlers;
|
|
8
|
+
loading?: boolean;
|
|
9
|
+
onValidationError?: (result: ValidationResult) => void;
|
|
10
|
+
validationErrorFallback?: ReactNode;
|
|
11
|
+
}): import("react").JSX.Element;
|
|
12
|
+
export declare function SnapViewV2({ snap, handlers, loading, appearance, colors, onValidationError, validationErrorFallback, }: {
|
|
13
|
+
snap: SnapPage;
|
|
14
|
+
handlers: SnapActionHandlers;
|
|
15
|
+
loading?: boolean;
|
|
16
|
+
appearance?: "light" | "dark";
|
|
17
|
+
colors?: Partial<SnapNativeColors>;
|
|
18
|
+
onValidationError?: (result: ValidationResult) => void;
|
|
19
|
+
validationErrorFallback?: ReactNode;
|
|
20
|
+
}): import("react").JSX.Element;
|
|
21
|
+
export declare function SnapCardV2({ snap, handlers, loading, appearance, colors, borderRadius, showOverflowWarning, onValidationError, validationErrorFallback, }: {
|
|
22
|
+
snap: SnapPage;
|
|
23
|
+
handlers: SnapActionHandlers;
|
|
24
|
+
loading?: boolean;
|
|
25
|
+
appearance?: "light" | "dark";
|
|
26
|
+
colors?: Partial<SnapNativeColors>;
|
|
27
|
+
borderRadius?: number;
|
|
28
|
+
showOverflowWarning?: boolean;
|
|
29
|
+
onValidationError?: (result: ValidationResult) => void;
|
|
30
|
+
validationErrorFallback?: ReactNode;
|
|
31
|
+
}): import("react").JSX.Element;
|