Skip to content

Commit

Permalink
v
Browse files Browse the repository at this point in the history
  • Loading branch information
julianbenegas committed May 15, 2024
1 parent 364ff62 commit defbe89
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-dancers-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"basehub": patch
---

Make recent searches keys just use the document id
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"playground": "0.0.85"
},
"changesets": [
"afraid-dancers-hang",
"blue-files-thank",
"brown-mayflies-enjoy",
"calm-cobras-glow",
Expand Down
6 changes: 6 additions & 0 deletions packages/basehub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# basehub

## 4.0.16-canary.15

### Patch Changes

- Make recent searches keys just use the document id

## 4.0.16-canary.14

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "basehub",
"description": "The first AI-native content hub.",
"author": "JB <[email protected]>",
"version": "4.0.16-canary.14",
"version": "4.0.16-canary.15",
"license": "MIT",
"repository": "basehub-ai/basehub",
"bugs": "https://github.com/basehub-ai/basehub/issues",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// A fast and simple 64-bit (or 53-bit) string hash function with decent collision resistance.
// Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
// See https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript/52171480#52171480

import type { SearchResponseHit } from "typesense/lib/Typesense/Documents";
import type { BaseDoc } from "./primitive";

// https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
const cyrb64 = (str: string, seed = 0) => {
let h1 = 0xdeadbeef ^ seed,
Expand All @@ -30,3 +34,23 @@ export const cyrb64Hash = (str: string, seed = 0) => {
const [h2, h1] = cyrb64(str, seed);
return h2.toString(36).padStart(7, "0") + h1.toString(36).padStart(7, "0");
};

export function getHitKey(hit: SearchResponseHit<object>): string {
return cyrb64Hash(
JSON.stringify({
_id: (hit.document as BaseDoc)._id,
curated: hit.curated,
highlight: hit.highlight,
highlights: hit.highlights,
text_match: hit.text_match,
text_match_info: hit.text_match_info,
})
);
}

export function getHitRecentSearchKey(hit: { document: BaseDoc }): string {
/**
* We'll just use the document ID as the recent search key so that you can't save a document multiple times into recent searches.
*/
return `recent-search-${hit.document._id}`;
}
11 changes: 10 additions & 1 deletion packages/basehub/src/react/search/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export * from "./primitive";
export { SearchBox, getSearchClient, useSearch } from "./primitive";
export type {
Highlight,
Hit,
SearchBoxContext,
SearchOptions,
SearchResult,
UseSearchParams,
UseSearchResult,
} from "./primitive";
22 changes: 8 additions & 14 deletions packages/basehub/src/react/search/primitive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Client } from "typesense";
import type { SearchParams } from "typesense/lib/Typesense/Documents";
import get from "lodash.get";
import { Slot } from "@radix-ui/react-slot";
import { cyrb64Hash } from "./hash";
import { getHitKey, getHitRecentSearchKey } from "./helpers";

/* -------------------------------------------------------------------------------------------------
* Utils
Expand Down Expand Up @@ -129,7 +129,7 @@ export type SearchOptions = {
queryByWeights?: SearchParams["query_by_weights"];
};

type BaseDoc = {
export type BaseDoc = {
_id: string;
_idPath: string;
_title?: string;
Expand Down Expand Up @@ -269,16 +269,7 @@ export const useSearch = <
return cast;
}) ?? [];

const _key = cyrb64Hash(
JSON.stringify({
_id: document._id,
curated: hit.curated,
highlight: hit.highlight,
highlights: hit.highlights,
text_match: hit.text_match,
text_match_info: hit.text_match_info,
})
);
const _key = getHitKey(hit);

return {
_key,
Expand Down Expand Up @@ -326,14 +317,17 @@ export const useSearch = <
}
const storage = getRecentSearchesStorageRef.current();

const _key = getHitRecentSearchKey(hit);
const updatedHit = { ...hit, _key };

setRecentSearchesHits((prev) => {
// check if this hit already exists
if (prev) {
const exists = prev.some((h) => h._key === hit._key);
const exists = prev.some((h) => h._key === updatedHit._key);
if (exists) return prev;
}

const next = prev ? [hit, ...prev] : [hit];
const next = prev ? [updatedHit, ...prev] : [hit];
storage.setItem(storageKey, JSON.stringify(next));
return next;
});
Expand Down
7 changes: 7 additions & 0 deletions playground/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# playground

## 0.0.86-canary.15

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.86-canary.14

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "playground",
"private": true,
"version": "0.0.86-canary.14",
"version": "0.0.86-canary.15",
"scripts": {
"dev": "basehub dev & next dev",
"build": "next build",
Expand Down

0 comments on commit defbe89

Please sign in to comment.