From e55f48bc833b8a673c5070b54858fd03aaf42dab Mon Sep 17 00:00:00 2001 From: muzam1l Date: Tue, 5 Jul 2022 18:45:52 +0530 Subject: [PATCH] Use Single LogBox comp for scripts too --- components/LogBox.tsx | 12 +- components/LogBoxForScripts.tsx | 234 -------------------------------- pages/develop/[[...slug]].tsx | 7 +- pages/test/[[...slug]].tsx | 29 ++-- 4 files changed, 31 insertions(+), 251 deletions(-) delete mode 100644 components/LogBoxForScripts.tsx diff --git a/components/LogBox.tsx b/components/LogBox.tsx index 6dfe8fd..1f7a31a 100644 --- a/components/LogBox.tsx +++ b/components/LogBox.tsx @@ -6,7 +6,7 @@ import { useState, useCallback, } from "react"; -import { Notepad, Prohibit } from "phosphor-react"; +import { IconProps, Notepad, Prohibit } from "phosphor-react"; import useStayScrolled from "react-stay-scrolled"; import NextLink from "next/link"; @@ -24,6 +24,7 @@ interface ILogBox { logs: ILog[]; renderNav?: () => ReactNode; enhanced?: boolean; + Icon?: FC } const LogBox: FC = ({ @@ -33,6 +34,7 @@ const LogBox: FC = ({ children, renderNav, enhanced, + Icon = Notepad }) => { const logRef = useRef(null); const { stayScrolled /*, scrollBottom*/ } = useStayScrolled(logRef); @@ -82,14 +84,14 @@ const LogBox: FC = ({ gap: "$3", }} > - {title} + {title} {renderNav?.()} diff --git a/components/LogBoxForScripts.tsx b/components/LogBoxForScripts.tsx deleted file mode 100644 index 3bdcb25..0000000 --- a/components/LogBoxForScripts.tsx +++ /dev/null @@ -1,234 +0,0 @@ -import { - useRef, - useLayoutEffect, - ReactNode, - FC, - useState, - useCallback, -} from "react"; -import { FileJs, Prohibit } from "phosphor-react"; -import useStayScrolled from "react-stay-scrolled"; -import NextLink from "next/link"; - -import Container from "./Container"; -import LogText from "./LogText"; -import state, { ILog } from "../state"; -import { Pre, Link, Heading, Button, Text, Flex, Box } from "."; -import regexifyString from "regexify-string"; -import { useSnapshot } from "valtio"; -import { AccountDialog } from "./Accounts"; -import RunScript from "./RunScript"; - -interface ILogBox { - title: string; - clearLog?: () => void; - logs: ILog[]; - renderNav?: () => ReactNode; - enhanced?: boolean; - showButtons?: boolean; -} - -const LogBox: FC = ({ - title, - clearLog, - logs, - children, - renderNav, - enhanced, - showButtons = true, -}) => { - const logRef = useRef(null); - const { stayScrolled /*, scrollBottom*/ } = useStayScrolled(logRef); - const snap = useSnapshot(state); - useLayoutEffect(() => { - stayScrolled(); - }, [stayScrolled, logs]); - - return ( - - - - - {title} - - {showButtons && ( - - {snap.files - .filter((f) => f.name.endsWith(".js")) - .map((file) => ( - - ))} - - )} - - {clearLog && ( - - )} - - - - {logs?.map((log, index) => ( - - - - ))} - {children} - - - - ); -}; - -export const Log: FC = ({ - type, - timestring, - message: _message, - link, - linkText, - defaultCollapsed, - jsonData: _jsonData, -}) => { - const [expanded, setExpanded] = useState(!defaultCollapsed); - const { accounts } = useSnapshot(state); - const [dialogAccount, setDialogAccount] = useState(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 ( - setDialogAccount(match)} - title={match} - highlighted - > - {name || match} - - ); - }, - input: str, - }); - - return <>{res}; - }, - [accounts] - ); - - let message: ReactNode; - - if (typeof _message === "string") { - _message = _message.trim().replace(/\n /gi, "\n"); - message = enrichAccounts(_message); - } else { - message = _message; - } - - const jsonData = enrichAccounts(_jsonData); - - return ( - <> - - - {timestring && ( - - {timestring}{" "} - - )} -
{message} 
- {link && ( - - {linkText} - - )} - {jsonData && ( - setExpanded(!expanded)} as="a"> - {expanded ? "Collapse" : "Expand"} - - )} - {expanded && jsonData &&
{jsonData}
} -
-
- - ); -}; - -export default LogBox; diff --git a/pages/develop/[[...slug]].tsx b/pages/develop/[[...slug]].tsx index 58144f7..bfca87e 100644 --- a/pages/develop/[[...slug]].tsx +++ b/pages/develop/[[...slug]].tsx @@ -1,14 +1,13 @@ import { Label } from "@radix-ui/react-label"; import type { NextPage } from "next"; import dynamic from "next/dynamic"; -import { Gear, Play } from "phosphor-react"; +import { FileJs, Gear, Play } from "phosphor-react"; import Hotkeys from "react-hot-keys"; import Split from "react-split"; import { useSnapshot } from "valtio"; import { ButtonGroup, Flex } from "../../components"; import Box from "../../components/Box"; import Button from "../../components/Button"; -import LogBoxForScripts from "../../components/LogBoxForScripts"; import Popover from "../../components/Popover"; import RunScript from "../../components/RunScript"; import state from "../../state"; @@ -244,8 +243,8 @@ const Home: NextPage = () => { flex: 1, }} > - (state.scriptLogs = [])} diff --git a/pages/test/[[...slug]].tsx b/pages/test/[[...slug]].tsx index e5a0833..4f1a2dd 100644 --- a/pages/test/[[...slug]].tsx +++ b/pages/test/[[...slug]].tsx @@ -6,8 +6,9 @@ import Transaction from "../../components/Transaction"; import state from "../../state"; import { getSplit, saveSplit } from "../../state/actions/persistSplits"; import { transactionsState, modifyTransaction } from "../../state"; -import LogBoxForScripts from "../../components/LogBoxForScripts"; import { useEffect, useState } from "react"; +import { FileJs } from "phosphor-react"; +import RunScript from '../../components/RunScript'; const DebugStream = dynamic(() => import("../../components/DebugStream"), { ssr: false, @@ -33,7 +34,17 @@ const Test = () => { return null; } const hasScripts = Boolean( - snap.files.filter((f) => f.name.toLowerCase()?.endsWith(".js")).length + snap.files.filter(f => f.name.toLowerCase()?.endsWith(".js")).length + ); + + const renderNav = () => ( + + {snap.files + .filter(f => f.name.endsWith(".js")) + .map(file => ( + + ))} + ); return ( @@ -50,7 +61,7 @@ const Test = () => { gutterSize={4} gutterAlign="center" style={{ height: "calc(100vh - 60px)" }} - onDragEnd={(e) => saveSplit("testVertical", e)} + onDragEnd={e => saveSplit("testVertical", e)} > { width: "100%", height: "100%", }} - onDragEnd={(e) => saveSplit("testHorizontal", e)} + onDragEnd={e => saveSplit("testHorizontal", e)} > { @@ -84,8 +95,8 @@ const Test = () => { }} keepAllAlive defaultExtension="json" - allowedExtensions={['json']} - onCreateNewTab={(header) => modifyTransaction(header, {})} + allowedExtensions={["json"]} + onCreateNewTab={header => modifyTransaction(header, {})} onCloseTab={(idx, header) => header && modifyTransaction(header, undefined) } @@ -111,10 +122,12 @@ const Test = () => { flexDirection: "column", }} > - (state.scriptLogs = [])} + renderNav={renderNav} /> ) : null}