Files
panel/resources/js/plugins/fake-api/handlers/app-bar-search/index.js
2025-08-04 16:33:07 +03:30

38 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import is from '@sindresorhus/is'
import { HttpResponse, http } from 'msw'
import { db } from '@db/app-bar-search/db'
export const handlerAppBarSearch = [
// Get Search Items
http.get('/api/app-bar/search', ({ request }) => {
const url = new URL(request.url)
const q = url.searchParams.get('q') ?? ''
const searchQuery = is.string(q) ? q : undefined
const queryLowered = (searchQuery ?? '').toString().toLowerCase()
const filteredSearchData = []
db.searchItems.forEach(item => {
if (item.children) {
const matchingChildren = item.children.filter(child => child.title.toLowerCase().includes(queryLowered))
if (matchingChildren.length > 0) {
const parentCopy = { ...item }
if (matchingChildren.length > 5)
parentCopy.children = matchingChildren.slice(0, 5)
else
parentCopy.children = matchingChildren
filteredSearchData.push(parentCopy)
}
}
})
// Enable this comment if you want to limit length of search results.
// if (filteredSearchData.length > 1) {
// filteredSearchData.forEach((item, index) => {
// if (item.children.length > 3)
// filteredSearchData[index].children.splice(0, 3)
// })
// }
return HttpResponse.json([...filteredSearchData], { status: 200 })
}),
]