Skip to content

Commit

Permalink
feat(components): Json viwer and view more btn
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpeach committed Mar 16, 2023
1 parent 883c553 commit 2a0b65e
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 144 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/fund/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const AttachFund = ({
}: AttachFundProps) => {
return (
<>
<Flex mb={6}>
<Flex mb="20px">
<SelectInput
formLabel="Attach Funds"
options={attachFundsOptions}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/fund/jsonFund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ interface JsonFundProps {
assetsJson: string;
}
export const JsonFund = ({ setValue, assetsJson }: JsonFundProps) => (
<JsonInput text={assetsJson} setText={setValue} height="160px" />
<JsonInput text={assetsJson} setText={setValue} minLines={12} />
);
98 changes: 29 additions & 69 deletions src/lib/components/json/JsonEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { LayoutProps } from "@chakra-ui/react";
import { Box } from "@chakra-ui/react";
import { useEffect, useRef, useState } from "react";
import { useRef } from "react";
import AceEditor from "react-ace";

import "ace-builds/src-noconflict/ace";
Expand All @@ -12,83 +10,45 @@ interface JsonEditorProps {
setValue?: (value: string) => void;
readOnly?: boolean;
isValid: boolean;
height?: LayoutProps["height"];
showFullMsg?: boolean;
disableResizing?: boolean;
showLines: number;
}

const JsonEditor = ({
value,
setValue,
readOnly = false,
isValid,
height = "sm",
showFullMsg,
disableResizing,
showLines,
}: JsonEditorProps) => {
const editorRef = useRef<AceEditor>(null);
const [boxHeight, setBoxHeight] = useState(height);
/**
* @todos revisit later for improving height calculation method
*/
useEffect(() => {
if (showFullMsg && height !== 0) {
const contentHeight =
editorRef.current?.editor.renderer.container.clientHeight;
const parsedHeight =
typeof height === "string" ? parseInt(height, 10) : height;
const targetHeight =
contentHeight !== undefined && contentHeight > parsedHeight
? contentHeight
: height;
setBoxHeight(targetHeight);
} else {
setBoxHeight(height);
}
}, [height, showFullMsg]);

useEffect(() => {
const resize = () => {
if (editorRef && editorRef.current) {
editorRef.current.editor.resize();
}
};
document.addEventListener("mouseup", resize);
return () => document.removeEventListener("mouseup", resize);
});

return (
<Box
height={boxHeight}
resize={disableResizing ? "unset" : "vertical"}
overflow="auto"
>
<AceEditor
ref={editorRef}
mode="json"
theme="monokai"
fontSize="14px"
readOnly={readOnly}
style={{
width: "100%",
height: "99%",
background: "transparent",
color: readOnly && !isValid ? "error.light" : "white",
offset: 0,
}}
setOptions={{
tabSize: 2,
useWorker: false,
showGutter: false,
wrap: readOnly && !isValid,
printMargin: false,
maxLines: readOnly ? 9999 : undefined,
}}
onChange={setValue}
value={value}
editorProps={{ $blockScrolling: true }}
/>
</Box>
<AceEditor
ref={editorRef}
mode="json"
theme="monokai"
fontSize="14px"
readOnly={readOnly}
style={{
width: "100%",
height: "100%",
background: "transparent",
color: readOnly && !isValid ? "error.light" : "white",
offset: 0,
}}
setOptions={{
tabSize: 2,
useWorker: false,
showGutter: false,
wrap: readOnly && !isValid,
printMargin: false,
minLines: showLines,
maxLines: showLines,
}}
onChange={setValue}
value={value}
editorProps={{ $blockScrolling: true }}
/>
);
};

Expand Down
22 changes: 17 additions & 5 deletions src/lib/components/json/JsonInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { LayoutProps } from "@chakra-ui/react";
import { Text, Box, Button, Spinner, Flex } from "@chakra-ui/react";
import dynamic from "next/dynamic";
import { useState, useEffect } from "react";
import { useState, useEffect, useMemo } from "react";

import { CustomIcon } from "../icon";
import { jsonPrettify, jsonValidate } from "lib/utils";
Expand All @@ -14,7 +13,7 @@ interface JsonInputProps {
topic?: string;
text?: string;
setText: (value: string) => void;
height?: LayoutProps["height"];
minLines?: number;
}

interface JsonState {
Expand Down Expand Up @@ -70,7 +69,12 @@ const getResponse = (jsonState: JsonState) => {
}
};

const JsonInput = ({ topic, text = "", setText, height }: JsonInputProps) => {
const JsonInput = ({
topic,
text = "",
setText,
minLines = 16,
}: JsonInputProps) => {
const [jsonState, setJsonState] = useState<JsonState>({ state: "empty" });

const handleOnChange = (value: string) => {
Expand All @@ -92,6 +96,14 @@ const JsonInput = ({ topic, text = "", setText, height }: JsonInputProps) => {

const { color, response } = getResponse(jsonState);
const isValidJson = jsonValidate(text) === null;

const showLines = useMemo(() => {
const lines = text.split("\n").length;

// Limit the number of lines from minLines (default is 16) to 100
return Math.min(Math.max(lines, minLines), 100);
}, [text, minLines]);

return (
<>
<Box
Expand All @@ -109,7 +121,7 @@ const JsonInput = ({ topic, text = "", setText, height }: JsonInputProps) => {
value={text}
setValue={handleOnChange}
isValid={isValidJson}
height={height}
showLines={showLines}
/>
{topic && (
<Text
Expand Down
60 changes: 31 additions & 29 deletions src/lib/components/json/JsonReadOnly.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
import type { LayoutProps } from "@chakra-ui/react";
import { Text, Box } from "@chakra-ui/react";
import dynamic from "next/dynamic";
import { useMemo, useState } from "react";

import { CopyButton } from "../copy";
import { jsonValidate } from "lib/utils";
import { jsonLineCount, jsonValidate } from "lib/utils";

import { ViewFullMsgButton } from "./ViewFullMsgButton";

const BUTTON_EXPAND_PADDING = "60px";

const JsonEditor = dynamic(() => import("lib/components/json/JsonEditor"), {
ssr: false,
});

interface JsonReadOnlyProps {
topic?: string;
text: string;
height?: LayoutProps["height"];
canCopy?: boolean;
canViewFull?: boolean;
disableResizing?: boolean;
isExpandable?: boolean;
}

const THRESHOLD_LINES = 16;

const JsonReadOnly = ({
topic,
text,
height,
canCopy,
canViewFull,
disableResizing,
isExpandable,
}: JsonReadOnlyProps) => {
const [viewFull, setViewFull] = useState(false);

const isJsonValid = useMemo(() => {
return jsonValidate(text) === null || text.length === 0;
}, [text]);

const zeroHeight = height === 0;
const showLines = useMemo(() => {
const lineCount = jsonLineCount(text);

if (isExpandable) {
return viewFull ? lineCount : Math.min(lineCount, THRESHOLD_LINES);
}

// for query response json viewer
return Math.max(lineCount, THRESHOLD_LINES);
}, [isExpandable, text, viewFull]);

const showExpandButton = useMemo(() => {
return isExpandable && jsonLineCount(text) > THRESHOLD_LINES;
}, [isExpandable, text]);

return (
<Box
m={zeroHeight ? 0 : "0px 0 16px"}
p={
zeroHeight
? 0
: `16px 12px ${viewFull ? BUTTON_EXPAND_PADDING : "16px"}`
}
borderWidth={zeroHeight ? "none" : "thin"}
borderWidth="thin"
borderColor={!isJsonValid ? "error.main" : "pebble.700"}
borderRadius="8px"
position="relative"
transition="all .25s ease-in-out"
_hover={{
borderColor: isJsonValid && "pebble.600",
"& .copy-button-box": { display: "block" },
}}
>
<JsonEditor
value={text}
readOnly
isValid={isJsonValid}
height={height}
showFullMsg={viewFull}
disableResizing={disableResizing}
/>
<Box p="16px 12px">
<JsonEditor
value={text}
readOnly
isValid={isJsonValid}
showLines={showLines}
/>
</Box>
{!!topic && (
<Text
top="-10px"
Expand All @@ -75,13 +77,13 @@ const JsonReadOnly = ({
{topic}
</Text>
)}
{canViewFull && (
{showExpandButton && (
<ViewFullMsgButton
onClick={() => setViewFull((prev) => !prev)}
viewFull={viewFull}
/>
)}
{canCopy && !zeroHeight && (
{canCopy && (
<Box
position="absolute"
top="10px"
Expand Down
3 changes: 0 additions & 3 deletions src/lib/components/json/ViewFullMsgButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export const ViewFullMsgButton = ({
borderTopColor="pebble.700"
bg="background.main"
borderRadius="0 0 8px 8px"
position="absolute"
bottom={0}
left={0}
cursor="pointer"
_hover={{ bg: "pebble.900" }}
transition="all .25s ease-in-out"
Expand Down
21 changes: 6 additions & 15 deletions src/lib/pages/contract-details/components/JsonInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Flex, Heading } from "@chakra-ui/react";
import type { CSSProperties } from "react";
import { useState } from "react";

import { CustomIcon } from "lib/components/icon";
Expand All @@ -8,22 +7,16 @@ import JsonReadOnly from "lib/components/json/JsonReadOnly";
interface JsonInfoProps {
header: string;
jsonString: string;
showViewFullButton?: boolean;
jsonAreaHeight?: CSSProperties["height"];
defaultExpand?: boolean;
}

export const JsonInfo = ({
header,
jsonString,
showViewFullButton,
jsonAreaHeight = "300px",
defaultExpand = false,
}: JsonInfoProps) => {
const [expand, setExpand] = useState(defaultExpand);
/**
* @todos revist height and expand mechanism later
*/

return (
<>
<Flex
Expand All @@ -45,13 +38,11 @@ export const JsonInfo = ({
name={expand ? "chevron-up" : "chevron-down"}
/>
</Flex>
<JsonReadOnly
text={jsonString}
height={expand ? jsonAreaHeight : 0}
canCopy
canViewFull={showViewFullButton && expand}
disableResizing
/>
<div
style={expand ? { display: "block" } : { height: 0, display: "none" }}
>
<JsonReadOnly text={jsonString} canCopy isExpandable />
</div>
</>
);
};
2 changes: 0 additions & 2 deletions src/lib/pages/contract-details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ const ContractDetailsBody = observer(
contractData.instantiateInfo?.raw.contract_info ?? {}
)
)}
jsonAreaHeight="230px"
/>
<JsonInfo
header="Instantiate Message"
jsonString={jsonPrettify(contractData.initMsg ?? "")}
showViewFullButton
defaultExpand
/>
</Flex>
Expand Down
7 changes: 1 addition & 6 deletions src/lib/pages/execute/components/ExecuteArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,7 @@ export const ExecuteArea = ({
)}
<Flex gap="32px" mt={8} direction={{ sm: "column", lg: "row" }}>
<Box w={{ sm: "full", lg: "50%" }}>
<JsonInput
topic="Execute Msg"
text={msg}
setText={setMsg}
height="240px"
/>
<JsonInput topic="Execute Msg" text={msg} setText={setMsg} />
{error && <ErrorMessageRender error={error} mb={4} />}
</Box>
<Box w={{ sm: "full", lg: "50%" }}>
Expand Down
Loading

0 comments on commit 2a0b65e

Please sign in to comment.