From dd87babc9ccd336ba9cac980c254da0d7e835bc7 Mon Sep 17 00:00:00 2001 From: Ben10Omintrix <138636438+Ben10Omintrix@users.noreply.github.com> Date: Sat, 13 Jul 2024 01:19:27 +0300 Subject: [PATCH] request manager refactor (#84584) --- code/modules/requests/request_manager.dm | 10 +- ...{RequestManager.jsx => RequestManager.tsx} | 181 ++++++++++-------- 2 files changed, 105 insertions(+), 86 deletions(-) rename tgui/packages/tgui/interfaces/{RequestManager.jsx => RequestManager.tsx} (55%) diff --git a/code/modules/requests/request_manager.dm b/code/modules/requests/request_manager.dm index 74a40304f7aed..99a9bba1cc84f 100644 --- a/code/modules/requests/request_manager.dm +++ b/code/modules/requests/request_manager.dm @@ -243,12 +243,10 @@ GLOBAL_DATUM_INIT(requests, /datum/request_manager, new) return TRUE /datum/request_manager/ui_data(mob/user) - . = list( - "requests" = list() - ) + var/list/data = list() for (var/ckey in requests) for (var/datum/request/request as anything in requests[ckey]) - var/list/data = list( + data["requests"] += list(list( "id" = request.id, "req_type" = request.req_type, "owner" = request.owner ? "[REF(request.owner)]" : null, @@ -258,8 +256,8 @@ GLOBAL_DATUM_INIT(requests, /datum/request_manager, new) "additional_info" = request.additional_information, "timestamp" = request.timestamp, "timestamp_str" = gameTimestamp(wtime = request.timestamp) - ) - .["requests"] += list(data) + )) + return data #undef REQUEST_PRAYER #undef REQUEST_CENTCOM diff --git a/tgui/packages/tgui/interfaces/RequestManager.jsx b/tgui/packages/tgui/interfaces/RequestManager.tsx similarity index 55% rename from tgui/packages/tgui/interfaces/RequestManager.jsx rename to tgui/packages/tgui/interfaces/RequestManager.tsx index b12d8285e80f1..9a009106788b7 100644 --- a/tgui/packages/tgui/interfaces/RequestManager.jsx +++ b/tgui/packages/tgui/interfaces/RequestManager.tsx @@ -3,37 +3,65 @@ * @copyright 2021 bobbahbrown (https://github.com/bobbahbrown) * @license MIT */ -import { decodeHtmlEntities } from 'common/string'; +import { createSearch, decodeHtmlEntities } from 'common/string'; import { useState } from 'react'; -import { useBackend, useLocalState } from '../backend'; -import { Button, Input, Popper, Section, Table } from '../components'; +import { useBackend } from '../backend'; +import { Button, Input, Popper, Section, Stack, Table } from '../components'; import { Window } from '../layouts'; +type Data = { + requests: Request[]; +}; + +type Request = { + id: string; + req_type: string; + owner: string; + owner_ckey: string; + owner_name: string; + message: string; + additional_info: string; + timestamp: number; + timestamp_str: string; +}; + +const displayTypeMap = { + request_prayer: 'PRAYER', + request_centcom: 'CENTCOM', + request_syndicate: 'SYNDICATE', + request_nuke: 'NUKE CODE', + request_fax: 'FAX', + request_internet_sound: 'INTERNET SOUND', +}; + export const RequestManager = (props) => { - const { act, data } = useBackend(); - const { requests } = data; - const [filteredTypes, _] = useLocalState( - 'filteredTypes', + const { act, data } = useBackend(); + const { requests = [] } = data; + const [filteredTypes, setFilteredTypes] = useState( Object.fromEntries( Object.entries(displayTypeMap).map(([type, _]) => [type, true]), ), ); const [searchText, setSearchText] = useState(''); + const updateFilter = (type) => { + const newFilter = { ...filteredTypes }; + newFilter[type] = !newFilter[type]; + setFilteredTypes(newFilter); + }; + // Handle filtering let displayedRequests = requests.filter( (request) => filteredTypes[request.req_type], ); - if (searchText) { - const filterText = searchText.toLowerCase(); - displayedRequests = displayedRequests.filter( - (request) => - decodeHtmlEntities(request.message) - .toLowerCase() - .includes(filterText) || - request.owner_name.toLowerCase().includes(filterText), - ); + const search = createSearch( + searchText, + (requests: Request) => + requests.owner_name + decodeHtmlEntities(requests.message), + ); + if (searchText.length > 0) { + displayedRequests = displayedRequests.filter((request) => search(request)); } return ( @@ -42,15 +70,22 @@ export const RequestManager = (props) => {
- setSearchText(value)} - placeholder={'Search...'} - mr={1} - /> - - + + + setSearchText(value)} + placeholder={'Search...'} + mr={1} + /> + + + + + } > {displayedRequests.map((request) => ( @@ -79,15 +114,6 @@ export const RequestManager = (props) => { ); }; -const displayTypeMap = { - request_prayer: 'PRAYER', - request_centcom: 'CENTCOM', - request_syndicate: 'SYNDICATE', - request_nuke: 'NUKE CODE', - request_fax: 'FAX', - request_internet_sound: 'INTERNET SOUND', -}; - const RequestType = (props) => { const { requestType } = props; @@ -99,7 +125,7 @@ const RequestType = (props) => { }; const RequestControls = (props) => { - const { act, _ } = useBackend(); + const { act } = useBackend(); const { request } = props; return ( @@ -131,52 +157,47 @@ const RequestControls = (props) => { const FilterPanel = (props) => { const [filterVisible, setFilterVisible] = useState(false); - const [filteredTypes, setFilteredTypes] = useLocalState( - 'filteredTypes', - Object.fromEntries( - Object.entries(displayTypeMap).map(([type, _]) => [type, true]), - ), - ); + const { typesList, updateFilter } = props; return ( - - - {Object.keys(displayTypeMap).map((type) => { - return ( - - - - - - { - filteredTypes[type] = !filteredTypes[type]; - setFilteredTypes(filteredTypes); - }} - my={0.25} - /> - - - ); - })} -
- - } - > -
- -
-
+
+ {' '} + + + + {Object.keys(displayTypeMap).map((type) => { + return ( + + + + + + { + updateFilter(type); + }} + my={0.25} + /> + + + ); + })} +
+
+ } + /> + ); };