Skip to content

Commit

Permalink
[RFC] Context to allow cloud to sort filter values (#17084)
Browse files Browse the repository at this point in the history
## Summary & Motivation

This is a pretty generic way for cloud to sort filter values. It allows
Cloud to specify a sorting function for a filter by it's name.
This means that multiple filters with the same name could be targeted by
cloud and this is intentional. The thinking behind it is that you might
have a "User" filter in many parts of the app and we would very likely
want cloud to target all of them instead of just a specific one.
  • Loading branch information
salazarm authored Oct 9, 2023
1 parent dcdcbeb commit 9f9f3fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type LaunchpadHooksContextValue = {
error: GenericError | PythonErrorFragment;
fallback?: React.ReactNode;
}>;
// TODO (salazarm): Remove this prop after cloud PR lands to override UserDisplay instead
RunCreatedByCell?: any;
StaticFilterSorter?: Record<string, (a: any, b: any) => number>;
};

export const LaunchpadHooksContext = React.createContext<LaunchpadHooksContextValue>({
LaunchRootExecutionButton: undefined,
useLaunchWithTelemetry: undefined,
StaticFilterSorter: undefined,
});

export function useLaunchPadHooks() {
Expand All @@ -33,6 +33,7 @@ export function useLaunchPadHooks() {
MaterializeButton: OverrideMaterializeButton,
UserDisplay: OverrideUserDisplay,
PythonErrorInfoHeader,
StaticFilterSorter,
} = React.useContext(LaunchpadHooksContext);

return {
Expand All @@ -41,5 +42,6 @@ export function useLaunchPadHooks() {
MaterializeButton: OverrideMaterializeButton ?? Button,
PythonErrorInfoHeader,
UserDisplay: OverrideUserDisplay ?? UserDisplay,
StaticFilterSorter,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Box, Checkbox, IconName, Popover} from '@dagster-io/ui-components';
import React from 'react';

import {useUpdatingRef} from '../../hooks/useUpdatingRef';
import {LaunchpadHooksContext} from '../../launchpad/LaunchpadHooksContext';

import {FilterObject, FilterTag, FilterTagHighlightedText} from './useFilter';

Expand Down Expand Up @@ -33,7 +34,7 @@ export function useStaticSetFilter<TValue>({
name,
icon,
getKey,
allValues,
allValues: _unsortedValues,
renderLabel,
renderActiveStateLabel,
initialState,
Expand All @@ -43,6 +44,16 @@ export function useStaticSetFilter<TValue>({
allowMultipleSelections = true,
matchType = 'any-of',
}: Args<TValue>): StaticSetFilter<TValue> {
const {StaticFilterSorter} = React.useContext(LaunchpadHooksContext);

const allValues = React.useMemo(() => {
const sorter = StaticFilterSorter?.[name];
if (sorter) {
return _unsortedValues.sort(sorter);
}
return _unsortedValues;
}, [StaticFilterSorter, name, _unsortedValues]);

const [state, setState] = React.useState(() => new Set(initialState || []));

React.useEffect(() => {
Expand Down

1 comment on commit 9f9f3fc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for dagit-core-storybook ready!

✅ Preview
https://dagit-core-storybook-cvmlwftug-elementl.vercel.app

Built with commit 9f9f3fc.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.