web/user: rework search (#5107)
closes #5106 Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
parent
bb2e5b4861
commit
b9754f9c13
|
@ -11,7 +11,7 @@ import Fuse from "fuse.js";
|
||||||
import { t } from "@lingui/macro";
|
import { t } from "@lingui/macro";
|
||||||
|
|
||||||
import { CSSResult, TemplateResult, css, html } from "lit";
|
import { CSSResult, TemplateResult, css, html } from "lit";
|
||||||
import { customElement, property } from "lit/decorators.js";
|
import { customElement, property, state } from "lit/decorators.js";
|
||||||
import { ifDefined } from "lit/directives/if-defined.js";
|
import { ifDefined } from "lit/directives/if-defined.js";
|
||||||
|
|
||||||
import PFContent from "@patternfly/patternfly/components/Content/content.css";
|
import PFContent from "@patternfly/patternfly/components/Content/content.css";
|
||||||
|
@ -36,25 +36,43 @@ export class LibraryPage extends AKElement {
|
||||||
@property({ attribute: false })
|
@property({ attribute: false })
|
||||||
apps?: PaginatedResponse<Application>;
|
apps?: PaginatedResponse<Application>;
|
||||||
|
|
||||||
@property({ attribute: false })
|
@state()
|
||||||
selectedApp?: Application;
|
selectedApp?: Application;
|
||||||
|
|
||||||
|
@state()
|
||||||
|
filteredApps: Application[] = [];
|
||||||
|
|
||||||
@property()
|
@property()
|
||||||
query = getURLParam<string | undefined>("search", undefined);
|
query = getURLParam<string | undefined>("search", undefined);
|
||||||
|
|
||||||
fuse?: Fuse<Application>;
|
fuse: Fuse<Application>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
this.fuse = new Fuse([], {
|
||||||
|
keys: [
|
||||||
|
{ name: "name", weight: 3 },
|
||||||
|
"slug",
|
||||||
|
"group",
|
||||||
|
{ name: "metaDescription", weight: 0.5 },
|
||||||
|
{ name: "metaPublisher", weight: 0.5 },
|
||||||
|
],
|
||||||
|
findAllMatches: true,
|
||||||
|
includeScore: true,
|
||||||
|
shouldSort: true,
|
||||||
|
ignoreFieldNorm: true,
|
||||||
|
useExtendedSearch: true,
|
||||||
|
threshold: 0.5,
|
||||||
|
});
|
||||||
new CoreApi(DEFAULT_CONFIG).coreApplicationsList({}).then((apps) => {
|
new CoreApi(DEFAULT_CONFIG).coreApplicationsList({}).then((apps) => {
|
||||||
this.apps = apps;
|
this.apps = apps;
|
||||||
this.fuse = new Fuse(apps.results, {
|
this.filteredApps = apps.results;
|
||||||
keys: ["slug", "name", "metaDescription", "metaPublisher", "group"],
|
this.fuse.setCollection(apps.results);
|
||||||
});
|
if (!this.query) return;
|
||||||
if (!this.fuse || !this.query) return;
|
|
||||||
const matchingApps = this.fuse.search(this.query);
|
const matchingApps = this.fuse.search(this.query);
|
||||||
if (matchingApps.length < 1) return;
|
if (matchingApps.length < 1) return;
|
||||||
this.selectedApp = matchingApps[0].item;
|
this.selectedApp = matchingApps[0].item;
|
||||||
|
this.filteredApps = matchingApps.map((a) => a.item);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,9 +128,8 @@ export class LibraryPage extends AKElement {
|
||||||
</div>`;
|
</div>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
filterApps(): Application[] {
|
filterApps(apps: Application[]): Application[] {
|
||||||
return (
|
return apps.filter((app) => {
|
||||||
this.apps?.results.filter((app) => {
|
|
||||||
if (app.launchUrl && app.launchUrl !== "") {
|
if (app.launchUrl && app.launchUrl !== "") {
|
||||||
// If the launch URL is a full URL, only show with http or https
|
// If the launch URL is a full URL, only show with http or https
|
||||||
if (app.launchUrl.indexOf("://") !== -1) {
|
if (app.launchUrl.indexOf("://") !== -1) {
|
||||||
|
@ -122,12 +139,11 @@ export class LibraryPage extends AKElement {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}) || []
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getApps(): [string, Application[]][] {
|
getApps(): [string, Application[]][] {
|
||||||
return groupBy(this.filterApps(), (app) => app.group || "");
|
return groupBy(this.filterApps(this.filteredApps), (app) => app.group || "");
|
||||||
}
|
}
|
||||||
|
|
||||||
renderApps(): TemplateResult {
|
renderApps(): TemplateResult {
|
||||||
|
@ -172,6 +188,19 @@ export class LibraryPage extends AKElement {
|
||||||
</div>`;
|
</div>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetSearch(): void {
|
||||||
|
const searchInput = this.shadowRoot?.querySelector("input");
|
||||||
|
if (searchInput) {
|
||||||
|
searchInput.value = "";
|
||||||
|
}
|
||||||
|
this.query = "";
|
||||||
|
updateURLParams({
|
||||||
|
search: this.query,
|
||||||
|
});
|
||||||
|
this.selectedApp = undefined;
|
||||||
|
this.filteredApps = this.apps?.results || [];
|
||||||
|
}
|
||||||
|
|
||||||
render(): TemplateResult {
|
render(): TemplateResult {
|
||||||
return html`<main role="main" class="pf-c-page__main" tabindex="-1" id="main-content">
|
return html`<main role="main" class="pf-c-page__main" tabindex="-1" id="main-content">
|
||||||
<div class="pf-c-content header">
|
<div class="pf-c-content header">
|
||||||
|
@ -180,37 +209,36 @@ export class LibraryPage extends AKElement {
|
||||||
? html`<input
|
? html`<input
|
||||||
@input=${(ev: InputEvent) => {
|
@input=${(ev: InputEvent) => {
|
||||||
this.query = (ev.target as HTMLInputElement).value;
|
this.query = (ev.target as HTMLInputElement).value;
|
||||||
|
if (this.query === "") {
|
||||||
|
return this.resetSearch();
|
||||||
|
}
|
||||||
updateURLParams({
|
updateURLParams({
|
||||||
search: this.query,
|
search: this.query,
|
||||||
});
|
});
|
||||||
if (!this.fuse) return;
|
|
||||||
const apps = this.fuse.search(this.query);
|
const apps = this.fuse.search(this.query);
|
||||||
if (apps.length < 1) return;
|
if (apps.length < 1) return;
|
||||||
this.selectedApp = apps[0].item;
|
this.selectedApp = apps[0].item;
|
||||||
|
this.filteredApps = apps.map((a) => a.item);
|
||||||
}}
|
}}
|
||||||
@keydown=${(ev: KeyboardEvent) => {
|
@keydown=${(ev: KeyboardEvent) => {
|
||||||
if (ev.key === "Enter" && this.selectedApp?.launchUrl) {
|
if (ev.key === "Enter" && this.selectedApp?.launchUrl) {
|
||||||
window.location.assign(this.selectedApp.launchUrl);
|
window.location.assign(this.selectedApp.launchUrl);
|
||||||
} else if (ev.key === "Escape") {
|
} else if (ev.key === "Escape") {
|
||||||
(ev.target as HTMLInputElement).value = "";
|
this.resetSearch();
|
||||||
this.query = "";
|
|
||||||
updateURLParams({
|
|
||||||
search: this.query,
|
|
||||||
});
|
|
||||||
this.selectedApp = undefined;
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
type="text"
|
type="text"
|
||||||
class="pf-u-display-none pf-u-display-block-on-md"
|
class="pf-u-display-none pf-u-display-block-on-md"
|
||||||
autofocus
|
autofocus
|
||||||
placeholder=${t`Search...`}
|
placeholder=${t`Search...`}
|
||||||
|
value=${ifDefined(this.query)}
|
||||||
/>`
|
/>`
|
||||||
: html``}
|
: html``}
|
||||||
</div>
|
</div>
|
||||||
<section class="pf-c-page__main-section">
|
<section class="pf-c-page__main-section">
|
||||||
${loading(
|
${loading(
|
||||||
this.apps,
|
this.apps,
|
||||||
html`${this.filterApps().length > 0
|
html`${this.filteredApps.length > 0
|
||||||
? this.renderApps()
|
? this.renderApps()
|
||||||
: this.renderEmptyState()}`,
|
: this.renderEmptyState()}`,
|
||||||
)}
|
)}
|
||||||
|
|
Reference in a new issue