Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove runtime error caused by theme issue #462

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions crowdsec-docs/src/components/tableRender.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useMemo } from 'react';
import { MaterialReactTable } from 'material-react-table';
import { createTheme, ThemeProvider } from '@mui/material';
import BrowserOnly from '@docusaurus/BrowserOnly';
import {useColorMode} from '@docusaurus/theme-common';


const getTheme = function () {
let currentTheme = localStorage.getItem('theme')
const isLight = currentTheme === 'light'
const tableTheme =
createTheme({
palette: {
mode: currentTheme,
background: {
default:
isLight
? 'rgb(255,255,255)'
: '#1b1b1d',
},
const TableRender = ({ columns, url }) => {
const [jsonContent, setJsonContent] = useState()
const {colorMode} = useColorMode();

const theme = useMemo(() => {
const isLight = colorMode === 'light';

return createTheme({
palette: {
mode: colorMode,
background: {
default:
isLight
? 'rgb(255,255,255)'
: '#1b1b1d',
},
},
},
)
return tableTheme
}
);
}, [colorMode]);


const TableRender = ({ columns, url }) => {
const [jsonContent, setJsonContent] = useState()

useEffect(() => {
useEffect(() => {
fetch(url)
.then((res) => res.json())
.then((data) => {
var newData = [];
var names = [];
const newData = [];
const names = [];

Object.keys(data).map((key, i) => {
// filter duplicate names
var name = data[key]["name"];
const name = data[key]["name"];

if (names.includes(name)) {
return
}

names.push(name)
newData.push(data[key]);
})
setJsonContent(newData)
})
})
// execute this fetch only once (on mount)
}, []);

if (!columns || !jsonContent) {
return <></>
Expand All @@ -53,7 +57,7 @@ const TableRender = ({ columns, url }) => {
return (
<BrowserOnly>
{() =>
<ThemeProvider theme={getTheme()}>
<ThemeProvider theme={theme}>
<MaterialReactTable
data={jsonContent}
columns={columns}
Expand Down