Skip to content

Commit

Permalink
fix(web): jsonviewer support to render large data (labring#1731)
Browse files Browse the repository at this point in the history
* fix(web): add support to large database data

* highlight react window
  • Loading branch information
newfish-cmyk authored Dec 12, 2023
1 parent 2b87cb7 commit 382d8f2
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 28 deletions.
45 changes: 45 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"react-markdown": "^8.0.7",
"react-router-dom": "^6.11.2",
"react-syntax-highlighter": "^15.5.0",
"react-window": "^1.8.10",
"recharts": "^2.7.2",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
Expand All @@ -66,6 +67,7 @@
"@types/react-datepicker": "^4.11.2",
"@types/react-dom": "^18.2.4",
"@types/react-syntax-highlighter": "^15.5.6",
"@types/react-window": "^1.8.8",
"@types/uuid": "^9.0.1",
"@vitejs/plugin-react-swc": "^3.3.1",
"autoprefixer": "^10.4.14",
Expand All @@ -81,7 +83,6 @@
},
"lint-staged": {
"*.{ts,tsx,js}": "eslint --fix",
"*.css": "stylelint --fix",
"*.scss": "stylelint --syntax=scss --fix"
"*.{css,scss}": "stylelint --fix"
}
}
61 changes: 48 additions & 13 deletions web/src/components/Editor/JSONViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import SyntaxHighlighter from "react-syntax-highlighter";
import SyntaxHighlighter, { Prism } from "react-syntax-highlighter";
import { FixedSizeList as List } from "react-window";
import SimpleBar from "simplebar-react";

import { COLOR_MODE } from "@/constants";

import "./index.scss";

type JSONViewerProps = {
code: string;
language?: string;
Expand Down Expand Up @@ -238,28 +241,60 @@ const JSONViewerDarkStyle: any = {
fontWeight: "bold",
},
};

export default function JSONViewer(props: JSONViewerProps) {
const { code, language = "json", colorMode = COLOR_MODE.light, ...rest } = props;
const lightTheme = { background: "#fdfdfe" };
const lightTheme = { background: "#fdfdfe", padding: 0 };
const darkTheme = {
background: "#202631",
color: "#f0f0f0",
};

return (
<SimpleBar
style={{
maxHeight: 390,
}}
{...rest}
>
<SyntaxHighlighter
const rowHeight = 22;

const renderRow = ({ index, style }: { index: number; style: any }) => (
<div style={style} className="code">
<Prism
language={language}
style={colorMode === COLOR_MODE.dark ? JSONViewerDarkStyle : JSONViewerStyle}
customStyle={colorMode === COLOR_MODE.dark ? darkTheme : lightTheme}
>
{code}
</SyntaxHighlighter>
</SimpleBar>
{code.split(`\n`)[index]}
</Prism>
</div>
);

if (code.split(`\n`).length <= 100) {
return (
<SimpleBar
style={{
maxHeight: 390,
}}
{...rest}
>
<SyntaxHighlighter
language={language}
style={colorMode === COLOR_MODE.dark ? JSONViewerDarkStyle : JSONViewerStyle}
customStyle={colorMode === COLOR_MODE.dark ? darkTheme : lightTheme}
>
{code}
</SyntaxHighlighter>
</SimpleBar>
);
} else {
return (
<List
height={390}
itemCount={code.split(`\n`).length}
itemSize={rowHeight}
width="100%"
style={{
paddingTop: 0,
}}
{...rest}
>
{renderRow}
</List>
);
}
}
23 changes: 23 additions & 0 deletions web/src/components/Editor/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.code .property {
color: #A31515
}

.code .number {
color: #01a99d
}

.code .string {
color: #0451a5
}

[data-theme="dark"] .code .property {
color: #9BDCFE
}

[data-theme="dark"] .code .number {
color: #B0CAA4
}

[data-theme="dark"] .code .string {
color: #CE9178
}
24 changes: 16 additions & 8 deletions web/src/components/Pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export default function Pagination(props: {
<Button
variant="link"
className={clsx(
"!h-6 !w-6 !rounded-full bg-lafWhite-600 !p-0",
darkMode ? "" : "!text-[#262A32]",
"!h-6 !w-6 !rounded-full !p-0",
darkMode
? "hover:bg-grayModern-600"
: "bg-lafWhite-600 !text-[#262A32] hover:bg-grayModern-200",
)}
style={{ minWidth: "24px" }}
isDisabled={page === 1 || maxPage === -1}
Expand All @@ -74,8 +76,10 @@ export default function Pagination(props: {
<Button
variant="link"
className={clsx(
"!h-6 !w-6 !rounded-full bg-lafWhite-600 !p-0",
darkMode ? "" : "!text-[#262A32]",
"!h-6 !w-6 !rounded-full !p-0",
darkMode
? "hover:bg-grayModern-600"
: "bg-lafWhite-600 !text-[#262A32] hover:bg-grayModern-200",
)}
style={{ minWidth: "24px" }}
isDisabled={page === 1 || maxPage === -1}
Expand All @@ -97,8 +101,10 @@ export default function Pagination(props: {
variant="link"
isDisabled={maxPage === page || maxPage === -1}
className={clsx(
"!h-6 !w-6 !rounded-full bg-lafWhite-600 !p-0",
darkMode ? "" : "!text-[#262A32]",
"!h-6 !w-6 !rounded-full !p-0",
darkMode
? "hover:bg-grayModern-600"
: "bg-lafWhite-600 !text-[#262A32] hover:bg-grayModern-200",
)}
style={{ minWidth: "24px" }}
onClick={() => {
Expand All @@ -115,8 +121,10 @@ export default function Pagination(props: {
<Button
variant="link"
className={clsx(
"!h-6 !w-6 !rounded-full bg-lafWhite-600 !p-0",
darkMode ? "" : "!text-[#262A32]",
"!h-6 !w-6 !rounded-full !p-0",
darkMode
? "hover:bg-grayModern-600"
: "bg-lafWhite-600 !text-[#262A32] hover:bg-grayModern-200",
)}
style={{ minWidth: "24px" }}
isDisabled={maxPage === page || maxPage === -1}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.dataList .simplebar-scrollbar::before {
.simplebar-scrollbar::before {
background: #7b838b;
width: 4px;
transform: translateX(100%);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import RightPanelList from "../../../RightComponent/List";
import { useDeleteDataMutation, useEntryDataQuery, useUpdateDataMutation } from "../../../service";
import useDBMStore from "../../../store";

import "./index.css";
import "./index.scss";

import useGlobalStore from "@/pages/globalStore";

Expand Down Expand Up @@ -62,6 +62,7 @@ export default function DataPanel() {
};

const [queryData, setQueryData] = useState<QueryData>();
const darkMode = colorMode === COLOR_MODE.dark;

useEffect(() => {
if (store.currentDB !== undefined) {
Expand Down Expand Up @@ -300,15 +301,15 @@ export default function DataPanel() {
hideToolTip
text={text}
tip={String(t("Copied"))}
className="ml-2 hover:bg-gray-200"
className={darkMode ? "ml-2 hover:bg-gray-600" : "ml-2 hover:bg-gray-200"}
>
<IconWrap
showBg
tooltip={t("Copy").toString()}
size={32}
className="group/icon"
>
<OutlineCopyIcon size="14" color="#24282C" />
<OutlineCopyIcon size="14" color={darkMode ? "#ffffff" : "#24282C"} />
</IconWrap>
</CopyText>
);
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/app/database/PolicyDataList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default function PolicyDataList() {
size={32}
className="group/icon"
>
<OutlineCopyIcon size="14" color="#24282C" />
<OutlineCopyIcon size="14" color={darkMode ? "#ffffff" : "#24282C"} />
</IconWrap>
</CopyText>
);
Expand Down

0 comments on commit 382d8f2

Please sign in to comment.