From cd7434ea3bc0da6397188be33ee4c73c47f28992 Mon Sep 17 00:00:00 2001
From: Erik Michelson <github@erik.michelson.eu>
Date: Mon, 11 Nov 2024 17:27:12 +0100
Subject: [PATCH] chore(deps): upgrade orama to 3.0.1

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
---
 frontend/jest.config.ts                       |  4 +-
 frontend/package.json                         |  2 +-
 .../src/hooks/common/use-cheatsheet-search.ts | 44 +++++++------------
 yarn.lock                                     | 10 ++---
 4 files changed, 23 insertions(+), 37 deletions(-)

diff --git a/frontend/jest.config.ts b/frontend/jest.config.ts
index 3f1e85192..82572e4b6 100644
--- a/frontend/jest.config.ts
+++ b/frontend/jest.config.ts
@@ -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',
diff --git a/frontend/package.json b/frontend/package.json
index 6ebc86f96..d4022ba8a 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -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",
diff --git a/frontend/src/hooks/common/use-cheatsheet-search.ts b/frontend/src/hooks/common/use-cheatsheet-search.ts
index 9aa05e5b8..e05623b61 100644
--- a/frontend/src/hooks/common/use-cheatsheet-search.ts
+++ b/frontend/src/hooks/common/use-cheatsheet-search.ts
@@ -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
 }
diff --git a/yarn.lock b/yarn.lock
index 5f85febcb..e88230d2d 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -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