chore: remove redundant code

This commit is contained in:
Hossein Mehrabi 2023-09-28 16:34:01 +03:30
parent 06c1fd0e54
commit a41d1a97e8
No known key found for this signature in database
GPG Key ID: 45C04964191AFAA1
3 changed files with 10 additions and 21 deletions

View File

@ -10,7 +10,6 @@ export type PostSearchContextType = {
query?: string query?: string
fetching?: boolean fetching?: boolean
results: Block[] results: Block[]
mappedResults?: Record<string, Block>
} }
export const PostSearchContext = React.createContext<PostSearchContextType>({ export const PostSearchContext = React.createContext<PostSearchContextType>({
@ -18,14 +17,12 @@ export const PostSearchContext = React.createContext<PostSearchContextType>({
query: '', query: '',
fetching: false, fetching: false,
results: [], results: [],
mappedResults: {},
}) })
export const usePostSearch = () => export const usePostSearch = () =>
useContext(PostSearchContext) || { useContext(PostSearchContext) || {
query: '', query: '',
results: [], results: [],
mappedResults: {},
active: false, active: false,
fetching: false, fetching: false,
} }

View File

@ -73,7 +73,7 @@ export const PostSearchContainer: React.FC<
globalMode={false} globalMode={false}
showCloseButton showCloseButton
fetching={res.isLoading} fetching={res.isLoading}
numberOfResults={res.data?.result?.length} numberOfResults={res.data?.length}
/> />
</SearchBoxContainer> </SearchBoxContainer>
<PostSearchContext.Provider <PostSearchContext.Provider
@ -81,8 +81,7 @@ export const PostSearchContainer: React.FC<
query, query,
fetching: res.isLoading, fetching: res.isLoading,
active: active && query.length > 0, active: active && query.length > 0,
results: res.data?.result || [], results: res.data || [],
mappedResults: res.data?.mapped || {},
}} }}
> >
{children} {children}

View File

@ -14,21 +14,14 @@ export const usePostSearchQuery = ({
limit?: number limit?: number
active?: boolean active?: boolean
}) => }) =>
useQuery<{ useQuery(['post-search-query', active, id, query, limit], async () =>
result: LPE.Search.ResultBlockItem[]
mapped: Record<string, LPE.Search.ResultBlockItem>
}>(['post-search-query', active, id, query, limit], async () =>
!active !active
? { result: [], mapped: {} } ? []
: api.searchPostBlocks({ limit, id, query, skip: 0 }).then((res) => { : api
let items = res.data.blocks as LPE.Search.ResultBlockItem[] .searchPostBlocks({ limit, id, query, skip: 0 })
items = items.filter(searchBlocksBasicFilter) .then((res) =>
(res.data.blocks as LPE.Search.ResultBlockItem[]).filter(
return { searchBlocksBasicFilter,
result: items, ),
mapped: Object.fromEntries(
items.map((item) => [item.data.id, item]),
), ),
}
}),
) )