chore(deps): upgrade orama to 3.0.1

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2024-11-11 17:27:12 +01:00
parent 9cd6003619
commit cd7434ea3b
4 changed files with 23 additions and 37 deletions

View file

@ -16,8 +16,8 @@ const customJestConfig = {
moduleNameMapper: {
// Handle module aliases (this will be automatically configured for you soon)
'^@/components/(.*)$': '<rootDir>/src/components/$1',
// fix uuid / jest problem https://github.com/uuidjs/uuid/pull/616
'^uuid$': require.resolve('uuid')
// fix ESM loading of orama breaking jest
'^@orama/orama$': require.resolve('@orama/orama')
},
roots: ['<rootDir>/src'],
testEnvironment: 'jsdom',

View file

@ -47,7 +47,7 @@
"@hedgedoc/html-to-react": "workspace:html-to-react",
"@hedgedoc/markdown-it-plugins": "workspace:markdown-it-plugins",
"@mrdrogdrog/optional": "1.2.1",
"@orama/orama": "2.0.23",
"@orama/orama": "3.0.1",
"@react-hook/resize-observer": "2.0.2",
"@redux-devtools/core": "4.0.0",
"@reduxjs/toolkit": "2.3.0",

View file

@ -4,10 +4,9 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useEffect, useState } from 'react'
import { create, insert, search } from '@orama/orama'
import { useAsync } from 'react-use'
import { Logger } from '../../utils/logger'
import { useEffect, useMemo, useState } from 'react'
import type { Orama, Results } from '@orama/orama'
import { create, insertMultiple, search } from '@orama/orama'
export interface CheatsheetSearchIndexEntry {
readonly id: string
@ -17,8 +16,6 @@ export interface CheatsheetSearchIndexEntry {
readonly extensionId: string
}
const logger = new Logger('Cheatsheet Search')
/**
* Generate document search index and provide functions to search.
*
@ -32,12 +29,8 @@ export const useCheatsheetSearch = (
): CheatsheetSearchIndexEntry[] => {
const [results, setResults] = useState<CheatsheetSearchIndexEntry[]>([])
const {
value: searchIndex,
loading: searchIndexLoading,
error: searchIndexError
} = useAsync(async () => {
const db = await create({
const searchIndex = useMemo(() => {
const db = create({
schema: {
id: 'string',
title: 'string',
@ -46,19 +39,15 @@ export const useCheatsheetSearch = (
extensionId: 'string'
} as const
})
const adds = entries.map((entry) => {
logger.debug('Add to search entry:', entry)
return insert(db, entry)
})
await Promise.all(adds)
return db
void insertMultiple(db, entries)
return db as Orama<CheatsheetSearchIndexEntry>
}, [entries])
useEffect(() => {
if (searchIndexLoading || searchIndexError !== undefined || searchIndex === undefined || searchTerm === '') {
if (searchIndex === undefined || searchTerm === '') {
return setResults(entries)
}
search(searchIndex, {
const rawResults = search(searchIndex, {
term: searchTerm,
tolerance: 1,
properties: ['title', 'description', 'example'],
@ -66,15 +55,12 @@ export const useCheatsheetSearch = (
title: 3,
description: 2,
example: 1
}
})
.then((results) => {
setResults(results.hits.map((entry) => entry.document))
})
.catch((error) => {
logger.error(error)
})
}, [entries, searchIndexError, searchIndexLoading, searchIndex, searchTerm])
},
limit: entries.length
}) as Results<CheatsheetSearchIndexEntry>
const results = rawResults.hits.map((entry) => entry.document)
setResults(results)
}, [entries, searchIndex, searchTerm])
return results
}

View file

@ -2703,7 +2703,7 @@ __metadata:
"@hedgedoc/markdown-it-plugins": "workspace:markdown-it-plugins"
"@mrdrogdrog/optional": "npm:1.2.1"
"@next/bundle-analyzer": "npm:14.2.7"
"@orama/orama": "npm:2.0.23"
"@orama/orama": "npm:3.0.1"
"@react-hook/resize-observer": "npm:2.0.2"
"@redux-devtools/core": "npm:4.0.0"
"@reduxjs/toolkit": "npm:2.3.0"
@ -4372,10 +4372,10 @@ __metadata:
languageName: node
linkType: hard
"@orama/orama@npm:2.0.23":
version: 2.0.23
resolution: "@orama/orama@npm:2.0.23"
checksum: 10c0/b65f57e338c5cfe6498cec5917a952ce783aef202f20ef27d8f55f40e175737a616b09ccbb7ebedb3cd24250504a2295caa950c8777cbc21309ee430584c1ed3
"@orama/orama@npm:3.0.1":
version: 3.0.1
resolution: "@orama/orama@npm:3.0.1"
checksum: 10c0/ef344c95c62be21b7a4ef745db8bb2d2f4da00db4ee8f382a8641de13759457f6c4a21ab3602dbda22b610459e163fa47ff95d97bf5a1bad4b60a760e6bee07c
languageName: node
linkType: hard