From a73b1b0964d3fa04cfc9497094609108973a5a34 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Wed, 3 Jul 2024 00:13:17 -0700 Subject: [PATCH] clean up FilteredConnection filter types and code (#63590) - Renames some types and fields for clarity: - FilteredConnectionFilter -> Filter - field name `values` -> `options` - FilteredConnectionFilterValue -> FilterOption - Avoids passing around unnecessary data No behavior change. This makes it easier to use the FilterControl component. ## Test plan Use existing filtered connections with filters (see the diff for a list of pages that contain this component). --- .../FilteredConnection/FilterControl.tsx | 78 ++++++++++++------- .../FilteredConnection/FilteredConnection.tsx | 51 ++++++------ .../FilteredConnection/ui/ConnectionForm.tsx | 10 +-- .../components/FilteredConnection/utils.ts | 30 ++++--- .../externalServices/ExternalServicesPage.tsx | 4 +- .../pages/CodeIntelConfigurationPage.tsx | 2 +- .../pages/CodeIntelPreciseIndexesPage.tsx | 20 ++--- .../executors/instances/ExecutorsListPage.tsx | 8 +- .../searchContexts/SearchContextsList.tsx | 12 +-- .../src/notebooks/listPage/NotebooksList.tsx | 6 +- .../notebooks/listPage/NotebooksListPage.tsx | 16 ++-- .../site-admin/SiteAdminFeatureFlagsPage.tsx | 16 ++-- .../site-admin/SiteAdminMigrationsPage.tsx | 24 +++--- .../SiteAdminOutboundRequestsPage.tsx | 8 +- .../src/site-admin/SiteAdminPackagesPage.tsx | 49 ++++++------ .../SiteAdminRepositoriesContainer.tsx | 70 +++++------------ .../site-admin/SiteAdminSlowRequestsPage.tsx | 8 +- .../SiteAdminSurveyResponsesPage.tsx | 18 ++--- .../site-admin/packages/AddFilterModal.tsx | 4 +- .../packages/ManageFiltersModal.tsx | 4 +- .../packages/components/MultiPackageForm.tsx | 14 ++-- .../packages/components/SinglePackageForm.tsx | 22 +++--- .../AddPackageFilterModalContent.tsx | 6 +- .../EditPackageFilterModalContent.tsx | 6 +- 24 files changed, 232 insertions(+), 254 deletions(-) diff --git a/client/web/src/components/FilteredConnection/FilterControl.tsx b/client/web/src/components/FilteredConnection/FilterControl.tsx index e42a8e54126..466c2c2518c 100644 --- a/client/web/src/components/FilteredConnection/FilterControl.tsx +++ b/client/web/src/components/FilteredConnection/FilterControl.tsx @@ -6,7 +6,41 @@ import { RadioButtons } from '../RadioButtons' import styles from './FilterControl.module.scss' -export interface FilteredConnectionFilterValue { +/** + * A filter to display next to the search input field. + */ +export interface Filter { + /** The UI label for the filter. */ + label: string + + /** The UI form control to use when displaying this filter. */ + type: 'radio' | 'select' + + /** + * The URL query parameter name for this filter (conventionally the label, lowercased and + * without spaces and punctuation). + */ + id: string + + /** An optional tooltip to display for this filter. */ + tooltip?: string + + /** + * All of the possible values for this filter that the user can select. + */ + options: FilterOption[] +} + +/** + * An option that the user can select for a filter ({@link Filter}). + */ +export interface FilterOption { + /** + * The value (corresponding to the key in {@link Filter.id}) if this option is chosen. For + * example, if a filter has {@link Filter.id} of `sort` and the user selects a + * {@link FilterOption} with {@link FilterOption.value} of `asc`, then the URL query string + * would be `sort=asc`. + */ value: string label: string tooltip?: string @@ -14,34 +48,18 @@ export interface FilteredConnectionFilterValue { } /** - * A filter to display next to the search input field. + * The values of all filters, keyed by the filter ID ({@link Filter.id}). */ -export interface FilteredConnectionFilter { - /** The UI label for the filter. */ - label: string - - /** "radio" or "select" */ - type: string - - /** - * The URL string for this filter (conventionally the label, lowercased and without spaces and punctuation). - */ - id: string - - /** An optional tooltip to display for this filter. */ - tooltip?: string - - values: FilteredConnectionFilterValue[] -} +export interface FilterValues extends Record {} interface FilterControlProps { /** All filters. */ - filters: FilteredConnectionFilter[] + filters: Filter[] /** Called when a filter is selected. */ - onValueSelect: (filter: FilteredConnectionFilter, value: FilteredConnectionFilterValue) => void + onValueSelect: (filter: Filter, value: FilterOption['value']) => void - values: Map + values: FilterValues } export const FilterControl: React.FunctionComponent> = ({ @@ -51,12 +69,12 @@ export const FilterControl: React.FunctionComponent { const onChange = useCallback( - (filter: FilteredConnectionFilter, id: string) => { - const value = filter.values.find(value => value.value === id) + (filter: Filter, id: string) => { + const value = filter.options.find(opt => opt.value === id) if (value === undefined) { return } - onValueSelect(filter, value) + onValueSelect(filter, value.value) }, [onValueSelect] ) @@ -70,8 +88,8 @@ export const FilterControl: React.FunctionComponent ({ + selected={values[filter.id] ?? undefined} + nodes={filter.options.map(({ value, label, tooltip }) => ({ tooltip, label, id: value, @@ -94,12 +112,12 @@ export const FilterControl: React.FunctionComponent onChange(filter, event.currentTarget.value)} - value={values.get(filter.id)?.value} + value={values[filter.id] ?? undefined} className="mb-0" isCustomStyle={true} > - {filter.values.map(value => ( -