web/src/utils/do-searchy-searchy.ts
70 lines · 1955 bytes
1import type { Category } from '../types/Service';
2
3export interface SearchItem {
4 type: 'Category' | 'Section' | 'Service';
5 category: string;
6 itemCount?: number;
7 sectionName?: string;
8 description?: string;
9 name?: string;
10 url?: string;
11 github?: string;
12 logo?: string;
13}
14
15export const prepareSearchItems = (categories: Category[]): SearchItem[] => {
16 const items: SearchItem[] = [];
17 // Add each category
18 categories.forEach((category) => {
19 items.push({
20 type: 'Category',
21 category: category.name,
22 itemCount: (category.sections || []).reduce((acc, section) => {
23 return acc + (section.services || []).length;
24 }, 0),
25 });
26
27 // Add section with category context
28 category.sections.forEach((section) => {
29 items.push({
30 type: 'Section',
31 sectionName: section.name,
32 description: section.intro || '',
33 category: category.name,
34 itemCount: (section.services || []).length,
35 });
36
37 // Add service with section and category context
38 (section.services || []).forEach((service) => {
39 items.push({
40 type: 'Service',
41 name: service.name,
42 description: service.description,
43 url: service.url,
44 github: service.github || '',
45 category: category.name,
46 sectionName: section.name,
47 logo: service.icon || '',
48 });
49 });
50 });
51 });
52 return items;
53};
54
55export const searchOptions = {
56 includeScore: true,
57 keys: [
58 { name: 'name', weight: 0.9 },
59 { name: 'sectionName', weight: 0.8 },
60 { name: 'category', weight: 0.7 },
61 { name: 'notableMentions', weight: 0.5 },
62 { name: 'alternativeTo', weight: 0.5 },
63 { name: 'github', weight: 0.4 },
64 { name: 'url', weight: 0.2 },
65 { name: 'description', weight: 0.1 },
66 { name: 'intro', weight: 0.1 },
67 { name: 'furtherInfo', weight: 0.1 },
68 { name: 'wordOfWarning', weight: 0.1 },
69 ],
70};