From d92e9e6bf2f7287bb853101b91d0e084f681413f Mon Sep 17 00:00:00 2001 From: sdjdd Date: Thu, 4 Jan 2024 14:09:31 +0800 Subject: [PATCH] feat(next/web): search v2 --- next/web/script/generateEnv.js | 1 + next/web/src/App/Admin/Tickets/index.tsx | 20 ++++------- next/web/src/api/ticket.ts | 44 +++++++++++++++++++++--- next/web/src/global.d.ts | 1 + 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/next/web/script/generateEnv.js b/next/web/script/generateEnv.js index ffde49db5..e899347dc 100644 --- a/next/web/script/generateEnv.js +++ b/next/web/script/generateEnv.js @@ -8,6 +8,7 @@ const OPTIONAL_ENV_KEYS = [ 'ENABLE_LEANCLOUD_INTEGRATION', 'ALGOLIA_API_KEY', 'ENABLE_USER_CONFIRMATION', + 'ENABLE_SEARCH_V2', ]; const CUSTOM_ENVS = { diff --git a/next/web/src/App/Admin/Tickets/index.tsx b/next/web/src/App/Admin/Tickets/index.tsx index d49072ecd..453806276 100644 --- a/next/web/src/App/Admin/Tickets/index.tsx +++ b/next/web/src/App/Admin/Tickets/index.tsx @@ -88,15 +88,11 @@ function useSmartSearchTickets({ const { fieldId, textValue } = filters as FieldFilters; const useFieldSearchResult = useSearchTicketCustomField( - `values.field:${fieldId ? encodeURIComponent(fieldId) : '*'} AND values.value:${ - textValue ? encodeURIComponent(textValue) : '*' - }${ - dateRange - ? ` AND createdAt:[${dateRange.from ? `"${dateRange.from.toISOString()}"` : '*'} TO ${ - dateRange.to ? `"${dateRange.to.toISOString()}"` : '*' - }]` - : '' - }`, + { + fieldId, + fieldValue: textValue, + createdAt: dateRange ? [dateRange.from, dateRange.to] : undefined, + }, { enabled: isFieldSearch, } @@ -120,11 +116,7 @@ function TicketListView() { const [localFilters, setLocalFilters] = useLocalFilters(); const [type] = useTicketSwitchType(); - const { - data: tickets, - totalCount, - isFetching, - } = useSmartSearchTickets({ + const { data: tickets, totalCount, isFetching } = useSmartSearchTickets({ page, pageSize, orderKey, diff --git a/next/web/src/api/ticket.ts b/next/web/src/api/ticket.ts index 58f795c60..52d999002 100644 --- a/next/web/src/api/ticket.ts +++ b/next/web/src/api/ticket.ts @@ -168,7 +168,11 @@ async function searchTickets( orderBy: `${orderKey}-${orderType}`, }; - const { headers, data } = await http.get('/api/2/tickets/search', { params }); + const url = import.meta.env.VITE_ENABLE_SEARCH_V2 + ? '/api/2/tickets/search/v2' + : '/api/2/tickets/search'; + + const { headers, data } = await http.get(url, { params }); return { tickets: data, totalCount: parseInt(headers['x-total-count']) }; } @@ -311,7 +315,37 @@ export function useOperateTicket( }); } -async function searchTicketCustomField(q: string) { +interface SearchTicketCustomFieldOptions { + fieldId?: string; + fieldValue?: string; + createdAt?: [Date | undefined, Date | undefined]; +} + +async function searchTicketCustomField({ + fieldId, + fieldValue, + createdAt, +}: SearchTicketCustomFieldOptions) { + if (import.meta.env.VITE_ENABLE_SEARCH_V2) { + const res = await http.get('/api/2/tickets/search/v2', { + params: { + fieldId, + fieldValue, + createdAt: createdAt?.map((d) => (d ? d.toISOString() : '*')).join('..'), + }, + }); + return { tickets: res.data, totalCount: Number(res.headers['x-total-count']) }; + } + + const q = `values.field:${fieldId ? encodeURIComponent(fieldId) : '*'} AND values.value:${ + fieldValue ? encodeURIComponent(fieldValue) : '*' + }${ + createdAt + ? ` AND createdAt:[${createdAt[0] ? `"${createdAt[0].toISOString()}"` : '*'} TO ${ + createdAt[1] ? `"${createdAt[1].toISOString()}"` : '*' + }]` + : '' + }`; const { data, headers } = await http.post('/api/2/tickets/search-custom-field', { q, }); @@ -319,12 +353,12 @@ async function searchTicketCustomField(q: string) { } export function useSearchTicketCustomField( - q: string, + searchOptions: SearchTicketCustomFieldOptions, options?: UseQueryOptions ) { const { data, ...rest } = useQuery({ - queryKey: ['searchTicketCustomField', q], - queryFn: () => searchTicketCustomField(q), + queryKey: ['searchTicketCustomField', searchOptions], + queryFn: () => searchTicketCustomField(searchOptions), ...options, }); diff --git a/next/web/src/global.d.ts b/next/web/src/global.d.ts index 2189c2f7a..dca5594f2 100644 --- a/next/web/src/global.d.ts +++ b/next/web/src/global.d.ts @@ -9,6 +9,7 @@ interface ImportMetaEnv { VITE_ALGOLIA_API_KEY?: string; VITE_ENABLE_TAP_SUPPORT?: string; VITE_ENABLE_USER_CONFIRMATION?: string; + VITE_ENABLE_SEARCH_V2?: string; } declare function docsearch(...args: any[]);