|
| 1 | +import type {NextApiRequest, NextApiResponse} from 'next'; |
| 2 | +import Fuse from 'fuse.js'; |
| 3 | + |
| 4 | +import { |
| 5 | + SearchResults, |
| 6 | + GroupedSearchResults, |
| 7 | + searchResultCategories, |
| 8 | + SearchResultCategory, |
| 9 | + Status, |
| 10 | +} from '../../../../src/types'; |
| 11 | + |
| 12 | +import {slugify, stripMarkdownLinks} from '../../../../src/utils/various'; |
| 13 | + |
| 14 | +import {metadata, MetadataProperties} from '@shopify/polaris-tokens'; |
| 15 | +import iconMetadata from '@shopify/polaris-icons/metadata'; |
| 16 | +import components from '../../../../src/data/components.json'; |
| 17 | +import foundations from '../../../../src/data/foundations.json'; |
| 18 | + |
| 19 | +const MAX_RESULTS: {[key in SearchResultCategory]: number} = { |
| 20 | + foundations: 8, |
| 21 | + components: 6, |
| 22 | + tokens: 5, |
| 23 | + icons: 9, |
| 24 | +}; |
| 25 | + |
| 26 | +const getSearchResults = (query: string) => { |
| 27 | + if (query.length === 0) return []; |
| 28 | + |
| 29 | + let results: SearchResults = []; |
| 30 | + |
| 31 | + // Add components |
| 32 | + components.forEach(({frontMatter: {title, status}, description}) => { |
| 33 | + const typedStatus: Status | undefined = status |
| 34 | + ? { |
| 35 | + value: status.value.toLowerCase() as Status['value'], |
| 36 | + message: status.message, |
| 37 | + } |
| 38 | + : undefined; |
| 39 | + |
| 40 | + results.push({ |
| 41 | + id: slugify(`components ${title}`), |
| 42 | + category: 'components', |
| 43 | + score: 0, |
| 44 | + url: `/components/${slugify(title)}`, |
| 45 | + meta: { |
| 46 | + components: { |
| 47 | + title, |
| 48 | + description: stripMarkdownLinks(description), |
| 49 | + status: typedStatus, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + const {colors, depth, font, motion, shape, spacing, zIndex} = metadata; |
| 56 | + const tokenGroups = { |
| 57 | + colors, |
| 58 | + depth, |
| 59 | + font, |
| 60 | + motion, |
| 61 | + shape, |
| 62 | + spacing, |
| 63 | + zIndex, |
| 64 | + }; |
| 65 | + |
| 66 | + Object.entries(tokenGroups).forEach(([groupSlug, tokenGroup]) => { |
| 67 | + Object.entries(tokenGroup).forEach( |
| 68 | + ([tokenName, tokenProperties]: [string, MetadataProperties]) => { |
| 69 | + results.push({ |
| 70 | + id: slugify(`tokens ${tokenName}`), |
| 71 | + category: 'tokens', |
| 72 | + score: 0, |
| 73 | + url: `/tokens/${slugify(groupSlug)}#${tokenName}`, |
| 74 | + meta: { |
| 75 | + tokens: { |
| 76 | + category: groupSlug, |
| 77 | + token: { |
| 78 | + name: tokenName, |
| 79 | + description: tokenProperties.description || '', |
| 80 | + value: tokenProperties.value, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + }, |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + // Add icons |
| 90 | + Object.keys(iconMetadata).forEach((fileName) => { |
| 91 | + results.push({ |
| 92 | + id: slugify(`icons ${fileName} ${iconMetadata[fileName].set}`), |
| 93 | + category: 'icons', |
| 94 | + url: `/icons?icon=${fileName}`, |
| 95 | + score: 0, |
| 96 | + meta: { |
| 97 | + icons: { |
| 98 | + icon: iconMetadata[fileName], |
| 99 | + }, |
| 100 | + }, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + // Add foundations |
| 105 | + foundations.forEach((data) => { |
| 106 | + const {title, icon} = data.frontMatter; |
| 107 | + const {description, category} = data; |
| 108 | + const url = `/foundations/${category}/${slugify(title)}`; |
| 109 | + |
| 110 | + results.push({ |
| 111 | + id: slugify(`foundations ${title}`), |
| 112 | + category: 'foundations', |
| 113 | + score: 0, |
| 114 | + url, |
| 115 | + meta: { |
| 116 | + foundations: { |
| 117 | + title, |
| 118 | + icon: icon || '', |
| 119 | + description, |
| 120 | + category: category || '', |
| 121 | + }, |
| 122 | + }, |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + const fuse = new Fuse(results, { |
| 127 | + keys: [ |
| 128 | + // Foundations |
| 129 | + {name: 'meta.foundations.title', weight: 100}, |
| 130 | + {name: 'meta.foundations.description', weight: 50}, |
| 131 | + |
| 132 | + // Components |
| 133 | + {name: 'meta.components.title', weight: 100}, |
| 134 | + {name: 'meta.components.description', weight: 50}, |
| 135 | + |
| 136 | + // Tokens |
| 137 | + {name: 'meta.tokens.token.name', weight: 200}, |
| 138 | + {name: 'meta.tokens.token.value', weight: 50}, |
| 139 | + |
| 140 | + // Icons |
| 141 | + {name: 'meta.icons.icon.fileName', weight: 50}, |
| 142 | + {name: 'meta.icons.icon.name', weight: 50}, |
| 143 | + {name: 'meta.icons.icon.keywords', weight: 20}, |
| 144 | + {name: 'meta.icons.icon.set', weight: 20}, |
| 145 | + {name: 'meta.icons.icon.description', weight: 50}, |
| 146 | + ], |
| 147 | + includeScore: true, |
| 148 | + threshold: 0.5, |
| 149 | + shouldSort: true, |
| 150 | + ignoreLocation: true, |
| 151 | + }); |
| 152 | + |
| 153 | + const groupedResults: GroupedSearchResults = []; |
| 154 | + |
| 155 | + const fuseResults = fuse.search(query); |
| 156 | + |
| 157 | + const scoredResults: SearchResults = fuseResults.map((result) => ({ |
| 158 | + ...result.item, |
| 159 | + score: result.score || 0, |
| 160 | + })); |
| 161 | + |
| 162 | + searchResultCategories.forEach((category) => { |
| 163 | + groupedResults.push({ |
| 164 | + category, |
| 165 | + results: scoredResults |
| 166 | + .filter((result) => result.category === category) |
| 167 | + .map((result) => ({...result, score: result.score || 0})) |
| 168 | + .slice(0, MAX_RESULTS[category]), |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + groupedResults.sort( |
| 173 | + (a, b) => (a.results[0]?.score || 0) - (b.results[0]?.score || 0), |
| 174 | + ); |
| 175 | + |
| 176 | + return groupedResults; |
| 177 | +}; |
| 178 | + |
| 179 | +const handler = async (req: NextApiRequest, res: NextApiResponse) => { |
| 180 | + const query = Array.isArray(req.query.q) |
| 181 | + ? req.query.q.join(' ') |
| 182 | + : req.query.q; |
| 183 | + |
| 184 | + const results = {results: getSearchResults(query)}; |
| 185 | + res.status(200).json(results); |
| 186 | +}; |
| 187 | + |
| 188 | +export default handler; |
0 commit comments