Skip to content

Add fuse-backed org search to superadmin org list #2277

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 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions frontend/src/components/orgs-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type {
SlMenuItem,
} from "@shoelace-style/shoelace";
import { serialize } from "@shoelace-style/shoelace/dist/utilities/form.js";
import { css, html, nothing } from "lit";
import Fuse from "fuse.js";
import { css, html, nothing, type PropertyValues } from "lit";
import { customElement, property, query, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";

Expand Down Expand Up @@ -56,12 +57,62 @@ export class OrgsList extends BtrixElement {
@query("#orgDeleteButton")
private readonly orgDeleteButton?: SlButton | null;

// For fuzzy search:
private readonly fuse = new Fuse(this.orgList ?? [], {
keys: [
"id",
"name",
"slug",
"users.name",
"users.email",
"subscription.subId",
"subscription.planId",
],
useExtendedSearch: true,
});

@state()
private search = "";

protected willUpdate(changedProperties: PropertyValues<this>) {
if (changedProperties.has("orgList")) {
this.fuse.setCollection(this.orgList ?? []);
}
}

protected firstUpdated() {
this.fuse.setCollection(this.orgList ?? []);
}

render() {
if (this.skeleton) {
return this.renderSkeleton();
}

const orgs = this.search
? this.fuse.search(this.search).map(({ item }) => item)
: this.orgList;

return html`
<sl-input
value=${this.search}
clearable
size="small"
class="mb-6"
placeholder=${msg(
"Search all orgs by name, id, slug, users, and subscriptions",
)}
@sl-input=${(e: Event) => {
this.search = (e.target as SlInput).value.trim() || "";
}}
>
<sl-icon
name="search"
slot="prefix"
aria-hidden="true"
library="default"
></sl-icon
></sl-input>
<btrix-table>
<btrix-table-head class="mb-2">
<btrix-table-header-cell>
Expand All @@ -84,7 +135,7 @@ export class OrgsList extends BtrixElement {
</btrix-table-header-cell>
</btrix-table-head>
<btrix-table-body class="rounded border">
${this.orgList?.map(this.renderOrg)}
${orgs?.map(this.renderOrg)}
</btrix-table-body>
</btrix-table>

Expand Down
Loading