From fbd6c578587e210a7d57c3fd02442b3bb39929f5 Mon Sep 17 00:00:00 2001 From: Praveen K B Date: Tue, 17 Dec 2024 15:10:47 +0530 Subject: [PATCH] feat: Add Autocomplete for Value Selection in RuleView Component --- .../components/Querier/FilterQueryBuilder.tsx | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/pages/Stream/components/Querier/FilterQueryBuilder.tsx b/src/pages/Stream/components/Querier/FilterQueryBuilder.tsx index 69802ab0..009c5ccc 100644 --- a/src/pages/Stream/components/Querier/FilterQueryBuilder.tsx +++ b/src/pages/Stream/components/Querier/FilterQueryBuilder.tsx @@ -6,10 +6,10 @@ import { Box, ThemeIcon, Select, - Input, CloseIcon, Pill, ActionIcon, + Autocomplete, } from '@mantine/core'; import { IconFilter, IconPlus } from '@tabler/icons-react'; import classes from '../../styles/Querier.module.css'; @@ -23,7 +23,7 @@ export const FilterPlaceholder = () => { ); }; -import { useCallback, useEffect } from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; import { useFilterStore, filterStoreReducers, operatorLabelMap } from '../../providers/FilterProvider'; import { useLogsStore } from '../../providers/LogsProvider'; import { @@ -60,6 +60,22 @@ type RuleViewType = { const RuleView = (props: RuleViewType) => { const { rule, type, groupId } = props; const [fieldNames, setFilterStore] = useFilterStore((store) => store.fieldNames); + const [pageData] = useLogsStore((store) => store.tableOpts.pageData); + const [columnValues, setColumnValues] = useState([]); + + const getUniqueColValues = useMemo(() => { + if (!rule.field) return []; + return Array.from( + new Set(pageData.filter((item) => item[rule.field] !== null).map((item) => String(item[rule.field]))), + ); + }, [pageData, rule.field]); + + useEffect(() => { + if (rule.field) { + setColumnValues(getUniqueColValues); + } + }, [rule.field, getUniqueColValues]); + const onFieldChange = useCallback((field: string | null) => { if (field === null) return; setFilterStore((store) => updateRule(store, groupId, rule.id, { field })); @@ -70,8 +86,8 @@ const RuleView = (props: RuleViewType) => { setFilterStore((store) => updateRule(store, groupId, rule.id, { operator })); }, []); - const onValueChange = useCallback((e: React.ChangeEvent) => { - setFilterStore((store) => updateRule(store, groupId, rule.id, { value: e.target.value })); + const onValueChange = useCallback((value: string) => { + setFilterStore((store) => updateRule(store, groupId, rule.id, { value })); }, []); const deleteBtnHandler = useCallback(() => { @@ -91,14 +107,15 @@ const RuleView = (props: RuleViewType) => { onChange={onOperatorChange} w="33%" /> -