-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
searchLocalStorageDataClasses #718
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ interface RawDataItem { | |
[key: string]: unknown | ||
} | ||
|
||
const recordKeys: Set<{ recordKey: string; className: string }> = new Set() | ||
|
||
export function provideLocalStorageDataClass( | ||
className: string, | ||
{ | ||
|
@@ -25,6 +27,7 @@ export function provideLocalStorageDataClass( | |
} = {}, | ||
) { | ||
const recordKey = `localDataClass-${scrivitoTenantId()}-${className}` | ||
recordKeys.add({ className, recordKey }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if (initialContent) initializeContent(initialContent) | ||
|
||
|
@@ -142,6 +145,34 @@ export function provideLocalStorageDataClass( | |
} | ||
} | ||
|
||
export function searchLocalStorageDataClasses( | ||
search: string, | ||
blackListEntities: string[] = [], | ||
): Array<{ _id: string; entity: string; title: string }> { | ||
const results: Array<{ _id: string; entity: string; title: string }> = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
return entities.flatMap((entity) =>
Object.entries(restoreRecord(recordKeyForClassName(entity)))
.filter(([, item]) => Object.values(item).some(matchesSearchTerm))
.map(([_id, item]) => ({
_id,
entity,
title: ensureString(item.title) || ensureString(item.keyword),
})),
) |
||
|
||
const lowerCaseSearchTerm = search.toLowerCase() | ||
const matchesSearchTerm = (value: unknown) => | ||
typeof value === 'string' && | ||
value.toLowerCase().includes(lowerCaseSearchTerm) | ||
|
||
recordKeys.forEach(({ className: entity, recordKey }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (blackListEntities.includes(entity)) return | ||
|
||
Object.entries(restoreRecord(recordKey)).forEach(([_id, item]) => { | ||
if (Object.values(item).some(matchesSearchTerm)) { | ||
results.push({ | ||
_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
entity, | ||
title: ensureString(item.title) || ensureString(item.keyword), | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
return results | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
function restoreRecord(recordKey: string): Record<string, RawDataItem> { | ||
let item: string | null | undefined | ||
try { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
recordKeys.add({ recordKey: "foo", className: "bar" })
will be added multiple times to the set. How about making it an Array instead?