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

View File

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

View File

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