Skip to content

Commit

Permalink
add prevFieldList
Browse files Browse the repository at this point in the history
  • Loading branch information
zjy365 committed Jan 22, 2025
1 parent 661e5ca commit 22104c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export const Filter = ({
</Text>
<Switch
isChecked={isJsonMode}
onChange={() => formHook.setValue('isJsonMode', !isJsonMode)}
onChange={() => {
formHook.setValue('isJsonMode', !isJsonMode);
formHook.setValue('jsonFilters', []);
}}
/>
</Flex>
<Flex alignItems={'center'} gap={'12px'}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const LogTable = ({
const { exportLogs } = useLogStore();

const generateFieldList = useCallback(
(data: any[]) => {
(data: any[], prevFieldList: FieldItem[] = []) => {
if (!data.length) return [];

if (!isJsonMode) {
Expand All @@ -77,7 +77,6 @@ export const LogTable = ({
}

const uniqueKeys = new Set<string>();

data.forEach((item) => {
Object.keys(item).forEach((key) => {
if (key !== '_msg') {
Expand All @@ -86,21 +85,25 @@ export const LogTable = ({
});
});

const prevFieldStates = prevFieldList.reduce((acc, field) => {
acc[field.value] = field.checked;
return acc;
}, {} as Record<string, boolean>);

return Array.from(uniqueKeys).map((key) => ({
value: key,
label: key,
checked: true,
checked: key in prevFieldStates ? prevFieldStates[key] : true,
accessorKey: key
}));
},
[isJsonMode]
);

const [fieldList, setFieldList] = useState<FieldItem[]>(() => generateFieldList(data));
console.log(fieldList, 'fieldList');
const [fieldList, setFieldList] = useState<FieldItem[]>(() => generateFieldList(data, []));

useEffect(() => {
setFieldList(generateFieldList(data));
setFieldList((prevFieldList) => generateFieldList(data, prevFieldList));
formHook.setValue(
'filterKeys',
generateFieldList(data).map((field) => ({ value: field.value, label: field.label }))
Expand Down

0 comments on commit 22104c8

Please sign in to comment.