chore: exclude local resource artifacts from main sync
This commit is contained in:
62
src/layer/search/context/SearchLayerContext.tsx
Executable file → Normal file
62
src/layer/search/context/SearchLayerContext.tsx
Executable file → Normal file
@@ -1,11 +1,13 @@
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type PropsWithChildren,
|
||||
} from 'react';
|
||||
import { SearchCommandModal, type SearchKeywordOption } from '../../../components/search';
|
||||
import { pushRecentSearchOption, resolveRecentSearchOptions } from '../../../app/main/searchRecent';
|
||||
import type { SearchLayerSnapshot, SearchOpenMode, SearchWindowSelection, SearchWindowSelectionDraft } from '../types';
|
||||
|
||||
type SearchLayerContextValue = SearchLayerSnapshot & {
|
||||
@@ -17,12 +19,46 @@ type SearchLayerContextValue = SearchLayerSnapshot & {
|
||||
};
|
||||
|
||||
const SearchLayerContext = createContext<SearchLayerContextValue | null>(null);
|
||||
const PREVIEW_APP_SELECTION_ID = 'page:preview:app';
|
||||
|
||||
function appendSelection(
|
||||
previous: SearchWindowSelection[],
|
||||
selection: SearchWindowSelectionDraft,
|
||||
): SearchWindowSelection[] {
|
||||
const nextSelection: SearchWindowSelection = {
|
||||
...selection,
|
||||
instanceId: `window-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
};
|
||||
|
||||
if (selection.id === PREVIEW_APP_SELECTION_ID) {
|
||||
return [...previous.filter((item) => item.id !== PREVIEW_APP_SELECTION_ID), nextSelection];
|
||||
}
|
||||
|
||||
return [...previous, nextSelection];
|
||||
}
|
||||
|
||||
export function SearchLayerProvider({ children }: PropsWithChildren) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [options, setOptions] = useState<SearchKeywordOption[]>([]);
|
||||
const [mode, setMode] = useState<SearchOpenMode>('navigate');
|
||||
const [windowSelections, setWindowSelections] = useState<SearchWindowSelection[]>([]);
|
||||
const [recentOptions, setRecentOptions] = useState<SearchKeywordOption[]>([]);
|
||||
|
||||
const appendWindowSelection = (option: SearchKeywordOption) => {
|
||||
setWindowSelections((previous) =>
|
||||
appendSelection(previous, {
|
||||
id: option.id,
|
||||
label: option.label,
|
||||
group: option.group,
|
||||
description: option.description,
|
||||
keywords: option.keywords ?? [],
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setRecentOptions(resolveRecentSearchOptions(options, mode));
|
||||
}, [mode, open, options]);
|
||||
|
||||
const value = useMemo<SearchLayerContextValue>(
|
||||
() => ({
|
||||
@@ -46,13 +82,7 @@ export function SearchLayerProvider({ children }: PropsWithChildren) {
|
||||
return;
|
||||
}
|
||||
|
||||
setWindowSelections((previous) => [
|
||||
...previous,
|
||||
...selections.map((selection) => ({
|
||||
...selection,
|
||||
instanceId: `window-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
})),
|
||||
]);
|
||||
setWindowSelections((previous) => selections.reduce((result, selection) => appendSelection(result, selection), previous));
|
||||
},
|
||||
}),
|
||||
[mode, open, options, windowSelections],
|
||||
@@ -64,22 +94,16 @@ export function SearchLayerProvider({ children }: PropsWithChildren) {
|
||||
<SearchCommandModal
|
||||
open={open}
|
||||
options={options}
|
||||
recentOptions={recentOptions}
|
||||
mode={mode}
|
||||
onClose={value.closeSearch}
|
||||
onSelectOption={(option) => {
|
||||
if (mode === 'window') {
|
||||
pushRecentSearchOption(option.id, mode);
|
||||
setRecentOptions(resolveRecentSearchOptions(options, mode));
|
||||
|
||||
if (mode === 'window' || option.id === 'page:preview:app') {
|
||||
setOpen(false);
|
||||
setWindowSelections((previous) => [
|
||||
...previous,
|
||||
{
|
||||
instanceId: `window-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
id: option.id,
|
||||
label: option.label,
|
||||
group: option.group,
|
||||
description: option.description,
|
||||
keywords: option.keywords ?? [],
|
||||
},
|
||||
]);
|
||||
appendWindowSelection(option);
|
||||
(option.onSelectWindow ?? option.onSelect)();
|
||||
return;
|
||||
}
|
||||
|
||||
0
src/layer/search/hooks/useSearchLayer.ts
Executable file → Normal file
0
src/layer/search/hooks/useSearchLayer.ts
Executable file → Normal file
0
src/layer/search/index.ts
Executable file → Normal file
0
src/layer/search/index.ts
Executable file → Normal file
0
src/layer/search/types/index.ts
Executable file → Normal file
0
src/layer/search/types/index.ts
Executable file → Normal file
Reference in New Issue
Block a user