Skip to content

Commit

Permalink
feat(nui): Query time table
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Jan 1, 2022
2 parents cebd9c5 + 4d93b92 commit ed03361
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
"homepage": "ui/build",
"private": true,
"dependencies": {
"@chakra-ui/icons": "^1.1.1",
"@chakra-ui/react": "^1.7.3",
"@emotion/react": "^11",
"@emotion/styled": "^11",
"@types/node": "^16.11.9",
"@types/react": "^17.0.35",
"@types/react-dom": "^17.0.11",
"@types/react-table": "^7.7.9",
"framer-motion": "4.1.17",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "6",
"react-scripts": "4.0.3",
"react-table": "^7.7.0",
"typescript": "^4.5.2"
},
"scripts": {
Expand Down
108 changes: 107 additions & 1 deletion ui/src/components/Resource.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,115 @@
import { useParams } from 'react-router-dom';
import { useEffect, useState, useMemo } from 'react';
import { fetchNui } from '../utils/fetchNui';
import { useNuiEvent } from '../hooks/useNuiEvent';
import { debugData } from '../utils/debugData';
import { Table, Thead, Tbody, Tr, Th, Td, chakra } from '@chakra-ui/react';
import { TriangleDownIcon, TriangleUpIcon } from '@chakra-ui/icons';
import { useTable, useSortBy, Column } from 'react-table';

interface QueryData {
date: number;
query: string;
executionTime: number;
}

interface TableData {
query: string;
executionTime: number;
}

const Resource: React.FC = () => {
let { resource } = useParams();
const [resourceData, setResourceData] = useState<QueryData[]>([
{
date: 0,
query: '',
executionTime: 0,
},
]);

useEffect(() => {
fetchNui('fetchResource', resource);
debugData([
{
action: 'loadResource',
data: [
{ query: 'SELECT * FROM `users`', executionTime: 5 },
{ query: 'SELECT * FROM `owned_vehicles`', executionTime: 2 },
{ query: 'SELECT * FROM `properties`', executionTime: 7 },
{ query: 'SELECT * FROM `phone_messages`', executionTime: 13 },
],
},
]);
}, [resource]);

useNuiEvent<QueryData[]>('loadResource', (data) => {
setResourceData(data);
});

const data = useMemo<TableData[]>(() => resourceData, [resourceData]);

const columns = useMemo<Column<TableData>[]>(
() => [
{
Header: 'Query',
accessor: 'query',
},
{
Header: 'Execution time',
accessor: 'executionTime',
},
],
[]
);

const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ columns, data }, useSortBy);

return <h2>{resource}</h2>;
// Todo: pagination
return (
<Table {...getTableProps} size="sm">
<Thead>
{headerGroups.map((header): any => (
<Tr {...header.getHeaderGroupProps()}>
{header.headers.map((column) => (
<Th {...column.getHeaderProps(column.getSortByToggleProps())} color="white" fontFamily="Poppins">
{column.render('Header')}
<chakra.span pl="4">
{column.isSorted ? (
column.isSortedDesc ? (
<TriangleDownIcon aria-label="sorted descending" />
) : (
<TriangleUpIcon aria-label="sorted ascending" />
)
) : null}
</chakra.span>
</Th>
))}
</Tr>
))}
</Thead>
<Tbody {...getTableBodyProps()}>
{rows.map((row) => {
prepareRow(row);
return (
<Tr {...row.getRowProps()}>
{row.cells.map((cell) => (
<Td
{...cell.getCellProps()}
fontFamily="Poppins"
wordBreak="break-word"
textOverflow="ellipsis"
overflow="hidden"
>
{cell.render('Cell')}
</Td>
))}
</Tr>
);
})}
</Tbody>
</Table>
);
};

export default Resource;
122 changes: 122 additions & 0 deletions ui/src/types/react-table-config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
UseColumnOrderInstanceProps,
UseColumnOrderState,
UseExpandedHooks,
UseExpandedInstanceProps,
UseExpandedOptions,
UseExpandedRowProps,
UseExpandedState,
UseFiltersColumnOptions,
UseFiltersColumnProps,
UseFiltersInstanceProps,
UseFiltersOptions,
UseFiltersState,
UseGlobalFiltersColumnOptions,
UseGlobalFiltersInstanceProps,
UseGlobalFiltersOptions,
UseGlobalFiltersState,
UseGroupByCellProps,
UseGroupByColumnOptions,
UseGroupByColumnProps,
UseGroupByHooks,
UseGroupByInstanceProps,
UseGroupByOptions,
UseGroupByRowProps,
UseGroupByState,
UsePaginationInstanceProps,
UsePaginationOptions,
UsePaginationState,
UseResizeColumnsColumnOptions,
UseResizeColumnsColumnProps,
UseResizeColumnsOptions,
UseResizeColumnsState,
UseRowSelectHooks,
UseRowSelectInstanceProps,
UseRowSelectOptions,
UseRowSelectRowProps,
UseRowSelectState,
UseRowStateCellProps,
UseRowStateInstanceProps,
UseRowStateOptions,
UseRowStateRowProps,
UseRowStateState,
UseSortByColumnOptions,
UseSortByColumnProps,
UseSortByHooks,
UseSortByInstanceProps,
UseSortByOptions,
UseSortByState
} from 'react-table'

declare module 'react-table' {
// take this file as-is, or comment out the sections that don't apply to your plugin configuration

export interface TableOptions<D extends Record<string, unknown>>
extends UseExpandedOptions<D>,
UseFiltersOptions<D>,
UseGlobalFiltersOptions<D>,
UseGroupByOptions<D>,
UsePaginationOptions<D>,
UseResizeColumnsOptions<D>,
UseRowSelectOptions<D>,
UseRowStateOptions<D>,
UseSortByOptions<D>,
// note that having Record here allows you to add anything to the options, this matches the spirit of the
// underlying js library, but might be cleaner if it's replaced by a more specific type that matches your
// feature set, this is a safe default.
Record<string, any> {}

export interface Hooks<D extends Record<string, unknown> = Record<string, unknown>>
extends UseExpandedHooks<D>,
UseGroupByHooks<D>,
UseRowSelectHooks<D>,
UseSortByHooks<D> {}

export interface TableInstance<D extends Record<string, unknown> = Record<string, unknown>>
extends UseColumnOrderInstanceProps<D>,
UseExpandedInstanceProps<D>,
UseFiltersInstanceProps<D>,
UseGlobalFiltersInstanceProps<D>,
UseGroupByInstanceProps<D>,
UsePaginationInstanceProps<D>,
UseRowSelectInstanceProps<D>,
UseRowStateInstanceProps<D>,
UseSortByInstanceProps<D> {}

export interface TableState<D extends Record<string, unknown> = Record<string, unknown>>
extends UseColumnOrderState<D>,
UseExpandedState<D>,
UseFiltersState<D>,
UseGlobalFiltersState<D>,
UseGroupByState<D>,
UsePaginationState<D>,
UseResizeColumnsState<D>,
UseRowSelectState<D>,
UseRowStateState<D>,
UseSortByState<D> {}

export interface ColumnInterface<D extends Record<string, unknown> = Record<string, unknown>>
extends UseFiltersColumnOptions<D>,
UseGlobalFiltersColumnOptions<D>,
UseGroupByColumnOptions<D>,
UseResizeColumnsColumnOptions<D>,
UseSortByColumnOptions<D> {}

export interface ColumnInstance<D extends Record<string, unknown> = Record<string, unknown>>
extends UseFiltersColumnProps<D>,
UseGroupByColumnProps<D>,
UseResizeColumnsColumnProps<D>,
UseSortByColumnProps<D> {
getSortByToggleProps: (props?: Partial<TableSortByToggleProps> | undefined) => TableSortByToggleProps
}

export interface Cell<D extends Record<string, unknown> = Record<string, unknown>, V = any>
extends UseGroupByCellProps<D>,
UseRowStateCellProps<D> {}

export interface Row<D extends Record<string, unknown> = Record<string, unknown>>
extends UseExpandedRowProps<D>,
UseGroupByRowProps<D>,
UseRowSelectRowProps<D>,
UseRowStateRowProps<D> {}
}

0 comments on commit ed03361

Please sign in to comment.