Skip to content

Commit

Permalink
fix: switch view reset search params
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxing9 committed Nov 22, 2024
1 parent 50cf813 commit 609333b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import type { IGridRef } from '@teable/sdk';
import { noop } from 'lodash';
import { create } from 'zustand';

interface IGridRefState {
gridRef: React.RefObject<IGridRef> | null;
setGridRef: (ref: React.RefObject<IGridRef>) => void;
searchCursor: [number, number] | null;
setSearchCursor: (cell: [number, number] | null) => void;
resetSearchHandler: () => void;
setResetSearchHandler: (fn: () => void) => void;
}

export const useGridSearchStore = create<IGridRefState>((set) => ({
gridRef: null,
searchCursor: null,
resetSearchHandler: noop,
setResetSearchHandler: (fn: () => void) => {
set((state) => {
return {
...state,
resetSearchHandler: fn,
};
});
},
setGridRef: (ref: React.RefObject<IGridRef>) => {
set((state) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useTranslation } from 'next-i18next';
import { useState, useRef } from 'react';
import { useDownload } from '../../../hooks/useDownLoad';
import { VIEW_ICON_MAP } from '../constant';
import { useGridSearchStore } from '../grid/useGridSearchStore';
import { useDeleteView } from './useDeleteView';

interface IProps {
Expand All @@ -38,8 +39,10 @@ export const ViewListItem: React.FC<IProps> = ({ view, removable, isActive }) =>
downloadUrl: `/api/export/${tableId}?viewId=${view.id}`,
key: 'view',
});
const { resetSearchHandler } = useGridSearchStore();

const navigateHandler = () => {
resetSearchHandler?.();
router.push(
{
pathname: '/base/[baseId]/[tableId]/[viewId]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ export const SearchButton = (props: ISearchButtonProps) => {
const view = useView();
const viewId = view?.id;
const { fieldId, value, setFieldId, setValue, hideNotMatchRow, setHideNotMatchRow } = useSearch();

const [inputValue, setInputValue] = useState(value);
const [isFocused, setIsFocused] = useState(false);
const { t } = useTranslation(['common', 'table']);
const searchComposition = useRef(false);
const ref = useRef<HTMLInputElement>(null);
const { setSearchCursor } = useGridSearchStore();
const { setSearchCursor, setResetSearchHandler } = useGridSearchStore();
const [enableGlobalSearch, setEnableGlobalSearch] = useLocalStorage(
LocalStorageKeys.EnableGlobalSearch,
true
Expand Down Expand Up @@ -74,10 +75,15 @@ export const SearchButton = (props: ISearchButtonProps) => {
setValue();
setInputValue('');
setSearchCursor(null);
setActive(false);
}, [cancel, setSearchCursor, setValue]);

useEffect(() => {
setResetSearchHandler(resetSearch);
}, [resetSearch, setResetSearchHandler]);

const initSearchParams = useCallback(() => {
if (!tableId || !viewId) {
if (!tableId || !viewId || fields.length === 0) {
return;
}

Expand Down Expand Up @@ -125,6 +131,10 @@ export const SearchButton = (props: ISearchButtonProps) => {
viewId,
]);

useEffect(() => {
setSearchCursor(null);
}, [viewId, tableId, setSearchCursor]);

const onFieldChangeHandler = useCallback(
(fieldIds: string[] | null) => {
if (!tableId || !viewId) {
Expand Down Expand Up @@ -172,11 +182,6 @@ export const SearchButton = (props: ISearchButtonProps) => {
}
}, [active, initSearchParams]);

useEffect(() => {
setActive(false);
resetSearch();
}, [resetSearch, view?.id]);

useHotkeys<HTMLInputElement>(
`esc`,
() => {
Expand All @@ -190,10 +195,6 @@ export const SearchButton = (props: ISearchButtonProps) => {
}
);

useEffect(() => {
setActive(false);
}, [viewId]);

const searchHeader = useMemo(() => {
if (fieldId === 'all_fields') {
return t('noun.global');
Expand Down

0 comments on commit 609333b

Please sign in to comment.