Compare commits
22 Commits
feat/add-h
...
fix/sample
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f8f47cb14 | ||
|
|
ddb043c104 | ||
|
|
d2ad6537d7 | ||
|
|
8f004ee4da | ||
|
|
b90bf67c20 | ||
|
|
38a097a8f9 | ||
|
|
db0ffe999e | ||
|
|
6d88c4e546 | ||
|
|
ce91182c7b | ||
|
|
2e3a0e557e | ||
|
|
6b9a9ef978 | ||
|
|
bc5bb5be39 | ||
|
|
0fe83811b9 | ||
|
|
c6359aa853 | ||
|
|
c9c818c8f3 | ||
|
|
c521246393 | ||
|
|
8936b34361 | ||
|
|
5993d2762f | ||
|
|
810d3b2524 | ||
|
|
a3393ded1e | ||
|
|
17ede265b1 | ||
|
|
629070edad |
@@ -27,7 +27,7 @@ const labelStyle = css({
|
|||||||
mb: "$0.5",
|
mb: "$0.5",
|
||||||
});
|
});
|
||||||
|
|
||||||
const AccountDialog = ({
|
export const AccountDialog = ({
|
||||||
activeAccountAddress,
|
activeAccountAddress,
|
||||||
setActiveAccountAddress,
|
setActiveAccountAddress,
|
||||||
}: {
|
}: {
|
||||||
@@ -36,11 +36,13 @@ const AccountDialog = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const snap = useSnapshot(state);
|
const snap = useSnapshot(state);
|
||||||
const [showSecret, setShowSecret] = useState(false);
|
const [showSecret, setShowSecret] = useState(false);
|
||||||
const activeAccount = snap.accounts.find(account => account.address === activeAccountAddress);
|
const activeAccount = snap.accounts.find(
|
||||||
|
(account) => account.address === activeAccountAddress
|
||||||
|
);
|
||||||
return (
|
return (
|
||||||
<Dialog
|
<Dialog
|
||||||
open={Boolean(activeAccountAddress)}
|
open={Boolean(activeAccountAddress)}
|
||||||
onOpenChange={open => {
|
onOpenChange={(open) => {
|
||||||
setShowSecret(false);
|
setShowSecret(false);
|
||||||
!open && setActiveAccountAddress(null);
|
!open && setActiveAccountAddress(null);
|
||||||
}}
|
}}
|
||||||
@@ -135,7 +137,7 @@ const AccountDialog = ({
|
|||||||
}}
|
}}
|
||||||
ghost
|
ghost
|
||||||
size="xs"
|
size="xs"
|
||||||
onClick={() => setShowSecret(curr => !curr)}
|
onClick={() => setShowSecret((curr) => !curr)}
|
||||||
>
|
>
|
||||||
{showSecret ? "Hide" : "Show"}
|
{showSecret ? "Hide" : "Show"}
|
||||||
</Button>
|
</Button>
|
||||||
@@ -221,13 +223,15 @@ interface AccountProps {
|
|||||||
showHookStats?: boolean;
|
showHookStats?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Accounts: FC<AccountProps> = props => {
|
const Accounts: FC<AccountProps> = (props) => {
|
||||||
const snap = useSnapshot(state);
|
const snap = useSnapshot(state);
|
||||||
const [activeAccountAddress, setActiveAccountAddress] = useState<string | null>(null);
|
const [activeAccountAddress, setActiveAccountAddress] = useState<
|
||||||
|
string | null
|
||||||
|
>(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchAccInfo = async () => {
|
const fetchAccInfo = async () => {
|
||||||
if (snap.clientStatus === "online") {
|
if (snap.clientStatus === "online") {
|
||||||
const requests = snap.accounts.map(acc =>
|
const requests = snap.accounts.map((acc) =>
|
||||||
snap.client?.send({
|
snap.client?.send({
|
||||||
id: acc.address,
|
id: acc.address,
|
||||||
command: "account_info",
|
command: "account_info",
|
||||||
@@ -239,13 +243,15 @@ const Accounts: FC<AccountProps> = props => {
|
|||||||
const address = res?.account_data?.Account as string;
|
const address = res?.account_data?.Account as string;
|
||||||
const balance = res?.account_data?.Balance as string;
|
const balance = res?.account_data?.Balance as string;
|
||||||
const sequence = res?.account_data?.Sequence as number;
|
const sequence = res?.account_data?.Sequence as number;
|
||||||
const accountToUpdate = state.accounts.find(acc => acc.address === address);
|
const accountToUpdate = state.accounts.find(
|
||||||
|
(acc) => acc.address === address
|
||||||
|
);
|
||||||
if (accountToUpdate) {
|
if (accountToUpdate) {
|
||||||
accountToUpdate.xrp = balance;
|
accountToUpdate.xrp = balance;
|
||||||
accountToUpdate.sequence = sequence;
|
accountToUpdate.sequence = sequence;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const objectRequests = snap.accounts.map(acc => {
|
const objectRequests = snap.accounts.map((acc) => {
|
||||||
return snap.client?.send({
|
return snap.client?.send({
|
||||||
id: `${acc.address}-hooks`,
|
id: `${acc.address}-hooks`,
|
||||||
command: "account_objects",
|
command: "account_objects",
|
||||||
@@ -255,7 +261,9 @@ const Accounts: FC<AccountProps> = props => {
|
|||||||
const objectResponses = await Promise.all(objectRequests);
|
const objectResponses = await Promise.all(objectRequests);
|
||||||
objectResponses.forEach((res: any) => {
|
objectResponses.forEach((res: any) => {
|
||||||
const address = res?.account as string;
|
const address = res?.account as string;
|
||||||
const accountToUpdate = state.accounts.find(acc => acc.address === address);
|
const accountToUpdate = state.accounts.find(
|
||||||
|
(acc) => acc.address === address
|
||||||
|
);
|
||||||
if (accountToUpdate) {
|
if (accountToUpdate) {
|
||||||
accountToUpdate.hooks = res.account_objects
|
accountToUpdate.hooks = res.account_objects
|
||||||
.filter((ac: any) => ac?.LedgerEntryType === "Hook")
|
.filter((ac: any) => ac?.LedgerEntryType === "Hook")
|
||||||
@@ -337,7 +345,7 @@ const Accounts: FC<AccountProps> = props => {
|
|||||||
overflowY: "auto",
|
overflowY: "auto",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{snap.accounts.map(account => (
|
{snap.accounts.map((account) => (
|
||||||
<Flex
|
<Flex
|
||||||
column
|
column
|
||||||
key={account.address + account.name}
|
key={account.address + account.name}
|
||||||
@@ -383,28 +391,37 @@ const Accounts: FC<AccountProps> = props => {
|
|||||||
</Text>
|
</Text>
|
||||||
</Box>
|
</Box>
|
||||||
{!props.hideDeployBtn && (
|
{!props.hideDeployBtn && (
|
||||||
<Button
|
<div
|
||||||
css={{ ml: "auto" }}
|
onClick={(e) => {
|
||||||
size="xs"
|
e.preventDefault();
|
||||||
uppercase
|
|
||||||
isLoading={account.isLoading}
|
|
||||||
disabled={
|
|
||||||
account.isLoading ||
|
|
||||||
!snap.files.filter(file => file.compiledWatContent).length
|
|
||||||
}
|
|
||||||
variant="secondary"
|
|
||||||
onClick={e => {
|
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
deployHook(account);
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Deploy
|
<Button
|
||||||
</Button>
|
css={{ ml: "auto" }}
|
||||||
|
size="xs"
|
||||||
|
uppercase
|
||||||
|
isLoading={account.isLoading}
|
||||||
|
disabled={
|
||||||
|
account.isLoading ||
|
||||||
|
!snap.files.filter((file) => file.compiledWatContent)
|
||||||
|
.length
|
||||||
|
}
|
||||||
|
variant="secondary"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
deployHook(account);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Deploy
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</Flex>
|
</Flex>
|
||||||
{props.showHookStats && (
|
{props.showHookStats && (
|
||||||
<Text muted small css={{ mt: "$2" }}>
|
<Text muted small css={{ mt: "$2" }}>
|
||||||
{account.hooks.length} hook{account.hooks.length === 1 ? "" : "s"} installed
|
{account.hooks.length} hook
|
||||||
|
{account.hooks.length === 1 ? "" : "s"} installed
|
||||||
</Text>
|
</Text>
|
||||||
)}
|
)}
|
||||||
</Flex>
|
</Flex>
|
||||||
@@ -436,7 +453,7 @@ const ImportAccountDialog = () => {
|
|||||||
name="secret"
|
name="secret"
|
||||||
type="password"
|
type="password"
|
||||||
value={value}
|
value={value}
|
||||||
onChange={e => setValue(e.target.value)}
|
onChange={(e) => setValue(e.target.value)}
|
||||||
/>
|
/>
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
|
|
||||||
|
|||||||
@@ -1,84 +1,136 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useCallback, useEffect } from "react";
|
||||||
import { useSnapshot } from "valtio";
|
import { proxy, ref, useSnapshot } from "valtio";
|
||||||
import { Select } from ".";
|
import { Select } from ".";
|
||||||
import state from "../state";
|
import state, { ILog } from "../state";
|
||||||
|
import { extractJSON } from "../utils/json";
|
||||||
import LogBox from "./LogBox";
|
import LogBox from "./LogBox";
|
||||||
import Text from "./Text";
|
|
||||||
|
interface ISelect<T = string> {
|
||||||
|
label: string;
|
||||||
|
value: T;
|
||||||
|
}
|
||||||
|
|
||||||
|
const streamState = proxy({
|
||||||
|
selectedAccount: null as ISelect | null,
|
||||||
|
logs: [] as ILog[],
|
||||||
|
socket: undefined as WebSocket | undefined,
|
||||||
|
});
|
||||||
|
|
||||||
const DebugStream = () => {
|
const DebugStream = () => {
|
||||||
const snap = useSnapshot(state);
|
const { selectedAccount, logs, socket } = useSnapshot(streamState);
|
||||||
|
const { accounts } = useSnapshot(state);
|
||||||
|
|
||||||
const accountOptions = snap.accounts.map(acc => ({
|
const accountOptions = accounts.map(acc => ({
|
||||||
label: acc.name,
|
label: acc.name,
|
||||||
value: acc.address,
|
value: acc.address,
|
||||||
}));
|
}));
|
||||||
const [selectedAccount, setSelectedAccount] = useState<typeof accountOptions[0] | null>(null);
|
|
||||||
|
|
||||||
const renderNav = () => (
|
const renderNav = () => (
|
||||||
<>
|
<>
|
||||||
<Text css={{ mx: "$2", fontSize: "inherit" }}>Account: </Text>
|
|
||||||
<Select
|
<Select
|
||||||
instanceId="debugStreamAccount"
|
instanceId="DSAccount"
|
||||||
placeholder="Select account"
|
placeholder="Select account"
|
||||||
options={accountOptions}
|
options={accountOptions}
|
||||||
hideSelectedOptions
|
hideSelectedOptions
|
||||||
value={selectedAccount}
|
value={selectedAccount}
|
||||||
onChange={acc => setSelectedAccount(acc as any)}
|
onChange={acc => (streamState.selectedAccount = acc as any)}
|
||||||
css={{ width: "30%" }}
|
css={{ width: "100%" }}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const prepareLog = useCallback((str: any): ILog => {
|
||||||
|
if (typeof str !== "string") throw Error("Unrecognized debug log stream!");
|
||||||
|
|
||||||
|
const match = str.match(/([\s\S]+(?:UTC|ISO|GMT[+|-]\d+))\ ?([\s\S]*)/m);
|
||||||
|
const [_, tm, msg] = match || [];
|
||||||
|
|
||||||
|
const extracted = extractJSON(msg);
|
||||||
|
const timestamp = isNaN(Date.parse(tm || ""))
|
||||||
|
? tm
|
||||||
|
: new Date(tm).toLocaleTimeString();
|
||||||
|
|
||||||
|
const message = !extracted
|
||||||
|
? msg
|
||||||
|
: msg.slice(0, extracted.start) + msg.slice(extracted.end + 1);
|
||||||
|
|
||||||
|
const jsonData = extracted
|
||||||
|
? JSON.stringify(extracted.result, null, 2)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: "log",
|
||||||
|
message,
|
||||||
|
timestamp,
|
||||||
|
jsonData,
|
||||||
|
defaultCollapsed: true,
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const account = selectedAccount?.value;
|
const account = selectedAccount?.value;
|
||||||
if (!account) {
|
if (account && (!socket || !socket.url.endsWith(account))) {
|
||||||
return;
|
socket?.close();
|
||||||
|
streamState.socket = ref(
|
||||||
|
new WebSocket(
|
||||||
|
`wss://hooks-testnet-debugstream.xrpl-labs.com/${account}`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else if (!account && socket) {
|
||||||
|
socket.close();
|
||||||
|
streamState.socket = undefined;
|
||||||
}
|
}
|
||||||
const socket = new WebSocket(`wss://hooks-testnet-debugstream.xrpl-labs.com/${account}`);
|
}, [selectedAccount?.value, socket]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const account = selectedAccount?.value;
|
||||||
|
const socket = streamState.socket;
|
||||||
|
if (!socket) return;
|
||||||
|
|
||||||
const onOpen = () => {
|
const onOpen = () => {
|
||||||
state.debugLogs = [];
|
streamState.logs = [];
|
||||||
state.debugLogs.push({
|
streamState.logs.push({
|
||||||
type: "success",
|
type: "success",
|
||||||
message: `Debug stream opened for account ${account}`,
|
message: `Debug stream opened for account ${account}`,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const onError = () => {
|
const onError = () => {
|
||||||
state.debugLogs.push({
|
streamState.logs.push({
|
||||||
type: "error",
|
type: "error",
|
||||||
message: "Something went wrong in establishing connection!",
|
message: "Something went wrong! Check your connection and try again.",
|
||||||
});
|
});
|
||||||
setSelectedAccount(null);
|
};
|
||||||
|
const onClose = (e: CloseEvent) => {
|
||||||
|
streamState.logs.push({
|
||||||
|
type: "error",
|
||||||
|
message: `Connection was closed. [code: ${e.code}]`,
|
||||||
|
});
|
||||||
|
streamState.selectedAccount = null;
|
||||||
};
|
};
|
||||||
const onMessage = (event: any) => {
|
const onMessage = (event: any) => {
|
||||||
if (!event.data) return;
|
if (!event.data) return;
|
||||||
state.debugLogs.push({
|
streamState.logs.push(prepareLog(event.data));
|
||||||
type: "log",
|
|
||||||
message: event.data,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.addEventListener("open", onOpen);
|
socket.addEventListener("open", onOpen);
|
||||||
socket.addEventListener("close", onError);
|
socket.addEventListener("close", onClose);
|
||||||
socket.addEventListener("error", onError);
|
socket.addEventListener("error", onError);
|
||||||
socket.addEventListener("message", onMessage);
|
socket.addEventListener("message", onMessage);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socket.removeEventListener("open", onOpen);
|
socket.removeEventListener("open", onOpen);
|
||||||
socket.removeEventListener("close", onError);
|
socket.removeEventListener("close", onClose);
|
||||||
socket.removeEventListener("message", onMessage);
|
socket.removeEventListener("message", onMessage);
|
||||||
|
socket.removeEventListener("error", onError);
|
||||||
socket.close();
|
|
||||||
};
|
};
|
||||||
}, [selectedAccount]);
|
}, [prepareLog, selectedAccount?.value, socket]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LogBox
|
<LogBox
|
||||||
enhanced
|
enhanced
|
||||||
renderNav={renderNav}
|
renderNav={renderNav}
|
||||||
title="Debug stream"
|
title="Debug stream"
|
||||||
logs={snap.debugLogs}
|
logs={logs}
|
||||||
clearLog={() => (state.debugLogs = [])}
|
clearLog={() => (streamState.logs = [])}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -67,7 +67,6 @@ const setMarkers = (monacoE: typeof monaco) => {
|
|||||||
// exact same range (location) where the markers are
|
// exact same range (location) where the markers are
|
||||||
const models = monacoE.editor.getModels();
|
const models = monacoE.editor.getModels();
|
||||||
models.forEach((model) => {
|
models.forEach((model) => {
|
||||||
console.log(decorations);
|
|
||||||
decorations[model.id] = model?.deltaDecorations(
|
decorations[model.id] = model?.deltaDecorations(
|
||||||
decorations?.[model.id] || [],
|
decorations?.[model.id] || [],
|
||||||
markers
|
markers
|
||||||
@@ -100,7 +99,6 @@ const setMarkers = (monacoE: typeof monaco) => {
|
|||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
console.log("decorat", decorations);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const HooksEditor = () => {
|
const HooksEditor = () => {
|
||||||
@@ -138,7 +136,6 @@ const HooksEditor = () => {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<EditorNavigation />
|
<EditorNavigation />
|
||||||
{console.log(snap)}
|
|
||||||
{snap.files.length > 0 && router.isReady ? (
|
{snap.files.length > 0 && router.isReady ? (
|
||||||
<Editor
|
<Editor
|
||||||
className="hooks-editor"
|
className="hooks-editor"
|
||||||
@@ -148,7 +145,6 @@ const HooksEditor = () => {
|
|||||||
path={`file:///work/c/${snap.files?.[snap.active]?.name}`}
|
path={`file:///work/c/${snap.files?.[snap.active]?.name}`}
|
||||||
defaultValue={snap.files?.[snap.active]?.content}
|
defaultValue={snap.files?.[snap.active]?.content}
|
||||||
beforeMount={(monaco) => {
|
beforeMount={(monaco) => {
|
||||||
console.log(monaco.languages.getLanguages());
|
|
||||||
if (!snap.editorCtx) {
|
if (!snap.editorCtx) {
|
||||||
snap.files.forEach((file) =>
|
snap.files.forEach((file) =>
|
||||||
monaco.editor.createModel(
|
monaco.editor.createModel(
|
||||||
|
|||||||
@@ -3,6 +3,14 @@ import { styled } from "../stitches.config";
|
|||||||
const StyledLink = styled("a", {
|
const StyledLink = styled("a", {
|
||||||
color: "CurrentColor",
|
color: "CurrentColor",
|
||||||
textDecoration: "underline",
|
textDecoration: "underline",
|
||||||
|
cursor: 'pointer',
|
||||||
|
variants: {
|
||||||
|
highlighted: {
|
||||||
|
true: {
|
||||||
|
color: '$blue9'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export default StyledLink;
|
export default StyledLink;
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
import React, { useRef, useLayoutEffect, ReactNode } from "react";
|
import { useRef, useLayoutEffect, ReactNode, FC, useState, useCallback } from "react";
|
||||||
import { Notepad, Prohibit } from "phosphor-react";
|
import { Notepad, Prohibit } from "phosphor-react";
|
||||||
import useStayScrolled from "react-stay-scrolled";
|
import useStayScrolled from "react-stay-scrolled";
|
||||||
import NextLink from "next/link";
|
import NextLink from "next/link";
|
||||||
|
|
||||||
import Container from "./Container";
|
import Container from "./Container";
|
||||||
import Box from "./Box";
|
|
||||||
import Flex from "./Flex";
|
|
||||||
import LogText from "./LogText";
|
import LogText from "./LogText";
|
||||||
import { ILog } from "../state";
|
import state, { ILog } from "../state";
|
||||||
import Text from "./Text";
|
import { Pre, Link, Heading, Button, Text, Flex, Box } from ".";
|
||||||
import Button from "./Button";
|
import regexifyString from "regexify-string";
|
||||||
import Heading from "./Heading";
|
import { useSnapshot } from "valtio";
|
||||||
import Link from "./Link";
|
import { AccountDialog } from "./Accounts";
|
||||||
|
|
||||||
interface ILogBox {
|
interface ILogBox {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -21,14 +19,7 @@ interface ILogBox {
|
|||||||
enhanced?: boolean;
|
enhanced?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const LogBox: React.FC<ILogBox> = ({
|
const LogBox: FC<ILogBox> = ({ title, clearLog, logs, children, renderNav, enhanced }) => {
|
||||||
title,
|
|
||||||
clearLog,
|
|
||||||
logs,
|
|
||||||
children,
|
|
||||||
renderNav,
|
|
||||||
enhanced,
|
|
||||||
}) => {
|
|
||||||
const logRef = useRef<HTMLPreElement>(null);
|
const logRef = useRef<HTMLPreElement>(null);
|
||||||
const { stayScrolled /*, scrollBottom*/ } = useStayScrolled(logRef);
|
const { stayScrolled /*, scrollBottom*/ } = useStayScrolled(logRef);
|
||||||
|
|
||||||
@@ -55,6 +46,7 @@ const LogBox: React.FC<ILogBox> = ({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Flex
|
<Flex
|
||||||
|
fluid
|
||||||
css={{
|
css={{
|
||||||
height: "48px",
|
height: "48px",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
@@ -78,7 +70,15 @@ const LogBox: React.FC<ILogBox> = ({
|
|||||||
>
|
>
|
||||||
<Notepad size="15px" /> <Text css={{ lineHeight: 1 }}>{title}</Text>
|
<Notepad size="15px" /> <Text css={{ lineHeight: 1 }}>{title}</Text>
|
||||||
</Heading>
|
</Heading>
|
||||||
{renderNav?.()}
|
<Flex
|
||||||
|
row
|
||||||
|
align="center"
|
||||||
|
css={{
|
||||||
|
width: "50%", // TODO make it max without breaking layout!
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{renderNav?.()}
|
||||||
|
</Flex>
|
||||||
<Flex css={{ ml: "auto", gap: "$3", marginRight: "$3" }}>
|
<Flex css={{ ml: "auto", gap: "$3", marginRight: "$3" }}>
|
||||||
{clearLog && (
|
{clearLog && (
|
||||||
<Button ghost size="xs" onClick={clearLog}>
|
<Button ghost size="xs" onClick={clearLog}>
|
||||||
@@ -117,17 +117,11 @@ const LogBox: React.FC<ILogBox> = ({
|
|||||||
backgroundColor: enhanced ? "$backgroundAlt" : undefined,
|
backgroundColor: enhanced ? "$backgroundAlt" : undefined,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
p: enhanced ? "$2 $1" : undefined,
|
p: enhanced ? "$1" : undefined,
|
||||||
|
my: enhanced ? "$1" : undefined,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<LogText variant={log.type}>
|
<Log {...log} />
|
||||||
{log.message}{" "}
|
|
||||||
{log.link && (
|
|
||||||
<NextLink href={log.link} shallow passHref>
|
|
||||||
<Link as="a">{log.linkText}</Link>
|
|
||||||
</NextLink>
|
|
||||||
)}
|
|
||||||
</LogText>
|
|
||||||
</Box>
|
</Box>
|
||||||
))}
|
))}
|
||||||
{children}
|
{children}
|
||||||
@@ -137,4 +131,74 @@ const LogBox: React.FC<ILogBox> = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const Log: FC<ILog> = ({
|
||||||
|
type,
|
||||||
|
timestamp: timestamp,
|
||||||
|
message: _message,
|
||||||
|
link,
|
||||||
|
linkText,
|
||||||
|
defaultCollapsed,
|
||||||
|
jsonData: _jsonData,
|
||||||
|
}) => {
|
||||||
|
const [expanded, setExpanded] = useState(!defaultCollapsed);
|
||||||
|
const { accounts } = useSnapshot(state);
|
||||||
|
const [dialogAccount, setDialogAccount] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const enrichAccounts = useCallback(
|
||||||
|
(str?: string): ReactNode => {
|
||||||
|
if (!str || !accounts.length) return null;
|
||||||
|
|
||||||
|
const pattern = `(${accounts.map(acc => acc.address).join("|")})`;
|
||||||
|
const res = regexifyString({
|
||||||
|
pattern: new RegExp(pattern, "gim"),
|
||||||
|
decorator: (match, idx) => {
|
||||||
|
const name = accounts.find(acc => acc.address === match)?.name;
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
key={match + idx}
|
||||||
|
as="a"
|
||||||
|
onClick={() => setDialogAccount(match)}
|
||||||
|
title={match}
|
||||||
|
highlighted
|
||||||
|
>
|
||||||
|
{name || match}
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
input: str,
|
||||||
|
});
|
||||||
|
|
||||||
|
return <>{res}</>;
|
||||||
|
},
|
||||||
|
[accounts]
|
||||||
|
);
|
||||||
|
_message = _message.trim().replace(/\n /gi, "\n");
|
||||||
|
const message = enrichAccounts(_message);
|
||||||
|
const jsonData = enrichAccounts(_jsonData);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<AccountDialog
|
||||||
|
setActiveAccountAddress={setDialogAccount}
|
||||||
|
activeAccountAddress={dialogAccount}
|
||||||
|
/>
|
||||||
|
<LogText variant={type}>
|
||||||
|
{timestamp && <Text muted monospace>{timestamp} </Text>}
|
||||||
|
<Pre>{message} </Pre>
|
||||||
|
{link && (
|
||||||
|
<NextLink href={link} shallow passHref>
|
||||||
|
<Link as="a">{linkText}</Link>
|
||||||
|
</NextLink>
|
||||||
|
)}
|
||||||
|
{jsonData && (
|
||||||
|
<Link onClick={() => setExpanded(!expanded)} as="a">
|
||||||
|
{expanded ? "Collapse" : "Expand"}
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
{expanded && jsonData && <Pre block>{jsonData}</Pre>}
|
||||||
|
</LogText>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default LogBox;
|
export default LogBox;
|
||||||
|
|||||||
27
components/Pre.tsx
Normal file
27
components/Pre.tsx
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { styled } from "../stitches.config";
|
||||||
|
|
||||||
|
const Pre = styled("span", {
|
||||||
|
m: 0,
|
||||||
|
wordBreak: "break-all",
|
||||||
|
fontFamily: '$monospace',
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
variants: {
|
||||||
|
fluid: {
|
||||||
|
true: {
|
||||||
|
width: "100%",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
line: {
|
||||||
|
true: {
|
||||||
|
whiteSpace: 'pre-line'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
block: {
|
||||||
|
true: {
|
||||||
|
display: 'block'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default Pre;
|
||||||
@@ -14,6 +14,11 @@ const Text = styled("span", {
|
|||||||
true: {
|
true: {
|
||||||
color: '$mauve9'
|
color: '$mauve9'
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
monospace: {
|
||||||
|
true: {
|
||||||
|
fontFamily: '$monospace'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ export * from "./Tabs";
|
|||||||
export * from "./AlertDialog";
|
export * from "./AlertDialog";
|
||||||
export { default as Box } from "./Box";
|
export { default as Box } from "./Box";
|
||||||
export { default as Button } from "./Button";
|
export { default as Button } from "./Button";
|
||||||
|
export { default as Pre } from "./Pre";
|
||||||
export { default as ButtonGroup } from "./ButtonGroup";
|
export { default as ButtonGroup } from "./ButtonGroup";
|
||||||
export { default as DeployFooter } from "./DeployFooter";
|
export { default as DeployFooter } from "./DeployFooter";
|
||||||
export * from "./Dialog";
|
export * from "./Dialog";
|
||||||
|
|||||||
@@ -48,6 +48,7 @@
|
|||||||
"react-stay-scrolled": "^7.4.0",
|
"react-stay-scrolled": "^7.4.0",
|
||||||
"react-time-ago": "^7.1.9",
|
"react-time-ago": "^7.1.9",
|
||||||
"reconnecting-websocket": "^4.4.0",
|
"reconnecting-websocket": "^4.4.0",
|
||||||
|
"regexify-string": "^1.0.17",
|
||||||
"valtio": "^1.2.5",
|
"valtio": "^1.2.5",
|
||||||
"vscode-languageserver": "^7.0.0",
|
"vscode-languageserver": "^7.0.0",
|
||||||
"vscode-uri": "^3.0.2",
|
"vscode-uri": "^3.0.2",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import state from "../state";
|
|||||||
|
|
||||||
import TimeAgo from "javascript-time-ago";
|
import TimeAgo from "javascript-time-ago";
|
||||||
import en from "javascript-time-ago/locale/en.json";
|
import en from "javascript-time-ago/locale/en.json";
|
||||||
|
import { useSnapshot } from "valtio";
|
||||||
TimeAgo.addDefaultLocale(en);
|
TimeAgo.addDefaultLocale(en);
|
||||||
|
|
||||||
function MyApp({ Component, pageProps: { session, ...pageProps } }: AppProps) {
|
function MyApp({ Component, pageProps: { session, ...pageProps } }: AppProps) {
|
||||||
@@ -25,15 +26,29 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }: AppProps) {
|
|||||||
const origin = "https://xrpl-hooks-ide.vercel.app"; // TODO: Change when site is deployed
|
const origin = "https://xrpl-hooks-ide.vercel.app"; // TODO: Change when site is deployed
|
||||||
const shareImg = "/share-image.png";
|
const shareImg = "/share-image.png";
|
||||||
|
|
||||||
|
const snap = useSnapshot(state);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (gistId && router.isReady) {
|
if (gistId && router.isReady) {
|
||||||
fetchFiles(gistId);
|
fetchFiles(gistId);
|
||||||
} else {
|
} else {
|
||||||
if (!gistId && router.isReady && !router.pathname.includes("/sign-in")) {
|
if (
|
||||||
|
!gistId &&
|
||||||
|
router.isReady &&
|
||||||
|
!router.pathname.includes("/sign-in") &&
|
||||||
|
!snap.files.length &&
|
||||||
|
!snap.mainModalShowed
|
||||||
|
) {
|
||||||
state.mainModalOpen = true;
|
state.mainModalOpen = true;
|
||||||
|
state.mainModalShowed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [gistId, router.isReady, router.pathname]);
|
}, [
|
||||||
|
gistId,
|
||||||
|
router.isReady,
|
||||||
|
router.pathname,
|
||||||
|
snap.files,
|
||||||
|
snap.mainModalShowed,
|
||||||
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import React from "react";
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
|
import React from "react";
|
||||||
|
import Split from "react-split";
|
||||||
import { useSnapshot } from "valtio";
|
import { useSnapshot } from "valtio";
|
||||||
import state from "../../state";
|
import state from "../../state";
|
||||||
import Split from "react-split";
|
import { getSplit, saveSplit } from "../../state/actions/persistSplits";
|
||||||
|
|
||||||
const DeployEditor = dynamic(() => import("../../components/DeployEditor"), {
|
const DeployEditor = dynamic(() => import("../../components/DeployEditor"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@@ -17,21 +18,22 @@ const LogBox = dynamic(() => import("../../components/LogBox"), {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const Deploy = () => {
|
const Deploy = () => {
|
||||||
const snap = useSnapshot(state);
|
const { deployLogs } = useSnapshot(state);
|
||||||
return (
|
return (
|
||||||
<Split
|
<Split
|
||||||
direction="vertical"
|
direction="vertical"
|
||||||
gutterSize={4}
|
gutterSize={4}
|
||||||
gutterAlign="center"
|
gutterAlign="center"
|
||||||
sizes={[40, 60]}
|
sizes={getSplit("deployVertical") || [40, 60]}
|
||||||
style={{ height: "calc(100vh - 60px)" }}
|
style={{ height: "calc(100vh - 60px)" }}
|
||||||
|
onDragEnd={(e) => saveSplit("deployVertical", e)}
|
||||||
>
|
>
|
||||||
<main style={{ display: "flex", flex: 1, position: "relative" }}>
|
<main style={{ display: "flex", flex: 1, position: "relative" }}>
|
||||||
<DeployEditor />
|
<DeployEditor />
|
||||||
</main>
|
</main>
|
||||||
<Split
|
<Split
|
||||||
direction="horizontal"
|
direction="horizontal"
|
||||||
sizes={[50, 50]}
|
sizes={getSplit("deployHorizontal") || [50, 50]}
|
||||||
minSize={[320, 160]}
|
minSize={[320, 160]}
|
||||||
gutterSize={4}
|
gutterSize={4}
|
||||||
gutterAlign="center"
|
gutterAlign="center"
|
||||||
@@ -41,6 +43,7 @@ const Deploy = () => {
|
|||||||
width: "100%",
|
width: "100%",
|
||||||
height: "100%",
|
height: "100%",
|
||||||
}}
|
}}
|
||||||
|
onDragEnd={(e) => saveSplit("deployHorizontal", e)}
|
||||||
>
|
>
|
||||||
<div style={{ alignItems: "stretch", display: "flex" }}>
|
<div style={{ alignItems: "stretch", display: "flex" }}>
|
||||||
<Accounts />
|
<Accounts />
|
||||||
@@ -48,7 +51,7 @@ const Deploy = () => {
|
|||||||
<div>
|
<div>
|
||||||
<LogBox
|
<LogBox
|
||||||
title="Deploy Log"
|
title="Deploy Log"
|
||||||
logs={snap.deployLogs}
|
logs={deployLogs}
|
||||||
clearLog={() => (state.deployLogs = [])}
|
clearLog={() => (state.deployLogs = [])}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import dynamic from "next/dynamic";
|
|
||||||
import { useSnapshot } from "valtio";
|
|
||||||
import Hotkeys from "react-hot-keys";
|
|
||||||
import { Play } from "phosphor-react";
|
|
||||||
import Split from "react-split";
|
|
||||||
|
|
||||||
import type { NextPage } from "next";
|
import type { NextPage } from "next";
|
||||||
import { compileCode } from "../../state/actions";
|
import dynamic from "next/dynamic";
|
||||||
import state from "../../state";
|
import { Play } from "phosphor-react";
|
||||||
import Button from "../../components/Button";
|
import Hotkeys from "react-hot-keys";
|
||||||
|
import Split from "react-split";
|
||||||
|
import { useSnapshot } from "valtio";
|
||||||
import Box from "../../components/Box";
|
import Box from "../../components/Box";
|
||||||
|
import Button from "../../components/Button";
|
||||||
|
import state from "../../state";
|
||||||
|
import { compileCode } from "../../state/actions";
|
||||||
|
import { getSplit, saveSplit } from "../../state/actions/persistSplits";
|
||||||
|
|
||||||
|
|
||||||
const HooksEditor = dynamic(() => import("../../components/HooksEditor"), {
|
const HooksEditor = dynamic(() => import("../../components/HooksEditor"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@@ -24,11 +25,12 @@ const Home: NextPage = () => {
|
|||||||
return (
|
return (
|
||||||
<Split
|
<Split
|
||||||
direction="vertical"
|
direction="vertical"
|
||||||
sizes={[70, 30]}
|
sizes={getSplit("developVertical") || [70, 30]}
|
||||||
minSize={[100, 100]}
|
minSize={[100, 100]}
|
||||||
gutterAlign="center"
|
gutterAlign="center"
|
||||||
gutterSize={4}
|
gutterSize={4}
|
||||||
style={{ height: "calc(100vh - 60px)" }}
|
style={{ height: "calc(100vh - 60px)" }}
|
||||||
|
onDragEnd={(e) => saveSplit("developVertical", e)}
|
||||||
>
|
>
|
||||||
<main style={{ display: "flex", flex: 1, position: "relative" }}>
|
<main style={{ display: "flex", flex: 1, position: "relative" }}>
|
||||||
<HooksEditor />
|
<HooksEditor />
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
import {
|
|
||||||
Container,
|
|
||||||
Flex,
|
|
||||||
Box,
|
|
||||||
Tabs,
|
|
||||||
Tab,
|
|
||||||
Input,
|
|
||||||
Select,
|
|
||||||
Text,
|
|
||||||
Button,
|
|
||||||
} from "../../components";
|
|
||||||
import { Play } from "phosphor-react";
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import { useSnapshot } from "valtio";
|
import { Play } from "phosphor-react";
|
||||||
|
import { FC, useCallback, useEffect, useState } from "react";
|
||||||
import Split from "react-split";
|
import Split from "react-split";
|
||||||
|
import { useSnapshot } from "valtio";
|
||||||
|
import {
|
||||||
|
Box, Button, Container,
|
||||||
|
Flex, Input,
|
||||||
|
Select, Tab, Tabs, Text
|
||||||
|
} from "../../components";
|
||||||
|
import transactionsData from "../../content/transactions.json";
|
||||||
import state from "../../state";
|
import state from "../../state";
|
||||||
import { sendTransaction } from "../../state/actions";
|
import { sendTransaction } from "../../state/actions";
|
||||||
import { useCallback, useEffect, useState, FC } from "react";
|
import { getSplit, saveSplit } from "../../state/actions/persistSplits";
|
||||||
import transactionsData from "../../content/transactions.json";
|
|
||||||
|
|
||||||
const DebugStream = dynamic(() => import("../../components/DebugStream"), {
|
const DebugStream = dynamic(() => import("../../components/DebugStream"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@@ -349,16 +344,17 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const Test = () => {
|
const Test = () => {
|
||||||
const snap = useSnapshot(state);
|
const { transactionLogs } = useSnapshot(state);
|
||||||
const [tabHeaders, setTabHeaders] = useState<string[]>(["test1.json"]);
|
const [tabHeaders, setTabHeaders] = useState<string[]>(["test1.json"]);
|
||||||
return (
|
return (
|
||||||
<Container css={{ px: 0 }}>
|
<Container css={{ px: 0 }}>
|
||||||
<Split
|
<Split
|
||||||
direction="vertical"
|
direction="vertical"
|
||||||
sizes={[50, 50]}
|
sizes={getSplit("testVertical") || [50, 50]}
|
||||||
gutterSize={4}
|
gutterSize={4}
|
||||||
gutterAlign="center"
|
gutterAlign="center"
|
||||||
style={{ height: "calc(100vh - 60px)" }}
|
style={{ height: "calc(100vh - 60px)" }}
|
||||||
|
onDragEnd={(e) => saveSplit("testVertical", e)}
|
||||||
>
|
>
|
||||||
<Flex
|
<Flex
|
||||||
row
|
row
|
||||||
@@ -370,7 +366,7 @@ const Test = () => {
|
|||||||
>
|
>
|
||||||
<Split
|
<Split
|
||||||
direction="horizontal"
|
direction="horizontal"
|
||||||
sizes={[50, 50]}
|
sizes={getSplit("testHorizontal") || [50, 50]}
|
||||||
minSize={[180, 320]}
|
minSize={[180, 320]}
|
||||||
gutterSize={4}
|
gutterSize={4}
|
||||||
gutterAlign="center"
|
gutterAlign="center"
|
||||||
@@ -380,6 +376,7 @@ const Test = () => {
|
|||||||
width: "100%",
|
width: "100%",
|
||||||
height: "100%",
|
height: "100%",
|
||||||
}}
|
}}
|
||||||
|
onDragEnd={(e) => saveSplit("testHorizontal", e)}
|
||||||
>
|
>
|
||||||
<Box css={{ width: "55%", px: "$2" }}>
|
<Box css={{ width: "55%", px: "$2" }}>
|
||||||
<Tabs
|
<Tabs
|
||||||
@@ -428,7 +425,7 @@ const Test = () => {
|
|||||||
>
|
>
|
||||||
<LogBox
|
<LogBox
|
||||||
title="Development Log"
|
title="Development Log"
|
||||||
logs={snap.transactionLogs}
|
logs={transactionLogs}
|
||||||
clearLog={() => (state.transactionLogs = [])}
|
clearLog={() => (state.transactionLogs = [])}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
15
state/actions/persistSplits.ts
Normal file
15
state/actions/persistSplits.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { snapshot } from "valtio"
|
||||||
|
import state from ".."
|
||||||
|
|
||||||
|
export type SplitSize = number[]
|
||||||
|
|
||||||
|
export const saveSplit = (splitId: string, event: SplitSize) => {
|
||||||
|
state.splits[splitId] = event
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getSplit = (splitId: string): SplitSize | null => {
|
||||||
|
const { splits } = snapshot(state)
|
||||||
|
const split = splits[splitId]
|
||||||
|
return split ? split : null
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import { proxy, ref, subscribe } from "valtio";
|
|
||||||
import { devtools } from 'valtio/utils'
|
|
||||||
import type monaco from "monaco-editor";
|
import type monaco from "monaco-editor";
|
||||||
|
import { proxy, ref, subscribe } from "valtio";
|
||||||
|
import { devtools } from 'valtio/utils';
|
||||||
import { XrplClient } from "xrpl-client";
|
import { XrplClient } from "xrpl-client";
|
||||||
|
import { SplitSize } from "./actions/persistSplits";
|
||||||
|
|
||||||
export interface IFile {
|
export interface IFile {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -33,8 +34,11 @@ export interface IAccount {
|
|||||||
export interface ILog {
|
export interface ILog {
|
||||||
type: "error" | "warning" | "log" | "success";
|
type: "error" | "warning" | "log" | "success";
|
||||||
message: string;
|
message: string;
|
||||||
|
jsonData?: any,
|
||||||
|
timestamp?: string;
|
||||||
link?: string;
|
link?: string;
|
||||||
linkText?: string;
|
linkText?: string;
|
||||||
|
defaultCollapsed?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IState {
|
export interface IState {
|
||||||
@@ -51,14 +55,17 @@ export interface IState {
|
|||||||
logs: ILog[];
|
logs: ILog[];
|
||||||
deployLogs: ILog[];
|
deployLogs: ILog[];
|
||||||
transactionLogs: ILog[];
|
transactionLogs: ILog[];
|
||||||
debugLogs: ILog[];
|
|
||||||
editorCtx?: typeof monaco.editor;
|
editorCtx?: typeof monaco.editor;
|
||||||
editorSettings: {
|
editorSettings: {
|
||||||
tabSize: number;
|
tabSize: number;
|
||||||
};
|
};
|
||||||
|
splits: {
|
||||||
|
[id: string]: SplitSize
|
||||||
|
};
|
||||||
client: XrplClient | null;
|
client: XrplClient | null;
|
||||||
clientStatus: "offline" | "online";
|
clientStatus: "offline" | "online";
|
||||||
mainModalOpen: boolean;
|
mainModalOpen: boolean;
|
||||||
|
mainModalShowed: boolean;
|
||||||
accounts: IAccount[];
|
accounts: IAccount[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +81,6 @@ let initialState: IState = {
|
|||||||
logs: [],
|
logs: [],
|
||||||
deployLogs: [],
|
deployLogs: [],
|
||||||
transactionLogs: [],
|
transactionLogs: [],
|
||||||
debugLogs: [],
|
|
||||||
editorCtx: undefined,
|
editorCtx: undefined,
|
||||||
gistId: undefined,
|
gistId: undefined,
|
||||||
gistOwner: undefined,
|
gistOwner: undefined,
|
||||||
@@ -84,14 +90,19 @@ let initialState: IState = {
|
|||||||
editorSettings: {
|
editorSettings: {
|
||||||
tabSize: 2,
|
tabSize: 2,
|
||||||
},
|
},
|
||||||
|
splits: {},
|
||||||
client: null,
|
client: null,
|
||||||
clientStatus: "offline" as "offline",
|
clientStatus: "offline" as "offline",
|
||||||
mainModalOpen: false,
|
mainModalOpen: false,
|
||||||
|
mainModalShowed: false,
|
||||||
accounts: [],
|
accounts: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
let localStorageAccounts: string | null = null;
|
let localStorageAccounts: string | null = null;
|
||||||
let initialAccounts: IAccount[] = [];
|
let initialAccounts: IAccount[] = [];
|
||||||
|
|
||||||
|
// TODO: What exactly should we store in localStorage? editorSettings, splits, accounts?
|
||||||
|
|
||||||
// Check if there's a persited accounts in localStorage
|
// Check if there's a persited accounts in localStorage
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
try {
|
try {
|
||||||
|
|||||||
21
utils/json.ts
Normal file
21
utils/json.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
export const extractJSON = (str?: string) => {
|
||||||
|
if (!str) return
|
||||||
|
let firstOpen = 0, firstClose = 0, candidate = '';
|
||||||
|
firstOpen = str.indexOf('{', firstOpen + 1);
|
||||||
|
do {
|
||||||
|
firstClose = str.lastIndexOf('}');
|
||||||
|
if (firstClose <= firstOpen) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
candidate = str.substring(firstOpen, firstClose + 1);
|
||||||
|
try {
|
||||||
|
let result = JSON.parse(candidate);
|
||||||
|
return { result, start: firstOpen < 0 ? 0 : firstOpen, end: firstClose }
|
||||||
|
}
|
||||||
|
catch (e) { }
|
||||||
|
firstClose = str.substring(0, firstClose).lastIndexOf('}');
|
||||||
|
} while (firstClose > firstOpen);
|
||||||
|
firstOpen = str.indexOf('{', firstOpen + 1);
|
||||||
|
} while (firstOpen != -1);
|
||||||
|
}
|
||||||
@@ -4045,6 +4045,11 @@ regenerator-runtime@^0.13.4:
|
|||||||
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52"
|
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52"
|
||||||
integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==
|
integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==
|
||||||
|
|
||||||
|
regexify-string@^1.0.17:
|
||||||
|
version "1.0.17"
|
||||||
|
resolved "https://registry.yarnpkg.com/regexify-string/-/regexify-string-1.0.17.tgz#b9e571b51c8ec566eb82b7121744dae0d8e829de"
|
||||||
|
integrity sha512-mmD0AUNaY/piGW2AyACWdQOjIAwNuWz+KIvxfBZPDdCBAexiROeQxdxTaYAWcIxwtUAOwojdTta6CMMil84jXw==
|
||||||
|
|
||||||
regexp.prototype.flags@^1.3.1:
|
regexp.prototype.flags@^1.3.1:
|
||||||
version "1.3.1"
|
version "1.3.1"
|
||||||
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.3.1.tgz#7ef352ae8d159e758c0eadca6f8fcb4eef07be26"
|
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.3.1.tgz#7ef352ae8d159e758c0eadca6f8fcb4eef07be26"
|
||||||
|
|||||||
Reference in New Issue
Block a user