UNPKG

@enclavemoney/enclave-wallet-sdk

Version:

A simple enclave wallet SDK for React applications

268 lines (267 loc) 11.8 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToastProvider = exports.toast = exports.useToast = void 0; var react_1 = __importStar(require("react")); var ToastContext = (0, react_1.createContext)(null); var useToast = function () { var context = (0, react_1.useContext)(ToastContext); if (!context) { throw new Error("useToast must be used within a ToastProvider"); } return context; }; exports.useToast = useToast; // Toast utility functions (similar to react-hot-toast API) var toastContext = null; exports.toast = { success: function (message, options) { if (!toastContext) return ""; return toastContext.addToast({ type: "success", message: message, duration: (options === null || options === void 0 ? void 0 : options.duration) || 4000, dismissible: true, }); }, error: function (message, options) { if (!toastContext) return ""; return toastContext.addToast({ type: "error", message: message, duration: (options === null || options === void 0 ? void 0 : options.duration) || 5000, dismissible: true, }); }, loading: function (message) { if (!toastContext) return ""; return toastContext.addToast({ type: "loading", message: message, dismissible: false, }); }, info: function (message, options) { if (!toastContext) return ""; return toastContext.addToast({ type: "info", message: message, duration: (options === null || options === void 0 ? void 0 : options.duration) || 4000, dismissible: true, }); }, dismiss: function (id) { if (!toastContext) return; toastContext.removeToast(id); }, update: function (id, updates) { if (!toastContext) return; toastContext.updateToast(id, updates); }, }; var ToastProvider = function (_a) { var children = _a.children; var _b = (0, react_1.useState)([]), toasts = _b[0], setToasts = _b[1]; var addToast = (0, react_1.useCallback)(function (toast) { var id = Math.random().toString(36).substring(2, 9); var newToast = __assign(__assign({}, toast), { id: id }); setToasts(function (prev) { return __spreadArray(__spreadArray([], prev, true), [newToast], false); }); // Auto-dismiss if duration is set if (toast.duration) { setTimeout(function () { removeToast(id); }, toast.duration); } return id; }, []); var removeToast = (0, react_1.useCallback)(function (id) { setToasts(function (prev) { return prev.filter(function (toast) { return toast.id !== id; }); }); }, []); var updateToast = (0, react_1.useCallback)(function (id, updates) { setToasts(function (prev) { return prev.map(function (toast) { return (toast.id === id ? __assign(__assign({}, toast), updates) : toast); }); }); }, []); var contextValue = { toasts: toasts, addToast: addToast, removeToast: removeToast, updateToast: updateToast, }; // Set global context reference (0, react_1.useEffect)(function () { toastContext = contextValue; return function () { toastContext = null; }; }, [contextValue]); return (react_1.default.createElement(ToastContext.Provider, { value: contextValue }, children, react_1.default.createElement(ToastContainer, null))); }; exports.ToastProvider = ToastProvider; var ToastContainer = function () { var _a = (0, exports.useToast)(), toasts = _a.toasts, removeToast = _a.removeToast; return (react_1.default.createElement("div", { style: { position: "fixed", top: "20px", left: "50%", transform: "translateX(-50%)", zIndex: 9999, display: "flex", flexDirection: "column", gap: "8px", pointerEvents: "none", } }, toasts.map(function (toast) { return (react_1.default.createElement(ToastItem, { key: toast.id, toast: toast, onDismiss: function () { return removeToast(toast.id); } })); }))); }; var ToastItem = function (_a) { var toast = _a.toast, onDismiss = _a.onDismiss; var _b = (0, react_1.useState)(false), isVisible = _b[0], setIsVisible = _b[1]; var _c = (0, react_1.useState)(false), isExiting = _c[0], setIsExiting = _c[1]; (0, react_1.useEffect)(function () { // Trigger entrance animation var timer = setTimeout(function () { return setIsVisible(true); }, 10); return function () { return clearTimeout(timer); }; }, []); var handleDismiss = function () { if (!toast.dismissible) return; setIsExiting(true); setTimeout(function () { onDismiss(); }, 200); }; var getIcon = function () { switch (toast.type) { case "success": return (react_1.default.createElement("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none" }, react_1.default.createElement("path", { d: "M13.5 4.5L6 12L2.5 8.5", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" }))); case "error": return (react_1.default.createElement("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none" }, react_1.default.createElement("path", { d: "M12 4L4 12M4 4L12 12", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" }))); case "loading": return (react_1.default.createElement("div", { style: { width: "16px", height: "16px", border: "2px solid rgba(255, 255, 255, 0.3)", borderTop: "2px solid currentColor", borderRadius: "50%", animation: "spin 1s linear infinite", } })); case "info": return (react_1.default.createElement("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none" }, react_1.default.createElement("circle", { cx: "8", cy: "8", r: "7", stroke: "currentColor", strokeWidth: "2" }), react_1.default.createElement("path", { d: "M8 12V8", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round" }), react_1.default.createElement("path", { d: "M8 4H8.01", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round" }))); default: return null; } }; var getBackgroundColor = function () { return "#000000"; }; return (react_1.default.createElement(react_1.default.Fragment, null, react_1.default.createElement("style", null, "\n @keyframes spin {\n from { transform: rotate(0deg); }\n to { transform: rotate(360deg); }\n }\n "), react_1.default.createElement("div", { style: { borderRadius: "50px", padding: "8px 16px", fontSize: "15px", fontWeight: "500", minHeight: "48px", width: "fit-content", maxWidth: "420px", fontFamily: "Inter, -apple-system, BlinkMacSystemFont, sans-serif", background: getBackgroundColor(), color: "#f9fafb", boxShadow: "0 8px 25px rgba(0, 0, 0, 0.4), 0 4px 10px rgba(0, 0, 0, 0.2)", border: "1px solid rgba(255, 255, 255, 0.33)", display: "flex", alignItems: "center", gap: "12px", pointerEvents: "auto", cursor: toast.dismissible ? "pointer" : "default", transform: "translateY(".concat(isVisible && !isExiting ? "0" : "-20px", ") scale(").concat(isVisible && !isExiting ? "1" : "0.95", ")"), opacity: isVisible && !isExiting ? 1 : 0, transition: "all 0.2s cubic-bezier(0.16, 1, 0.3, 1)", whiteSpace: "nowrap", flexWrap: "nowrap", }, onClick: handleDismiss }, getIcon(), react_1.default.createElement("div", { style: { flex: 1, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", } }, toast.message), toast.dismissible && (react_1.default.createElement("button", { onClick: function (e) { e.stopPropagation(); handleDismiss(); }, style: { background: "none", border: "none", color: "currentColor", cursor: "pointer", padding: "2px", borderRadius: "50%", display: "flex", alignItems: "center", justifyContent: "center", opacity: 0.7, transition: "opacity 0.2s", }, onMouseEnter: function (e) { e.currentTarget.style.opacity = "1"; }, onMouseLeave: function (e) { e.currentTarget.style.opacity = "0.7"; } }, react_1.default.createElement("svg", { width: "14", height: "14", viewBox: "0 0 14 14", fill: "none" }, react_1.default.createElement("path", { d: "M10.5 3.5L3.5 10.5M3.5 3.5L10.5 10.5", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }))))))); }; exports.default = exports.ToastProvider;