feat: 将搜索页面由Strapi迁移至Direcuts
- 路由页面相关源码修改 - 类型标注与组合式API - 相关工具函数
This commit is contained in:
16
app/models/mappers/searchItemMapper.ts
Normal file
16
app/models/mappers/searchItemMapper.ts
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 搜索索引转换器
|
||||
* @param hit 搜索条目
|
||||
* @returns 转换后的搜索条目视图模型
|
||||
*
|
||||
* ---
|
||||
* @example
|
||||
* const view = toSearchItemView(item, 'products');
|
||||
*/
|
||||
export function toSearchItemView<T extends MeiliSearchItemType>(
|
||||
item: MeiliIndexMap[T],
|
||||
type: T
|
||||
): SearchItemView {
|
||||
const converter = converters[type];
|
||||
return converter ? converter(item) : null;
|
||||
}
|
||||
35
app/models/utils/search-converters.ts
Normal file
35
app/models/utils/search-converters.ts
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 各索引对应的转换函数表
|
||||
*/
|
||||
export const converters: {
|
||||
[K in keyof MeiliIndexMap]: (item: MeiliIndexMap[K]) => SearchItemView;
|
||||
} = {
|
||||
products: (item: MeiliIndexMap['products']): SearchItemView => ({
|
||||
id: item.id,
|
||||
type: 'product',
|
||||
title: item.name,
|
||||
summary: item.summary,
|
||||
}),
|
||||
|
||||
solutions: (item: MeiliIndexMap['solutions']): SearchItemView => ({
|
||||
id: item.id,
|
||||
type: 'solution',
|
||||
title: item.title,
|
||||
summary: item.summary,
|
||||
}),
|
||||
|
||||
questions: (item: MeiliIndexMap['questions']): SearchItemView => ({
|
||||
id: item.id,
|
||||
type: 'question',
|
||||
title: item.title,
|
||||
summary: item.content.slice(0, 100) + '...',
|
||||
}),
|
||||
|
||||
product_documents: (
|
||||
item: MeiliIndexMap['product_documents']
|
||||
): SearchItemView => ({
|
||||
id: item.id,
|
||||
type: 'document',
|
||||
title: item.title,
|
||||
}),
|
||||
};
|
||||
13
app/models/views/SearchItemView.ts
Normal file
13
app/models/views/SearchItemView.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export interface SearchItemView {
|
||||
/** 唯一标识符 **/
|
||||
id: number;
|
||||
|
||||
/** 条目类型 **/
|
||||
type: 'product' | 'solution' | 'question' | 'document';
|
||||
|
||||
/** 条目标题 **/
|
||||
title: string;
|
||||
|
||||
/** 条目摘要 **/
|
||||
summary?: string;
|
||||
}
|
||||
Reference in New Issue
Block a user