|
| 1 | +import { ChangeEvent, useEffect, useState } from 'react'; |
| 2 | +import { RuleViewModel } from '@/lib/infrastructure/data/view-model/rule'; |
| 3 | +import useChunkedStream, { StreamingStatus } from '@/lib/infrastructure/hooks/useChunkedStream'; |
| 4 | +import { GridApi, GridReadyEvent } from 'ag-grid-community'; |
| 5 | +import { useToast } from '@/lib/infrastructure/hooks/useToast'; |
| 6 | +import { BaseViewModelValidator } from '@/component-library/features/utils/BaseViewModelValidator'; |
| 7 | +import { alreadyStreamingToast, noApiToast } from '@/component-library/features/utils/list-toasts'; |
| 8 | +import { Heading } from '@/component-library/atoms/misc/Heading'; |
| 9 | +import { Input } from '@/component-library/atoms/form/input'; |
| 10 | +import { SearchButton } from '@/component-library/features/search/SearchButton'; |
| 11 | +import { ListRuleTable } from '@/component-library/pages/Rule/list/ListRuleTable'; |
| 12 | + |
| 13 | +type ListRuleProps = { |
| 14 | + initialData?: RuleViewModel[]; |
| 15 | +}; |
| 16 | + |
| 17 | +const DEFAULT_SCOPE = '*'; |
| 18 | + |
| 19 | +export const ListRule = (props: ListRuleProps) => { |
| 20 | + const streamingHook = useChunkedStream<RuleViewModel>(); |
| 21 | + const [scope, setScope] = useState<string>(DEFAULT_SCOPE); |
| 22 | + const [gridApi, setGridApi] = useState<GridApi<RuleViewModel> | null>(null); |
| 23 | + |
| 24 | + const { toast, dismiss } = useToast(); |
| 25 | + const validator = new BaseViewModelValidator(toast); |
| 26 | + |
| 27 | + const onGridReady = (event: GridReadyEvent) => { |
| 28 | + setGridApi(event.api); |
| 29 | + }; |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (props.initialData) { |
| 33 | + onData(props.initialData); |
| 34 | + } |
| 35 | + }, [gridApi]); |
| 36 | + |
| 37 | + const onData = (data: RuleViewModel[]) => { |
| 38 | + const validData = data.filter(element => validator.isValid(element)); |
| 39 | + gridApi?.applyTransactionAsync({ add: validData }); |
| 40 | + }; |
| 41 | + |
| 42 | + const startStreaming = () => { |
| 43 | + if (gridApi) { |
| 44 | + dismiss(); |
| 45 | + gridApi.flushAsyncTransactions(); |
| 46 | + gridApi.setGridOption('rowData', []); |
| 47 | + validator.reset(); |
| 48 | + |
| 49 | + const url = `/api/feature/list-rules?scope=${scope}`; |
| 50 | + streamingHook.start({ url, onData }); |
| 51 | + } else { |
| 52 | + toast(noApiToast); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const isRunning = streamingHook.status === StreamingStatus.RUNNING; |
| 57 | + |
| 58 | + const onInputChange = (event: ChangeEvent<HTMLInputElement>) => { |
| 59 | + const value = event.target.value; |
| 60 | + setScope(value !== '' ? value : DEFAULT_SCOPE); |
| 61 | + }; |
| 62 | + |
| 63 | + const onSearch = (event: any) => { |
| 64 | + event.preventDefault(); |
| 65 | + if (!isRunning) { |
| 66 | + startStreaming(); |
| 67 | + } else { |
| 68 | + toast(alreadyStreamingToast); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + const onStop = (event: any) => { |
| 73 | + event.preventDefault(); |
| 74 | + if (isRunning) { |
| 75 | + streamingHook.stop(); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className="flex flex-col space-y-3 w-full grow"> |
| 81 | + <Heading text="Rules" /> |
| 82 | + <div className="space-y-2"> |
| 83 | + <div className="text-neutral-900 dark:text-neutral-100">Scope</div> |
| 84 | + <div className="flex flex-col space-y-2 sm:flex-row sm:space-y-0 sm:space-x-2 items-center sm:items-start"> |
| 85 | + <Input className="w-full sm:flex-grow" onChange={onInputChange} onEnterKey={onSearch} placeholder={DEFAULT_SCOPE} /> |
| 86 | + <SearchButton isRunning={isRunning} onStop={onStop} onSearch={onSearch} /> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + <ListRuleTable streamingHook={streamingHook} onGridReady={onGridReady} /> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +}; |
0 commit comments