-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Refactor sequence matching to accept item rather than word array #8866
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
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 |
|---|---|---|
|
|
@@ -66,13 +66,18 @@ interface CommandItem extends QuickBarItem { | |
| } | ||
|
|
||
| interface EntityItem extends QuickBarItem { | ||
| altText: string; | ||
| icon?: string; | ||
| } | ||
|
|
||
| const isCommandItem = (item: EntityItem | CommandItem): item is CommandItem => { | ||
| const isCommandItem = (item: QuickBarItem): item is CommandItem => { | ||
| return (item as CommandItem).categoryKey !== undefined; | ||
| }; | ||
|
|
||
| const isEntityItem = (item: QuickBarItem): item is EntityItem => { | ||
| return !isCommandItem(item); | ||
| }; | ||
|
|
||
| interface QuickBarNavigationItem extends CommandItem { | ||
| path: string; | ||
| } | ||
|
|
@@ -228,9 +233,15 @@ export class QuickBar extends LitElement { | |
| } | ||
|
|
||
| private _renderItem(item: QuickBarItem, index?: number) { | ||
| return isCommandItem(item) | ||
| ? this._renderCommandItem(item, index) | ||
| : this._renderEntityItem(item, index); | ||
| if (isCommandItem(item)) { | ||
| return this._renderCommandItem(item, index); | ||
| } | ||
|
|
||
| if (isEntityItem(item)) { | ||
|
Member
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. This is just... we check if it is not a command item, we already know that, because we checked that above...?
Contributor
Author
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.
Member
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.
Contributor
Author
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. Could do, but I try to avoid
Member
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. yes, but I rather do that than to add unneeded runtime checks 😄
Contributor
Author
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. Okie doke
Contributor
Author
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. Fixed |
||
| return this._renderEntityItem(item, index); | ||
| } | ||
|
|
||
| return html``; | ||
| } | ||
|
|
||
| private _renderEntityItem(item: EntityItem, index?: number) { | ||
|
|
@@ -289,13 +300,6 @@ export class QuickBar extends LitElement { | |
| </span> | ||
|
|
||
| <span class="command-text">${item.primaryText}</span> | ||
| ${item.altText | ||
| ? html` | ||
| <span slot="secondary" class="item-text secondary" | ||
| >${item.altText}</span | ||
| > | ||
| ` | ||
| : null} | ||
| </mwc-list-item> | ||
| `; | ||
| } | ||
|
|
@@ -389,17 +393,20 @@ export class QuickBar extends LitElement { | |
| } | ||
| } | ||
|
|
||
| private _generateEntityItems(): QuickBarItem[] { | ||
| private _generateEntityItems(): EntityItem[] { | ||
| return Object.keys(this.hass.states) | ||
| .map((entityId) => { | ||
| const primaryText = computeStateName(this.hass.states[entityId]); | ||
| return { | ||
| primaryText, | ||
| filterText: primaryText, | ||
| const entityItem = { | ||
| primaryText: computeStateName(this.hass.states[entityId]), | ||
| altText: entityId, | ||
| icon: domainIcon(computeDomain(entityId), this.hass.states[entityId]), | ||
| action: () => fireEvent(this, "hass-more-info", { entityId }), | ||
| }; | ||
|
|
||
| return { | ||
| ...entityItem, | ||
| words: [entityItem.primaryText, entityItem.altText], | ||
| }; | ||
| }) | ||
| .sort((a, b) => | ||
| compare(a.primaryText.toLowerCase(), b.primaryText.toLowerCase()) | ||
|
|
@@ -412,32 +419,35 @@ export class QuickBar extends LitElement { | |
| ...this._generateServerControlCommands(), | ||
| ...this._generateNavigationCommands(), | ||
| ].sort((a, b) => | ||
| compare(a.filterText.toLowerCase(), b.filterText.toLowerCase()) | ||
| compare(a.words.join(" ").toLowerCase(), b.words.join(" ").toLowerCase()) | ||
| ); | ||
| } | ||
|
|
||
| private _generateReloadCommands(): CommandItem[] { | ||
| const reloadableDomains = componentsWithService(this.hass, "reload").sort(); | ||
|
|
||
| return reloadableDomains.map((domain) => { | ||
| const categoryText = this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.types.reload` | ||
| ); | ||
| const primaryText = | ||
| this.hass.localize(`ui.dialogs.quick-bar.commands.reload.${domain}`) || | ||
| this.hass.localize( | ||
| "ui.dialogs.quick-bar.commands.reload.reload", | ||
| "domain", | ||
| domainToName(this.hass.localize, domain) | ||
| ); | ||
| const commandItem = { | ||
| primaryText: | ||
| this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.reload.${domain}` | ||
| ) || | ||
| this.hass.localize( | ||
| "ui.dialogs.quick-bar.commands.reload.reload", | ||
| "domain", | ||
| domainToName(this.hass.localize, domain) | ||
| ), | ||
| action: () => this.hass.callService(domain, "reload"), | ||
| iconPath: mdiReload, | ||
| categoryText: this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.types.reload` | ||
| ), | ||
| }; | ||
|
|
||
| return { | ||
| primaryText, | ||
| filterText: `${categoryText} ${primaryText}`, | ||
| action: () => this.hass.callService(domain, "reload"), | ||
| ...commandItem, | ||
| categoryKey: "reload", | ||
| iconPath: mdiReload, | ||
| categoryText, | ||
| words: [`${commandItem.categoryText} ${commandItem.primaryText}`], | ||
|
bramkragten marked this conversation as resolved.
Outdated
|
||
| }; | ||
| }); | ||
| } | ||
|
|
@@ -446,26 +456,28 @@ export class QuickBar extends LitElement { | |
| const serverActions = ["restart", "stop"]; | ||
|
|
||
| return serverActions.map((action) => { | ||
| const categoryKey = "server_control"; | ||
| const categoryText = this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.types.${categoryKey}` | ||
| ); | ||
| const primaryText = this.hass.localize( | ||
| "ui.dialogs.quick-bar.commands.server_control.perform_action", | ||
| "action", | ||
| this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.server_control.${action}` | ||
| ) | ||
| ); | ||
| const categoryKey: CommandItem["categoryKey"] = "server_control"; | ||
|
|
||
| const item = { | ||
| primaryText: this.hass.localize( | ||
| "ui.dialogs.quick-bar.commands.server_control.perform_action", | ||
| "action", | ||
| this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.server_control.${action}` | ||
| ) | ||
| ), | ||
| iconPath: mdiServerNetwork, | ||
| categoryText: this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.types.${categoryKey}` | ||
| ), | ||
| categoryKey, | ||
| action: () => this.hass.callService("homeassistant", action), | ||
| }; | ||
|
|
||
| return this._generateConfirmationCommand( | ||
| { | ||
| primaryText, | ||
| filterText: `${categoryText} ${primaryText}`, | ||
| categoryKey, | ||
| iconPath: mdiServerNetwork, | ||
| categoryText, | ||
| action: () => this.hass.callService("homeassistant", action), | ||
| ...item, | ||
| words: [`${item.categoryText} ${item.primaryText}`], | ||
| }, | ||
| this.hass.localize("ui.dialogs.generic.ok") | ||
| ); | ||
|
|
@@ -550,19 +562,22 @@ export class QuickBar extends LitElement { | |
| items: BaseNavigationCommand[] | ||
| ): CommandItem[] { | ||
| return items.map((item) => { | ||
| const categoryKey = "navigation"; | ||
| const categoryText = this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.types.${categoryKey}` | ||
| ); | ||
| const categoryKey: CommandItem["categoryKey"] = "navigation"; | ||
|
|
||
| return { | ||
| const navItem = { | ||
| ...item, | ||
| categoryKey, | ||
| iconPath: mdiEarth, | ||
| categoryText, | ||
| filterText: `${categoryText} ${item.primaryText}`, | ||
| categoryText: this.hass.localize( | ||
| `ui.dialogs.quick-bar.commands.types.${categoryKey}` | ||
| ), | ||
| action: () => navigate(this, item.path), | ||
| }; | ||
|
|
||
| return { | ||
| ...navItem, | ||
| words: [`${navItem.categoryText} ${navItem.primaryText}`], | ||
| categoryKey, | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.