-
-
Notifications
You must be signed in to change notification settings - Fork 59
devex: Refactor components to use org search context #2880
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
13d6aa8
add form section
SuaYoo 5b37fb4
wip field
SuaYoo e2761aa
reduce calls to search values
SuaYoo cb6dccb
generalize org search
SuaYoo 8d233fb
revert field changes
SuaYoo 904340b
revert combobox change
SuaYoo 0310244
revert crawler type change
SuaYoo 6a154f1
remove min search matches
SuaYoo 4ebc5eb
remove unused task
SuaYoo 077bb97
limit search results
SuaYoo 02bac8d
limit combobox results
SuaYoo bf56125
manually filter out quoted names
SuaYoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
frontend/src/context/search-org/SearchOrgContextController.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { ContextProvider } from "@lit/context"; | ||
| import { Task } from "@lit/task"; | ||
| import { type ReactiveController } from "lit"; | ||
|
|
||
| import { connectFuse } from "./connectFuse"; | ||
| import { | ||
| searchOrgContext, | ||
| searchOrgInitialValue, | ||
| type SearchOrgContext, | ||
| } from "./search-org"; | ||
| import { type SearchOrgKey, type SearchQuery } from "./types"; | ||
|
|
||
| import type { BtrixElement } from "@/classes/BtrixElement"; | ||
| import type { CollectionSearchValues } from "@/types/collection"; | ||
|
|
||
| /** | ||
| * Provides org-wide search data to all descendents of a component. | ||
| * | ||
| * @example Usage: | ||
| * ```ts | ||
| * class Component extends BtrixElement { | ||
| * readonly [searchOrgContextKey] = new SearchOrgContextController(this); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export class SearchOrgContextController implements ReactiveController { | ||
| readonly #host: BtrixElement; | ||
| readonly #context: ContextProvider<{ __context__: SearchOrgContext }>; | ||
| readonly #tasks = new Map<SearchOrgKey, Task>(); | ||
|
|
||
| constructor(host: BtrixElement) { | ||
| this.#host = host; | ||
| this.#context = new ContextProvider(this.#host, { | ||
| context: searchOrgContext, | ||
| initialValue: searchOrgInitialValue, | ||
| }); | ||
|
|
||
| this.addTask("collections", this.getCollectionsSearchValues); | ||
|
|
||
| host.addController(this); | ||
| } | ||
|
|
||
| hostConnected(): void {} | ||
| hostDisconnected(): void {} | ||
|
|
||
| public async refresh(key?: SearchOrgKey) { | ||
| if (key) { | ||
| void this.#tasks.get(key)?.run(); | ||
| } else { | ||
| for (const [_key, task] of this.#tasks) { | ||
| void task.run(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private addTask( | ||
| key: SearchOrgKey, | ||
| request: (orgId: string, signal: AbortSignal) => Promise<SearchQuery[]>, | ||
| ) { | ||
| this.#tasks.set( | ||
| key, | ||
| new Task(this.#host, { | ||
| task: async ([orgId], { signal }) => { | ||
| if (!orgId) return null; | ||
|
|
||
| const values = await request(orgId, signal); | ||
|
|
||
| if (signal.aborted) return; | ||
|
|
||
| this.#context.setValue({ | ||
| ...this.#context.value, | ||
| [key]: connectFuse(values), | ||
| }); | ||
| }, | ||
| args: () => [this.#host.appState.orgId] as const, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| private readonly getCollectionsSearchValues = async ( | ||
| orgId: string, | ||
| signal: AbortSignal, | ||
| ) => { | ||
| try { | ||
| const { names } = await this.#host.api.fetch<CollectionSearchValues>( | ||
| `/orgs/${orgId}/collections/search-values`, | ||
| { signal }, | ||
| ); | ||
|
|
||
| return names.map((name) => ({ name })); | ||
| } catch (err) { | ||
| console.debug(err); | ||
| } | ||
|
|
||
| return []; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { ContextConsumer } from "@lit/context"; | ||
| import type { LitElement } from "lit"; | ||
| import type { Constructor } from "type-fest"; | ||
|
|
||
| import { searchOrgContext, searchOrgInitialValue } from "./search-org"; | ||
| import type { SearchOrgKey } from "./types"; | ||
|
|
||
| /** | ||
| * Consume search data. | ||
| * | ||
| * @example Usage: | ||
| * ```ts | ||
| * class Component extends WithSearchOrgContext(BtrixElement) {} | ||
| * ``` | ||
| */ | ||
| export const WithSearchOrgContext = <T extends Constructor<LitElement>>( | ||
| superClass: T, | ||
| ) => | ||
| class extends superClass { | ||
| readonly #searchOrg = new ContextConsumer(this, { | ||
| context: searchOrgContext, | ||
| subscribe: true, | ||
| }); | ||
emma-sg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public get searchOrg() { | ||
| return this.#searchOrg.value || searchOrgInitialValue; | ||
| } | ||
|
|
||
| public listSearchValuesFor(key: SearchOrgKey) { | ||
| return this.searchOrg[key]?.getIndex().toJSON().records || null; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * Enable fuzzy search on available values. | ||
| */ | ||
| import Fuse from "fuse.js"; | ||
|
|
||
| import { searchQueryKeys, type SearchQuery } from "./types"; | ||
|
|
||
| export function connectFuse(values: SearchQuery[]) { | ||
| return new Fuse(values, { | ||
| keys: searchQueryKeys, | ||
| threshold: 0.3, | ||
| useExtendedSearch: true, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { searchOrgContext, type SearchOrgContext } from "./search-org"; | ||
|
|
||
| export type { SearchOrgContext }; | ||
|
|
||
| export default searchOrgContext; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * Store org-wide searchable data, like collection names. | ||
| */ | ||
| import { createContext } from "@lit/context"; | ||
| import type Fuse from "fuse.js"; | ||
|
|
||
| import { | ||
| searchOrgContextKey, | ||
| type SearchOrgKey, | ||
| type SearchQuery, | ||
| } from "./types"; | ||
|
|
||
| export type SearchOrgContext = Record<SearchOrgKey, Fuse<SearchQuery> | null>; | ||
|
|
||
| export const searchOrgInitialValue = { | ||
| collections: null, | ||
| } as const satisfies SearchOrgContext; | ||
|
|
||
| export const searchOrgContext = | ||
| createContext<SearchOrgContext>(searchOrgContextKey); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export const searchQueryKeys = ["name"]; | ||
emma-sg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export const searchOrgContextKey = Symbol("search-values"); | ||
| export type SearchQuery = Record<(typeof searchQueryKeys)[number], string>; | ||
| export type SearchOrgKey = "collections"; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.