|
1 | 1 | import { LitElement, css, html } from "lit";
|
2 |
| -import { customElement } from "lit/decorators.js"; |
| 2 | +import { customElement, property, state } from "lit/decorators.js"; |
| 3 | +import "@shoelace-style/shoelace/dist/components/popup/popup"; |
| 4 | +import { Library, type Album, type Artist } from "@echo/core-types"; |
| 5 | +import { EffectFn } from "@echo/components-shared-controllers/src/effect-fn.controller"; |
| 6 | +import { Option } from "effect"; |
3 | 7 |
|
4 | 8 | /**
|
5 | 9 | * Component that displays a search bar that can search in the user's library
|
6 | 10 | * and execute commands.
|
7 | 11 | */
|
8 | 12 | @customElement("command-bar")
|
9 | 13 | export class CommandBar extends LitElement {
|
| 14 | + @state() |
| 15 | + private resultsVisible = false; |
| 16 | + |
| 17 | + @state() |
| 18 | + private searchResults: [Album[], Artist[]] = [[], []]; |
| 19 | + |
| 20 | + private previousSearchTimeout: NodeJS.Timeout | undefined; |
| 21 | + |
| 22 | + private search = new EffectFn(this, Library.search, { |
| 23 | + complete: (results) => { |
| 24 | + this.searchResults = results; |
| 25 | + this.resultsVisible = true; |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
10 | 29 | static styles = css`
|
11 | 30 | input {
|
12 | 31 | padding: 0.5rem;
|
13 | 32 | border: 1px solid var(--border-color);
|
14 | 33 | background-color: var(--background-color-muted);
|
| 34 | + color: var(--text-color); |
| 35 | + font-family: "DepartureMono", monospace; |
15 | 36 | font-size: 1rem;
|
| 37 | + outline: none; |
16 | 38 | width: 95%;
|
17 | 39 | }
|
18 | 40 |
|
19 |
| - input::placeholder { |
20 |
| - font-family: "DepartureMono", monospace; |
| 41 | + input:focus { |
| 42 | + border-color: var(--accent-color); |
| 43 | + } |
| 44 | +
|
| 45 | + div.search-results { |
| 46 | + background-color: var(--background-color-muted); |
| 47 | + } |
| 48 | + `; |
| 49 | + |
| 50 | + connectedCallback(): void { |
| 51 | + super.connectedCallback(); |
| 52 | + |
| 53 | + // Listen for the Escape key to close the search bar. |
| 54 | + window.addEventListener("keydown", (event) => this._onKeyDown(event)); |
| 55 | + window.addEventListener("mousedown", (event) => this._onMouseDown(event)); |
| 56 | + } |
| 57 | + |
| 58 | + render() { |
| 59 | + return html` |
| 60 | + <sl-popup ?active=${this.resultsVisible} placement="bottom" sync="width"> |
| 61 | + <input |
| 62 | + slot="anchor" |
| 63 | + placeholder="Search or command" |
| 64 | + @input="${this._onQueryChanged}" |
| 65 | + @focus="${() => (this.resultsVisible = true)}" |
| 66 | + /> |
| 67 | +
|
| 68 | + <div class="search-results"> |
| 69 | + ${this.searchResults[0].map( |
| 70 | + (album) => html` |
| 71 | + <command-bar-result |
| 72 | + title="${album.name}" |
| 73 | + subtitle="${album.artist.name}" |
| 74 | + .imageSource="${album.embeddedCover}" |
| 75 | + link="/albums/${album.id}" |
| 76 | + @click="${this._onOptionSelected}" |
| 77 | + ></command-bar-result> |
| 78 | + `, |
| 79 | + )} |
| 80 | + ${this.searchResults[1].map( |
| 81 | + (artist) => html` |
| 82 | + <command-bar-result |
| 83 | + title="${artist.name}" |
| 84 | + subtitle="" |
| 85 | + .imageSource="${artist.image}" |
| 86 | + rounded |
| 87 | + link="/artists/${artist.id}" |
| 88 | + @click="${this._onOptionSelected}" |
| 89 | + ></command-bar-result> |
| 90 | + `, |
| 91 | + )} |
| 92 | + </div> |
| 93 | + </sl-popup> |
| 94 | + `; |
| 95 | + } |
| 96 | + |
| 97 | + private _onQueryChanged(event: Event) { |
| 98 | + const query = (event.target as HTMLInputElement).value; |
| 99 | + |
| 100 | + if (this.previousSearchTimeout) { |
| 101 | + clearTimeout(this.previousSearchTimeout); |
| 102 | + } |
| 103 | + |
| 104 | + this.previousSearchTimeout = setTimeout(() => { |
| 105 | + this.search.run(query); |
| 106 | + }, 200); |
| 107 | + } |
| 108 | + |
| 109 | + private _onKeyDown(event: KeyboardEvent) { |
| 110 | + if (event.key === "Escape") { |
| 111 | + this.resultsVisible = false; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private _onMouseDown(event: MouseEvent) { |
| 116 | + const elementPath = event.composedPath(); |
| 117 | + const popup = this.shadowRoot?.querySelector("sl-popup") as HTMLElement; |
| 118 | + |
| 119 | + // Close the search results if the user clicks outside of the popup. |
| 120 | + // `composedPath` returns a list of all elements that the event will pass |
| 121 | + // through, so if the popup is not in the path, the user clicked outside. |
| 122 | + if (!elementPath.includes(popup)) { |
| 123 | + this.resultsVisible = false; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private _onOptionSelected() { |
| 128 | + this.resultsVisible = false; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +@customElement("command-bar-result") |
| 133 | +class CommandBarResult extends LitElement { |
| 134 | + @property({ type: String }) |
| 135 | + title = ""; |
| 136 | + |
| 137 | + @property({ type: String }) |
| 138 | + subtitle = ""; |
| 139 | + |
| 140 | + @property({ type: Object }) |
| 141 | + imageSource: Option.Option<Blob> = Option.none(); |
| 142 | + |
| 143 | + @property({ type: String }) |
| 144 | + link = ""; |
| 145 | + |
| 146 | + @property({ type: Boolean }) |
| 147 | + rounded = false; |
| 148 | + |
| 149 | + static styles = css` |
| 150 | + a { |
| 151 | + display: flex; |
| 152 | + align-items: center; |
| 153 | + cursor: pointer; |
| 154 | + gap: 1rem; |
| 155 | + text-decoration: none; |
| 156 | + color: inherit; |
| 157 | + padding: 0.5rem; |
| 158 | + width: 100%; |
| 159 | + } |
| 160 | +
|
| 161 | + a:hover { |
| 162 | + background-color: var(--background-color); |
| 163 | + } |
| 164 | +
|
| 165 | + img { |
| 166 | + width: 3rem; |
| 167 | + height: 3rem; |
| 168 | + border-radius: 0.5rem; |
| 169 | + } |
| 170 | +
|
| 171 | + img.rounded { |
| 172 | + border-radius: 50%; |
| 173 | + } |
| 174 | +
|
| 175 | + .info { |
| 176 | + display: flex; |
| 177 | + flex-direction: column; |
| 178 | + } |
| 179 | +
|
| 180 | + .info > * { |
| 181 | + margin: 0; |
21 | 182 | }
|
22 | 183 | `;
|
23 | 184 |
|
24 | 185 | render() {
|
25 |
| - return html`<input placeholder="Search or command (f)" />`; |
| 186 | + return html` |
| 187 | + <a href=${this.link}> |
| 188 | + ${Option.isSome(this.imageSource) && |
| 189 | + html` |
| 190 | + <img |
| 191 | + class="${this.rounded ? "rounded" : ""}" |
| 192 | + src="${URL.createObjectURL(this.imageSource.value)}" |
| 193 | + alt="${this.title}" |
| 194 | + /> |
| 195 | + `} |
| 196 | + <div class="info"> |
| 197 | + <h4>${this.title}</h4> |
| 198 | + <p>${this.subtitle}</p> |
| 199 | + </div> |
| 200 | + </a> |
| 201 | + `; |
26 | 202 | }
|
27 | 203 | }
|
28 | 204 |
|
29 | 205 | declare global {
|
30 | 206 | interface HTMLElementTagNameMap {
|
31 | 207 | "command-bar": CommandBar;
|
| 208 | + "command-bar-result": CommandBarResult; |
32 | 209 | }
|
33 | 210 | }
|
0 commit comments