From 3554ba2fc8e18c3bd902c70c33742c125e809c2c Mon Sep 17 00:00:00 2001 From: dunak-debug <19434539+dunak-debug@users.noreply.github.com> Date: Sat, 8 Jan 2022 14:15:17 +0100 Subject: [PATCH] feat(ui): Implement sorting function (WIP) --- src/logger/index.ts | 40 ++++++++++++++++++++++++++-------- ui/src/components/Resource.tsx | 38 +++++++++++--------------------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/logger/index.ts b/src/logger/index.ts index 4f30657..55a74dc 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -48,15 +48,37 @@ RegisterCommand( true ); -onNet(`oxmysql:fetchResource`, (data: {resource: string, pageIndex: number}) => { - if (typeof data.resource !== 'string') return; +const sortQueries = (queries: QueryData[], sort: { id: 'query' | 'executionTime'; desc: boolean }) => { + const sortedQueries = queries.sort((a, b) => { + switch (sort.id) { + case 'query': + return a.query > b.query ? 1 : -1; + case 'executionTime': + return a.executionTime - b.executionTime; + default: + return 0; + } + }); + + return sort.desc ? sortedQueries.reverse() : sortedQueries; +}; - const startRow = data.pageIndex * 12 - const endRow = startRow + 12 - const queries = logStorage[data.resource].slice(startRow, endRow) - const pageCount = Math.ceil(logStorage[data.resource].length / 12) +onNet( + `oxmysql:fetchResource`, + (data: { resource: string; pageIndex: number; sortBy?: { id: 'query' | 'executionTime'; desc: boolean }[] }) => { + if (typeof data.resource !== 'string') return; - if (!queries) return; + const sort = data.sortBy ? data.sortBy[0] : false; - emitNet(`oxmysql:loadResource`, source, {queries, pageCount}); -}); + const startRow = data.pageIndex * 12; + const endRow = startRow + 12; + const queries = sort + ? sortQueries(logStorage[data.resource], sort).slice(startRow, endRow) + : logStorage[data.resource].slice(startRow, endRow); + const pageCount = Math.ceil(logStorage[data.resource].length / 12); + + if (!queries) return; + + emitNet(`oxmysql:loadResource`, source, { queries, pageCount }); + } +); diff --git a/ui/src/components/Resource.tsx b/ui/src/components/Resource.tsx index 90336de..182bfaf 100644 --- a/ui/src/components/Resource.tsx +++ b/ui/src/components/Resource.tsx @@ -30,7 +30,8 @@ interface NuiData { } const Resource: React.FC = () => { - let { resource } = useParams(); + const { resource } = useParams(); + const [isLoaded, setIsLoaded] = useState(false); const [resourceData, setResourceData] = useState([ { @@ -41,12 +42,6 @@ const Resource: React.FC = () => { ]); const [totalPages, setTotalPages] = useState(1); - useNuiEvent('loadResource', (data) => { - setResourceData(data.queries); - setTotalPages(data.pageCount); - setIsLoaded(true); - }); - const data = useMemo(() => resourceData, [resourceData]); const columns = useMemo[]>( @@ -75,7 +70,7 @@ const Resource: React.FC = () => { gotoPage, nextPage, previousPage, - state: { pageIndex }, + state: { pageIndex, sortBy }, prepareRow, } = useTable( { @@ -83,6 +78,7 @@ const Resource: React.FC = () => { data, initialState: { pageSize: 12 }, manualPagination: true, + manualSortBy: true, pageCount: totalPages, autoResetPage: false, }, @@ -91,27 +87,19 @@ const Resource: React.FC = () => { ); useEffect(() => { - fetchNui('fetchResource', { resource, pageIndex }); + fetchNui('fetchResource', { resource, pageIndex, sortBy }); setIsLoaded(false); - // debugData([ - // { - // action: 'loadResource', - // data: { - // pageCount: 1, - // queries: [ - // { 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, pageIndex]); + }, [resource, pageIndex, sortBy]); + + useNuiEvent('loadResource', (data) => { + setResourceData(data.queries); + setTotalPages(data.pageCount); + setIsLoaded(true); + }); return ( <> - +
{headerGroups.map((header): any => (