-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {} | ||
} |