Skip to content

Commit

Permalink
Merge pull request #46406 from software-mansion-labs/search/selected-…
Browse files Browse the repository at this point in the history
…transactions-in-context

[Search] Move selectedTransactions array to SearchContext
  • Loading branch information
luacmartins authored Jul 31, 2024
2 parents e834e5f + d27a97c commit 00a45ac
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 317 deletions.
55 changes: 32 additions & 23 deletions src/components/Search/SearchContext.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
import React, {useCallback, useContext, useMemo, useState} from 'react';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import type {SearchContext} from './types';
import type {SearchContext, SelectedTransactions} from './types';

const defaultSearchContext = {
currentSearchHash: -1,
selectedTransactionIDs: [],
selectedTransactions: {},
setCurrentSearchHash: () => {},
setSelectedTransactionIDs: () => {},
setSelectedTransactions: () => {},
clearSelectedTransactions: () => {},
};

const Context = React.createContext<SearchContext>(defaultSearchContext);

function SearchContextProvider({children}: ChildrenProps) {
const [searchContextData, setSearchContextData] = useState<Pick<SearchContext, 'currentSearchHash' | 'selectedTransactionIDs'>>({
const [searchContextData, setSearchContextData] = useState<Pick<SearchContext, 'currentSearchHash' | 'selectedTransactions'>>({
currentSearchHash: defaultSearchContext.currentSearchHash,
selectedTransactionIDs: defaultSearchContext.selectedTransactionIDs,
selectedTransactions: defaultSearchContext.selectedTransactions,
});

const setCurrentSearchHash = useCallback(
(searchHash: number) => {
setSearchContextData({
...searchContextData,
currentSearchHash: searchHash,
});
},
[searchContextData],
);
const setCurrentSearchHash = useCallback((searchHash: number) => {
setSearchContextData((prevState) => ({
...prevState,
currentSearchHash: searchHash,
}));
}, []);

const setSelectedTransactions = useCallback((selectedTransactions: SelectedTransactions) => {
setSearchContextData((prevState) => ({
...prevState,
selectedTransactions,
}));
}, []);

const setSelectedTransactionIDs = useCallback(
(selectedTransactionIDs: string[]) => {
setSearchContextData({
...searchContextData,
selectedTransactionIDs,
});
const clearSelectedTransactions = useCallback(
(searchHash?: number) => {
if (searchHash === searchContextData.currentSearchHash) {
return;
}
setSearchContextData((prevState) => ({
...prevState,
selectedTransactions: {},
}));
},
[searchContextData],
[searchContextData.currentSearchHash],
);

const searchContext = useMemo<SearchContext>(
() => ({
...searchContextData,
setCurrentSearchHash,
setSelectedTransactionIDs,
setSelectedTransactions,
clearSelectedTransactions,
}),
[searchContextData, setCurrentSearchHash, setSelectedTransactionIDs],
[searchContextData, setCurrentSearchHash, setSelectedTransactions, clearSelectedTransactions],
);

return <Context.Provider value={searchContext}>{children}</Context.Provider>;
Expand Down
206 changes: 0 additions & 206 deletions src/components/Search/SearchListWithHeader.tsx

This file was deleted.

Loading

0 comments on commit 00a45ac

Please sign in to comment.