Skip to content

Commit

Permalink
feat(next/web): search v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd committed Jan 4, 2024
1 parent d2e57fc commit d92e9e6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
1 change: 1 addition & 0 deletions next/web/script/generateEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const OPTIONAL_ENV_KEYS = [
'ENABLE_LEANCLOUD_INTEGRATION',
'ALGOLIA_API_KEY',
'ENABLE_USER_CONFIRMATION',
'ENABLE_SEARCH_V2',
];

const CUSTOM_ENVS = {
Expand Down
20 changes: 6 additions & 14 deletions next/web/src/App/Admin/Tickets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down
44 changes: 39 additions & 5 deletions next/web/src/api/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']) };
}

Expand Down Expand Up @@ -311,20 +315,50 @@ 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<TicketSchema[]>('/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<TicketSchema[]>('/api/2/tickets/search-custom-field', {
q,
});
return { tickets: data, totalCount: Number(headers['x-total-count']) };
}

export function useSearchTicketCustomField(
q: string,
searchOptions: SearchTicketCustomFieldOptions,
options?: UseQueryOptions<FetchTicketsResult, Error>
) {
const { data, ...rest } = useQuery({
queryKey: ['searchTicketCustomField', q],
queryFn: () => searchTicketCustomField(q),
queryKey: ['searchTicketCustomField', searchOptions],
queryFn: () => searchTicketCustomField(searchOptions),
...options,
});

Expand Down
1 change: 1 addition & 0 deletions next/web/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);

0 comments on commit d92e9e6

Please sign in to comment.