-
Notifications
You must be signed in to change notification settings - Fork 23
feat: view variables & namespaced query parameters #2620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a55d916
feat: view variables
adityathebe 3606370
fix: send request fingerprint on table query
adityathebe 2b17909
chore: rename to variables
adityathebe f1a6577
fix: view variables
adityathebe a6af3e5
feat(view-state): create a generic URL state manager partitioned by
adityathebe dbe6b1d
fix: FilterByCellValue in view tables
adityathebe 0154e42
fix: review comments
adityathebe e4562f2
fix: state management for global filter
adityathebe d89776e
fix(usePrefixedSearchParams): allow some global keys without requiring
adityathebe fa1a030
fix: review comments
adityathebe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useCallback, useMemo } from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
|
||
/** | ||
* Hook that manages URL search params with a specific prefix. | ||
* Provides filtered params (without prefix) and a setter that adds the prefix. | ||
* | ||
* @param prefix - The prefix to use for this component's params (e.g., 'viewvar', 'view_namespace_name') | ||
*/ | ||
export function usePrefixedSearchParams( | ||
prefix: string | ||
): [ | ||
URLSearchParams, | ||
(updater: (prev: URLSearchParams) => URLSearchParams) => void | ||
] { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
|
||
const prefixedParams = useMemo(() => { | ||
const filtered = new URLSearchParams(); | ||
const prefixWithSeparator = `${prefix}__`; | ||
|
||
Array.from(searchParams.entries()).forEach(([key, value]) => { | ||
if (["sortBy", "sortOrder"].includes(key)) { | ||
adityathebe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
filtered.set(key, value); | ||
} else if (key.startsWith(prefixWithSeparator)) { | ||
const cleanKey = key.substring(prefixWithSeparator.length); | ||
filtered.set(cleanKey, value); | ||
} | ||
}); | ||
|
||
return filtered; | ||
}, [searchParams, prefix]); | ||
|
||
// Setter that adds prefix to keys when updating URL | ||
const setPrefixedParams = useCallback( | ||
(updater: (prev: URLSearchParams) => URLSearchParams) => { | ||
setSearchParams((currentParams) => { | ||
const newParams = new URLSearchParams(currentParams); | ||
const prefixWithSeparator = `${prefix}__`; | ||
|
||
// Remove all existing params with our prefix | ||
Array.from(currentParams.entries()).forEach(([key]) => { | ||
if (key.startsWith(prefixWithSeparator)) { | ||
newParams.delete(key); | ||
} | ||
}); | ||
|
||
// Get the updated params from the updater | ||
const updatedParams = updater(prefixedParams); | ||
|
||
// Add new params with prefix | ||
Array.from(updatedParams.entries()).forEach(([key, value]) => { | ||
if (value && value.trim() !== "") { | ||
newParams.set(`${prefixWithSeparator}${key}`, value); | ||
} | ||
}); | ||
|
||
return newParams; | ||
}); | ||
}, | ||
[setSearchParams, prefixedParams, prefix] | ||
); | ||
|
||
return [prefixedParams, setPrefixedParams]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useMemo } from "react"; | ||
import { | ||
GroupByOptions, | ||
MultiSelectDropdown | ||
} from "../../../../ui/Dropdowns/MultiSelectDropdown"; | ||
import { ViewVariable } from "../../types"; | ||
import { formatDisplayLabel } from "./panels/utils"; | ||
import { useField } from "formik"; | ||
|
||
interface DropdownProps { | ||
label: string; | ||
paramsKey: string; | ||
options: string[]; | ||
} | ||
|
||
const Dropdown: React.FC<DropdownProps> = ({ label, paramsKey, options }) => { | ||
const [field] = useField({ | ||
name: paramsKey | ||
}); | ||
const dropdownOptions = useMemo(() => { | ||
const mappedOptions = options.map( | ||
(option) => | ||
({ | ||
value: option, | ||
label: option | ||
}) satisfies GroupByOptions | ||
); | ||
|
||
return mappedOptions; | ||
}, [options]); | ||
|
||
return ( | ||
<MultiSelectDropdown | ||
label={label} | ||
options={dropdownOptions} | ||
value={dropdownOptions.find((option) => option.value === field.value)} | ||
onChange={(selectedOption: unknown) => { | ||
const option = selectedOption as GroupByOptions; | ||
field.onChange({ | ||
target: { name: paramsKey, value: option?.value } | ||
}); | ||
}} | ||
className="w-auto max-w-[400px]" | ||
isMulti={false} | ||
closeMenuOnSelect={true} | ||
isClearable={false} | ||
/> | ||
); | ||
}; | ||
|
||
interface GlobalFiltersProps { | ||
variables?: ViewVariable[]; | ||
} | ||
|
||
const GlobalFilters: React.FC<GlobalFiltersProps> = ({ variables }) => { | ||
const filterComponents = useMemo(() => { | ||
if (!variables || variables.length === 0) return []; | ||
|
||
return variables.map((variable) => ( | ||
<Dropdown | ||
key={variable.key} | ||
label={variable.label || formatDisplayLabel(variable.key)} | ||
paramsKey={variable.key} | ||
options={variable.options} | ||
/> | ||
)); | ||
}, [variables]); | ||
|
||
if (!variables || variables.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="mb-4"> | ||
<div className="flex flex-wrap items-center gap-2"> | ||
{filterComponents} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default GlobalFilters; |
90 changes: 90 additions & 0 deletions
90
src/pages/audit-report/components/View/GlobalFiltersForm.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Form, Formik, useFormikContext } from "formik"; | ||
import { useEffect } from "react"; | ||
import { usePrefixedSearchParams } from "../../../../hooks/usePrefixedSearchParams"; | ||
import { ViewVariable } from "../../types"; | ||
|
||
type GlobalFiltersFormProps = { | ||
children: React.ReactNode; | ||
variables: ViewVariable[]; | ||
globalVarPrefix: string; | ||
currentVariables?: Record<string, string>; | ||
}; | ||
|
||
function GlobalFiltersListener({ | ||
children, | ||
variables, | ||
globalVarPrefix, | ||
currentVariables = {} | ||
}: GlobalFiltersFormProps): React.ReactElement { | ||
const { values, setFieldValue } = | ||
useFormikContext<Record<string, string | undefined>>(); | ||
const [globalParams, setGlobalParams] = | ||
usePrefixedSearchParams(globalVarPrefix); | ||
|
||
useEffect(() => { | ||
setGlobalParams(() => { | ||
const newParams = new URLSearchParams(); | ||
|
||
variables.forEach((variable) => { | ||
const value = values[variable.key]; | ||
if (value) { | ||
newParams.set(variable.key, value); | ||
} | ||
}); | ||
|
||
return newParams; | ||
}); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [values, setGlobalParams]); | ||
|
||
// Initialize form values when variables load or URL params change | ||
useEffect(() => { | ||
variables.forEach((variable) => { | ||
const urlValue = globalParams.get(variable.key); | ||
const currentValue = currentVariables[variable.key]; | ||
const defaultValue = | ||
variable.default || | ||
(variable.options.length > 0 ? variable.options[0] : ""); | ||
adityathebe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const valueToUse = urlValue || currentValue || defaultValue; | ||
if (valueToUse) { | ||
setFieldValue(variable.key, valueToUse); | ||
} | ||
}); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [globalParams.toString(), variables, currentVariables, setFieldValue]); | ||
|
||
return children as React.ReactElement; | ||
} | ||
|
||
/** | ||
* Global filters form that manages view-level filter parameters. | ||
* This handles synchronization between Formik form state and URL parameters | ||
* for global filters that affect the entire view (panels + table). | ||
*/ | ||
export default function GlobalFiltersForm({ | ||
children, | ||
variables, | ||
globalVarPrefix, | ||
currentVariables | ||
}: GlobalFiltersFormProps) { | ||
return ( | ||
<Formik | ||
initialValues={{}} | ||
onSubmit={() => { | ||
// Form submission is handled by the listener | ||
}} | ||
enableReinitialize | ||
> | ||
<Form> | ||
<GlobalFiltersListener | ||
variables={variables} | ||
globalVarPrefix={globalVarPrefix} | ||
currentVariables={currentVariables} | ||
> | ||
{children} | ||
</GlobalFiltersListener> | ||
</Form> | ||
</Formik> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.