Added highlight search setting and toggle in menu to trigger

Summary:
Created search highlight settings to be stored as part of persisted state. Added toggle/menu option in the dropdown.

This diff serves as the start of the stack that introduces highlighting the search terms in the search results of flipper. Currently, the results returned do not show why(the key word) they are appearing.

The next diffs will introduce not only the actual highlighting but also "custom"-ish color-picking for the highlights. The colors will be chosen from the sub-menu where the highlighting is enabled.

Reviewed By: mweststrate

Differential Revision: D37229735

fbshipit-source-id: d681f8e7b7fdfce8135c2c3fa81d8450447565c1
This commit is contained in:
Feiyu Wong
2022-06-29 10:36:52 -07:00
committed by Facebook GitHub Bot
parent 47c4504e7f
commit 24a314054e
3 changed files with 61 additions and 9 deletions

View File

@@ -18,6 +18,10 @@ export type Sorting<T = any> = {
key: keyof T;
direction: Exclude<SortDirection, undefined>;
};
export type SearchHighlightSetting = {
highlightEnabled: boolean;
color: string;
};
export type SortDirection = 'asc' | 'desc' | undefined;
@@ -46,6 +50,7 @@ type PersistedState = {
scrollOffset: number;
autoScroll: boolean;
searchHistory: string[];
highlightSearchSetting: SearchHighlightSetting;
};
type Action<Name extends string, Args = {}> = {type: Name} & Args;
@@ -100,7 +105,8 @@ type DataManagerActions<T> =
| Action<'toggleAutoScroll'>
| Action<'setAutoScroll', {autoScroll: boolean}>
| Action<'toggleSearchValue'>
| Action<'clearSearchHistory'>;
| Action<'clearSearchHistory'>
| Action<'toggleHighlightSearch'>;
type DataManagerConfig<T> = {
dataSource: DataSource<T, T[keyof T]>;
@@ -126,6 +132,7 @@ export type DataManagerState<T> = {
/** Used to remember the record entry to lookup when user presses ctrl */
previousSearchValue: string;
searchHistory: string[];
highlightSearchSetting: SearchHighlightSetting;
};
export type DataTableReducer<T> = Reducer<
@@ -297,6 +304,11 @@ export const dataTableManagerReducer = produce<
draft.autoScroll = action.autoScroll;
break;
}
case 'toggleHighlightSearch': {
draft.highlightSearchSetting.highlightEnabled =
!draft.highlightSearchSetting.highlightEnabled;
break;
}
default: {
throw new Error('Unknown action ' + (action as any).type);
}
@@ -328,6 +340,7 @@ export type DataTableManager<T> = {
setSearchValue(value: string, addToHistory?: boolean): void;
dataSource: DataSource<T, T[keyof T]>;
toggleSearchValue(): void;
toggleHighlightSearch(): void;
};
export function createDataTableManager<T>(
@@ -377,6 +390,9 @@ export function createDataTableManager<T>(
toggleSearchValue() {
dispatch({type: 'toggleSearchValue'});
},
toggleHighlightSearch() {
dispatch({type: 'toggleHighlightSearch'});
},
dataSource,
};
}
@@ -422,6 +438,10 @@ export function createInitialState<T>(
searchHistory: prefs?.searchHistory ?? [],
useRegex: prefs?.useRegex ?? false,
autoScroll: prefs?.autoScroll ?? config.autoScroll ?? false,
highlightSearchSetting: prefs?.highlightSearchSetting ?? {
highlightEnabled: false,
color: '',
},
};
// @ts-ignore
res.config[immerable] = false; // optimization: never proxy anything in config
@@ -500,6 +520,7 @@ export function savePreferences(
scrollOffset,
autoScroll: state.autoScroll,
searchHistory: state.searchHistory,
highlightSearchSetting: state.highlightSearchSetting,
};
localStorage.setItem(state.storageKey, JSON.stringify(prefs));
}