Skip to content

Commit

Permalink
feat: Hide unnecessary NavItem #1841 (#2037)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

feat: Hide unnecessary NavItem #1841

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Oct 14, 2024
1 parent e140330 commit 04dd952
Show file tree
Hide file tree
Showing 10 changed files with 2,182 additions and 150 deletions.
3 changes: 3 additions & 0 deletions gui/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["next/core-web-vitals", "next/typescript", "prettier"]
}
10 changes: 10 additions & 0 deletions gui/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');

const buildEslintCommand = (filenames) =>
`next lint --fix --file ${filenames
.map((f) => path.relative(process.cwd(), f))
.join(' --file ')}`;

module.exports = {
'*.{js,jsx,ts,tsx}': [buildEslintCommand]
};
45 changes: 30 additions & 15 deletions gui/app/(dashboard)/database/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { ITableColumns, ITableIndex } from '@/lib/databse-interface';
import {
ITableColumns,
ITableIndex,
ITableSegment
} from '@/lib/databse-interface';
import { useRouter } from 'next/navigation';
import { useCallback, useEffect, useRef, useState } from 'react';
import { INode } from 'react-accessible-treeview';
import { INode, ITreeViewOnLoadDataProps } from 'react-accessible-treeview';
import {
listDatabase,
listTable,
showTableColumns,
showTableIndexes,
showTableSegments
} from '../actions';
import { initialData } from './constants';
import { DatabaseRouteParams, TreeParentId } from './interface';
import { buildLeafData, getParentIdById, updateTreeData } from './utils';

Expand Down Expand Up @@ -43,19 +46,26 @@ export const useHandleClickTreeName = () => {
};

export const useBuildTreeData = () => {
const loadedAlertElement = useRef(null);
const [data, setData] = useState<INode[]>(initialData);
const [nodesAlreadyLoaded, setNodesAlreadyLoaded] = useState<any[]>([]);
const loadedAlertElement = useRef<HTMLDivElement>(null);
const [data, setData] = useState<INode[]>([]);
const [nodesAlreadyLoaded, setNodesAlreadyLoaded] = useState<INode[]>([]);
const [loading, setLoading] = useState<boolean>(true);

const fetchDatabases = useCallback(async () => {
try {
setLoading(true);
const ret = await listDatabase();
if (ret.databases.length > 0) {
setData((value) =>
setData(
updateTreeData(
value,
[
{
name: '',
id: 0,
children: [],
parent: null
}
],
0,
ret.databases.map((x: string) => ({
name: x,
Expand All @@ -68,7 +78,8 @@ export const useBuildTreeData = () => {
);
}
setLoading(false);
} catch (error) {
} catch (err: unknown) {
console.log('🚀 ~ fetchDatabases ~ err:', err);
setLoading(false);
}
}, []);
Expand Down Expand Up @@ -99,7 +110,7 @@ export const useBuildTreeData = () => {
} else {
setData((value) =>
value.map((x) => {
let metadata = x.metadata ?? {};
const metadata = x.metadata ?? {};
if (x.id === databaseName) {
metadata['isEmpty'] = true;
}
Expand Down Expand Up @@ -145,7 +156,7 @@ export const useBuildTreeData = () => {
// });
};

const wrappedOnLoadData = async (props: any) => {
const wrappedOnLoadData = async (props: ITreeViewOnLoadDataProps) => {
const nodeHasNoChildData = props.element.children.length === 0;
const nodeHasAlreadyBeenLoaded = nodesAlreadyLoaded.find(
(e) => e.id === props.element.id
Expand All @@ -154,13 +165,17 @@ export const useBuildTreeData = () => {
await onLoadData(props);

if (nodeHasNoChildData && !nodeHasAlreadyBeenLoaded) {
const el: any = loadedAlertElement.current;
const el = loadedAlertElement.current;
setNodesAlreadyLoaded([...nodesAlreadyLoaded, props.element]);
el && (el.innerHTML = `${props.element.name} loaded`);
if (el) {
el.innerHTML = `${props.element.name} loaded`;
}

// Clearing aria-live region so loaded node alerts no longer appear in DOM
setTimeout(() => {
el && (el.innerHTML = '');
if (el) {
el.innerHTML = '';
}
}, 5000);
}
};
Expand Down Expand Up @@ -216,7 +231,7 @@ export const useFetchTableSegments = ({
databaseId,
tableId
}: DatabaseRouteParams['params']) => {
const [tableSegments, setTableSegments] = useState<any[]>([]);
const [tableSegments, setTableSegments] = useState<ITableSegment[]>([]);

const fetchTableSegments = useCallback(async () => {
const data = await showTableSegments({
Expand Down
4 changes: 0 additions & 4 deletions gui/app/(dashboard)/database/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ import Loading from './loading';
import AsyncTree from './tree';

export default async function DatabaseLayout({
searchParams,
children
}: {
searchParams: { q: string; offset: string };
children: React.ReactNode;
}) {
const search = searchParams?.q ?? '';
const offset = searchParams?.offset ?? 0;

return (
<div className="flex divide-x h-full">
<section className="w-1/4">
Expand Down
14 changes: 10 additions & 4 deletions gui/app/(dashboard)/database/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ function AsyncTree() {
element,
isBranch,
isExpanded,
isSelected,
isHalfSelected,
// isSelected,
// isHalfSelected,
// handleSelect,
getNodeProps,
level,
handleSelect,
handleExpand
}) => {
const branchNode = (isExpanded: boolean, element: INode) => {
Expand Down Expand Up @@ -100,7 +100,13 @@ function AsyncTree() {
);
}

const ArrowIcon = ({ isOpen, className }: any) => {
const ArrowIcon = ({
isOpen,
className
}: {
isOpen: boolean;
className?: string;
}) => {
const baseClass = 'arrow';
const classes = cx(
baseClass,
Expand Down
2 changes: 1 addition & 1 deletion gui/app/(dashboard)/database/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Leaf } from './constants';
import { TreeParentId } from './interface';

export const updateTreeData = (
list: any[],
list: INode[],
id: string | number,
children: Array<INode>
) => {
Expand Down
Loading

0 comments on commit 04dd952

Please sign in to comment.