diff --git a/ui/package.json b/ui/package.json index 33224a1..45af84d 100644 --- a/ui/package.json +++ b/ui/package.json @@ -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": { diff --git a/ui/src/components/Resource.tsx b/ui/src/components/Resource.tsx index e4478f7..9fd78b6 100644 --- a/ui/src/components/Resource.tsx +++ b/ui/src/components/Resource.tsx @@ -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([ + { + date: 0, + query: '', + executionTime: 0, + }, + ]); - return

{resource}

; + 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('loadResource', (data) => { + setResourceData(data); + }); + + const data = useMemo(() => resourceData, [resourceData]); + + const columns = useMemo[]>( + () => [ + { + Header: 'Query', + accessor: 'query', + }, + { + Header: 'Execution time', + accessor: 'executionTime', + }, + ], + [] + ); + + const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ columns, data }, useSortBy); + + // Todo: pagination + return ( + + + {headerGroups.map((header): any => ( + + {header.headers.map((column) => ( + + ))} + + ))} + + + {rows.map((row) => { + prepareRow(row); + return ( + + {row.cells.map((cell) => ( + + ))} + + ); + })} + +
+ {column.render('Header')} + + {column.isSorted ? ( + column.isSortedDesc ? ( + + ) : ( + + ) + ) : null} + +
+ {cell.render('Cell')} +
+ ); }; export default Resource; diff --git a/ui/src/types/react-table-config.d.ts b/ui/src/types/react-table-config.d.ts new file mode 100644 index 0000000..00f9416 --- /dev/null +++ b/ui/src/types/react-table-config.d.ts @@ -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> + extends UseExpandedOptions, + UseFiltersOptions, + UseGlobalFiltersOptions, + UseGroupByOptions, + UsePaginationOptions, + UseResizeColumnsOptions, + UseRowSelectOptions, + UseRowStateOptions, + UseSortByOptions, + // 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 {} + + export interface Hooks = Record> + extends UseExpandedHooks, + UseGroupByHooks, + UseRowSelectHooks, + UseSortByHooks {} + + export interface TableInstance = Record> + extends UseColumnOrderInstanceProps, + UseExpandedInstanceProps, + UseFiltersInstanceProps, + UseGlobalFiltersInstanceProps, + UseGroupByInstanceProps, + UsePaginationInstanceProps, + UseRowSelectInstanceProps, + UseRowStateInstanceProps, + UseSortByInstanceProps {} + + export interface TableState = Record> + extends UseColumnOrderState, + UseExpandedState, + UseFiltersState, + UseGlobalFiltersState, + UseGroupByState, + UsePaginationState, + UseResizeColumnsState, + UseRowSelectState, + UseRowStateState, + UseSortByState {} + + export interface ColumnInterface = Record> + extends UseFiltersColumnOptions, + UseGlobalFiltersColumnOptions, + UseGroupByColumnOptions, + UseResizeColumnsColumnOptions, + UseSortByColumnOptions {} + + export interface ColumnInstance = Record> + extends UseFiltersColumnProps, + UseGroupByColumnProps, + UseResizeColumnsColumnProps, + UseSortByColumnProps { + getSortByToggleProps: (props?: Partial | undefined) => TableSortByToggleProps + } + + export interface Cell = Record, V = any> + extends UseGroupByCellProps, + UseRowStateCellProps {} + + export interface Row = Record> + extends UseExpandedRowProps, + UseGroupByRowProps, + UseRowSelectRowProps, + UseRowStateRowProps {} +} \ No newline at end of file