mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-27 11:02:16 +01:00
Splits the insights page into separate sections with their own separate filters. Each filter is sticky to the top of the page, similar to how the previous filters worked. In doing this, I have also moved around a lot of code. Refer to the inline comments for more specific examples, but on a high level, this PR: - Moves the flag check from InsightsCharts into Insights itself. Because the old Insights had filters and state, and the new one does not, it made sense to fork off higher up in the tree. Because the new version doesn't have state, I have also simplified it and removed an intermediate component (InsightsCharts) that doesn't feel necessary anymore. - Because InsightsCharts isn't used anymore, I've moved the LegacyInsightsCharts file back into InsightsCharts. However, I'm happy to move it back if we think that's useful. - Instead of all charts living in InsightsCharts, I've split each section into its own file in the new sections directory. Because the sections have separate filters, they don't share any state anymore, so there's nothing they share. - I'm reusing the existing hook and endpoint. As it stands, the performance insights use **most** of the data from that payload (but not all of it). The user insights use some of it. Flag lifecycle insights doesn't use anything, but I've wired it up to make the filters work. I expect that we'll iterate on the API endpoints etc later. 
97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
import { useEffect, useCallback, useMemo } from 'react';
|
|
import { useSearchParams } from 'react-router-dom';
|
|
import { createLocalStorage } from 'utils/createLocalStorage';
|
|
import { encodeQueryParams, useQueryParams } from 'use-query-params';
|
|
import type { QueryParamConfigMap } from 'serialize-query-params/src/types';
|
|
import { reorderObject } from '../utils/reorderObject.js';
|
|
|
|
const usePersistentSearchParams = <T extends QueryParamConfigMap>(
|
|
key: string,
|
|
queryParamsDefinition: T,
|
|
) => {
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
const { value, setValue } = createLocalStorage(key, {});
|
|
useEffect(() => {
|
|
const params = Object.fromEntries(searchParams.entries());
|
|
if (Object.keys(params).length > 0) {
|
|
return;
|
|
}
|
|
if (Object.keys(value).length === 0) {
|
|
return;
|
|
}
|
|
setSearchParams(
|
|
encodeQueryParams(queryParamsDefinition, value) as Record<
|
|
string,
|
|
string
|
|
>,
|
|
{ replace: true },
|
|
);
|
|
}, []);
|
|
|
|
return setValue;
|
|
};
|
|
|
|
export const usePersistentTableState = <T extends QueryParamConfigMap>(
|
|
key: string,
|
|
queryParamsDefinition: T,
|
|
excludedFromStorage: (keyof T)[] = ['offset'],
|
|
) => {
|
|
const updateStoredParams = usePersistentSearchParams(
|
|
key,
|
|
queryParamsDefinition,
|
|
);
|
|
|
|
const [tableState, setTableStateInternal] = useQueryParams(
|
|
queryParamsDefinition,
|
|
{ updateType: 'replaceIn' },
|
|
);
|
|
|
|
const [searchParams] = useSearchParams();
|
|
const orderedTableState = useMemo(() => {
|
|
return reorderObject(tableState, [...searchParams.keys()]);
|
|
}, [searchParams, tableState, reorderObject]);
|
|
|
|
type SetTableStateInternalParam = Parameters<
|
|
typeof setTableStateInternal
|
|
>[0];
|
|
|
|
const setTableState = useCallback(
|
|
(newState: SetTableStateInternalParam) => {
|
|
if (!queryParamsDefinition.offset) {
|
|
return setTableStateInternal(newState);
|
|
}
|
|
if (typeof newState === 'function') {
|
|
setTableStateInternal((prevState) => {
|
|
const updatedState = (newState as Function)(prevState);
|
|
return queryParamsDefinition.offset
|
|
? {
|
|
offset: queryParamsDefinition.offset.decode('0'),
|
|
...updatedState,
|
|
}
|
|
: updatedState;
|
|
});
|
|
} else {
|
|
const updatedState = queryParamsDefinition.offset
|
|
? {
|
|
offset: queryParamsDefinition.offset.decode('0'),
|
|
...newState,
|
|
}
|
|
: newState;
|
|
setTableStateInternal(updatedState);
|
|
}
|
|
},
|
|
[setTableStateInternal, queryParamsDefinition.offset],
|
|
);
|
|
|
|
useEffect(() => {
|
|
const filteredTableState = Object.fromEntries(
|
|
Object.entries(orderedTableState).filter(
|
|
([key]) => !excludedFromStorage.includes(key),
|
|
),
|
|
);
|
|
updateStoredParams(filteredTableState);
|
|
}, [JSON.stringify(orderedTableState)]);
|
|
|
|
return [orderedTableState, setTableState] as const;
|
|
};
|