From bb20079b4321cd9e266e0bf4a473de517969190a Mon Sep 17 00:00:00 2001 From: amirhouieh Date: Thu, 11 May 2023 11:31:35 +0200 Subject: [PATCH] fix --- src/hooks/useSearch.ts | 34 ++++--------------- .../api/{search.ts => search/[postType].ts} | 13 +++++-- src/pages/search.tsx | 13 +++++-- src/queries/searchBlocks.ts | 8 +++++ src/services/search.service.ts | 7 ++-- src/services/unbody.service.ts | 13 ++++--- src/types/data.types.ts | 11 +++++- 7 files changed, 59 insertions(+), 40 deletions(-) rename src/pages/api/{search.ts => search/[postType].ts} (53%) diff --git a/src/hooks/useSearch.ts b/src/hooks/useSearch.ts index 7dbbc03..1d0a38e 100644 --- a/src/hooks/useSearch.ts +++ b/src/hooks/useSearch.ts @@ -5,24 +5,27 @@ import { } from '@/lib/unbody/unbody.types' import searchApi from '@/services/search.service' import { + PostTypes, SearchHook, SearchHookDataPayload, SearchResultItem, - SearchResults, } from '@/types/data.types' +import { UnbodyGraphQl } from '@/lib/unbody/unbody-content.types' + import { useState } from 'react' export const useSearchGeneric = ( initialData: SearchResultItem[], + postType: PostTypes, ): SearchHook => { const [data, setData] = useState[]>(initialData) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const search = async (query: string, tags: string[]) => { - if (loading) return null + if (loading) return Promise.resolve([]) setLoading(true) - const result = await searchApi.searchArticles(query, tags) + const result = await searchApi.serach(query, tags, postType) setData(result.data) setLoading(false) return result.data @@ -35,28 +38,3 @@ export const useSearchGeneric = ( } return { data, loading, error, search, reset } } - -export const useSearch = ( - initialData: SearchHookDataPayload, -): SearchResults => { - const articles = useSearchGeneric( - initialData?.articles ?? null, - ) - const blocks = useSearchGeneric( - initialData?.blocks ?? null, - ) - - const search = async (query: string, tags: string[]) => { - const [articlesResult, blocksResult] = await Promise.all([ - articles.loading ? () => {} : articles.search(query, tags), - blocks.loading ? () => {} : blocks.search(query, tags), - ]) - } - - const reset = (initialData: SearchHookDataPayload) => { - articles.reset(initialData?.articles) - blocks.reset(initialData?.blocks) - } - - return { search, reset, blocks, articles } -} diff --git a/src/pages/api/search.ts b/src/pages/api/search/[postType].ts similarity index 53% rename from src/pages/api/search.ts rename to src/pages/api/search/[postType].ts index 8aaa228..5be2c5a 100644 --- a/src/pages/api/search.ts +++ b/src/pages/api/search/[postType].ts @@ -1,4 +1,5 @@ import api from '@/services/unbody.service' +import { PostTypes } from '@/types/data.types' import type { NextApiRequest, NextApiResponse } from 'next' export default async function handler( @@ -6,8 +7,12 @@ export default async function handler( res: NextApiResponse, ) { const { - query: { q = '', tags: tagsString = '' }, + query: { q = '', tags: tagsString = '', postType }, } = req + if (!Object.values(PostTypes).includes(postType as PostTypes)) { + return res.status(400).json({ error: 'Invalid postType' }) + } + const tags = typeof tagsString === 'string' ? tagsString @@ -15,6 +20,10 @@ export default async function handler( .map((tag: string) => tag.trim()) .filter((t) => t.length > 0) : undefined - const response = await api.searchArticles(q as string, tags) + const response = + postType === PostTypes.Article + ? await api.searchArticles(q as string, tags) + : await api.serachBlocks(q as string, tags) + res.status(200).json(response) } diff --git a/src/pages/search.tsx b/src/pages/search.tsx index d0f285c..d241aa3 100644 --- a/src/pages/search.tsx +++ b/src/pages/search.tsx @@ -1,4 +1,4 @@ -import { useSearch, useSearchGeneric } from '@/hooks/useSearch' +import { useSearchGeneric } from '@/hooks/useSearch' import { UnbodyGoogleDoc, UnbodyImageBlock, @@ -10,6 +10,7 @@ import Image from 'next/image' import unbodyApi from '@/services/unbody.service' import { + PostTypes, SearchHook, SearchHookDataPayload, SearchResultItem, @@ -43,11 +44,19 @@ export default function SearchPage({ query: { query = '', topics = [] }, } = router - const articles = useSearchGeneric(initialArticles) + const articles = useSearchGeneric( + initialArticles, + PostTypes.Article, + ) + const blocks = useSearchGeneric( initialBlocks, + PostTypes.Block, ) + // console.log('articles', articles) + // console.log('blocks', blocks) + const [mounted, setMounted] = useState(false) useEffect(() => { diff --git a/src/queries/searchBlocks.ts b/src/queries/searchBlocks.ts index f4125df..120b79b 100644 --- a/src/queries/searchBlocks.ts +++ b/src/queries/searchBlocks.ts @@ -19,9 +19,13 @@ export const getSearchBlocksQuery = (args: UnbodyGetFilters = defaultArgs) => ...on GoogleDoc{ title remoteId + slug __typename } } + _additional{ + certainty + } `), GetImageBlockQuery(args)(` __typename @@ -33,9 +37,13 @@ export const getSearchBlocksQuery = (args: UnbodyGetFilters = defaultArgs) => ...on GoogleDoc{ title remoteId + slug __typename } } + _additional{ + certainty + } `), ].join(' '), ) diff --git a/src/services/search.service.ts b/src/services/search.service.ts index 3f55c6a..c0c6bef 100644 --- a/src/services/search.service.ts +++ b/src/services/search.service.ts @@ -1,8 +1,9 @@ +import { PostTypes } from '@/types/data.types' + class SearchService { constructor() {} - - searchArticles = (query: string, tags: string[]) => { - return fetch(`/api/search?q=${query}&tags=${tags.join(',')}`) + serach = (query: string, tags: string[], postType: PostTypes) => { + return fetch(`/api/search/${postType}?q=${query}&tags=${tags.join(',')}`) .then((res) => res.json()) .catch((e) => { console.error(e) diff --git a/src/services/unbody.service.ts b/src/services/unbody.service.ts index 4e2cea9..67eaf9c 100644 --- a/src/services/unbody.service.ts +++ b/src/services/unbody.service.ts @@ -57,10 +57,10 @@ const OperandFactory = ( }) export const Operands: Record WhereOperandsInpObj> = { - WHERE_PUBLISHED: () => + WHERE_PUBLISHED: (subPath: string[] = ['pathString']) => OperandFactory( UnbodyGraphQl.Filters.WhereOperatorEnum.Like, - 'pathString', + [...subPath], '*/published/*', 'valueString', ), @@ -178,16 +178,21 @@ class UnbodyService extends UnbodyClient { ? { nearText: { concepts: [q, ...tags], - certainty: 0.75, + certainty: 0.85, }, ...(published ? { - where: Operands.WHERE_PUBLISHED(), + where: Operands.WHERE_PUBLISHED([ + 'document', + 'GoogleDoc', + 'pathString', + ]), } : {}), } : {}), }) + return this.request(query) .then(({ data }) => { if (!data || !(data.Get.ImageBlock || data.Get.TextBlock)) diff --git a/src/types/data.types.ts b/src/types/data.types.ts index df36c90..98155c3 100644 --- a/src/types/data.types.ts +++ b/src/types/data.types.ts @@ -5,6 +5,11 @@ import { } from '@/lib/unbody/unbody.types' import { UnbodyGraphQl } from '@/lib/unbody/unbody-content.types' +export enum PostTypes { + Article = 'article', + Block = 'block', +} + export interface ArticlePostData extends UnbodyGoogleDoc { toc: Array } @@ -32,7 +37,11 @@ export type SearchHookDataPayload = { export type SearchResults = { articles: SearchHook blocks: SearchHook - search: (query: string, tags: string[]) => Promise + search: ( + query: string, + tags: string[], + docType: UnbodyGraphQl.UnbodyDocumentTypeNames, + ) => Promise reset: (initialData: SearchHookDataPayload) => void }