Skip to content

Commit

Permalink
don't show trash-card if no events are available (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
idaho authored Oct 3, 2024
1 parent 2523292 commit 498c166
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/cards/trash-card/container/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Cards extends LitElement implements BaseContainerElement {
}

if (!this.items || this.items.length === 0) {
return nothing;
return html`<trash-card-item-empty .config=${this.config} .hass=${this.hass}/>`;
}

const itemsPerRow = this.config.items_per_row ?? 1;
Expand Down
2 changes: 1 addition & 1 deletion src/cards/trash-card/container/chips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Chips extends LitElement implements BaseContainerElement {
}

if (!this.items || this.items.length === 0) {
return nothing;
return html`<trash-card-item-empty .config=${this.config} .hass=${this.hass}/>`;
}

const cssClasses = {
Expand Down
2 changes: 1 addition & 1 deletion src/cards/trash-card/container/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Icons extends LitElement implements BaseContainerElement {
}

if (!this.items || this.items.length === 0) {
return nothing;
return html`<trash-card-item-empty .config=${this.config} .hass=${this.hass}/>`;
}

const itemsPerRow = this.items.length;
Expand Down
4 changes: 2 additions & 2 deletions src/cards/trash-card/items/BaseItemElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LitElement, html } from 'lit';
import { state } from 'lit/decorators.js';
import { getPicture } from '../../../utils/getPicture';

import type { CardStyleConfig } from '../trash-card-config';
import type { TrashCardConfig } from '../trash-card-config';
import type { CalendarItem } from '../../../utils/calendarItem';
import type { HomeAssistant } from '../../../utils/ha';

Expand All @@ -13,7 +13,7 @@ class BaseItemElement<T = {}> extends LitElement {

@state() protected readonly hass?: HomeAssistant;

@state() protected readonly config?: CardStyleConfig;
@state() protected readonly config?: TrashCardConfig;

protected withBackground = false;

Expand Down
36 changes: 36 additions & 0 deletions src/cards/trash-card/items/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { html, nothing } from 'lit';
import { customElement } from 'lit/decorators.js';
import { TRASH_CARD_NAME } from '../const';
import { BaseItemElement } from './BaseItemElement';
import setupCustomlocalize from '../../../localize';

@customElement(`${TRASH_CARD_NAME}-item-empty`)
class ItemCard extends BaseItemElement {
public render () {
if (!this.hass || !this.config) {
return nothing;
}

const customLocalize = setupCustomlocalize(this.hass);

if (this.config.entities.length === 0) {
return html`
<ha-alert alert-type="error" .title="TrashCard">
<b>${customLocalize('card.not_found.title')}</b>
<div>${customLocalize('card.not_found.description')}</div>
</ha-alert>
`;
}

return html`
<ha-alert alert-type="warning" .title="TrashCard">
<div><b>TrashCard</b></div>
<div>${customLocalize('card.empty.description')}</div>
</hui-warning>
`;
}
}

export {
ItemCard
};
54 changes: 45 additions & 9 deletions src/cards/trash-card/trash-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import { TRASH_CARD_EDITOR_NAME, TRASH_CARD_NAME } from './const';
import { Debugger } from '../../utils/debugger';
import { getCalendarData } from '../../utils/getCalendarData';
import { getTimeZoneOffset } from '../../utils/getTimeZoneOffset';
import { fireEvent } from '../../utils/fireEvent';

import './container';
import './items/empty';

import type { PropertyValues } from 'lit';
import type { TrashCardConfig } from './trash-card-config';
import type { HomeAssistant } from '../../utils/ha';
import type { CalendarItem } from '../../utils/calendarItem';
import type { BaseContainerElement } from './container/BaseContainerElement';

declare global {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface HASSDomEvents {
// eslint-disable-next-line @typescript-eslint/naming-convention
'card-visibility-changed': { value: boolean };
}
}

registerCustomCard({
type: TRASH_CARD_NAME,
name: 'TrashCard',
Expand Down Expand Up @@ -64,6 +74,10 @@ export class TrashCard extends LitElement {

@property() private debugger?: Debugger;

@property({ type: Boolean }) public preview = false;

protected element?: BaseContainerElement;

private lastChanged?: Date;

public get hass (): HomeAssistant | undefined {
Expand Down Expand Up @@ -129,7 +143,9 @@ export class TrashCard extends LitElement {
}

protected shouldUpdate (changedProps: PropertyValues): boolean {
if (changedProps.has('currentItems')) {
super.updated(changedProps);

if (changedProps.has('currentItems') || changedProps.has('hass')) {
return true;
}

Expand All @@ -139,9 +155,33 @@ export class TrashCard extends LitElement {
this.fetchCurrentTrashData();
}

if (changedProps.has('preview')) {
return true;
}

return false;
}

protected update (changedProps: PropertyValues) {
super.update(changedProps);

if (!this.preview) {
if (this.config && this.config.entities.length > 0 && (!this.currentItems || this.currentItems.length === 0)) {
this.style.display = 'none';
this.toggleAttribute('hidden', true);
fireEvent(this, 'card-visibility-changed', { value: false });
} else {
this.style.display = 'block';
this.toggleAttribute('hidden', false);
fireEvent(this, 'card-visibility-changed', { value: true });
}
} else {
this.style.display = 'block';
this.toggleAttribute('hidden', false);
fireEvent(this, 'card-visibility-changed', { value: true });
}
}

protected createBaseContainerElement (cardStyle: TrashCardConfig['card_style']) {
try {
const tag = `trash-card-${cardStyle ?? 'card'}s-container`;
Expand Down Expand Up @@ -181,20 +221,16 @@ export class TrashCard extends LitElement {
return nothing;
}

if (!this.currentItems || this.currentItems.length === 0) {
return this.config.debug ? html`<trash-card-debug-container .debugger=${this.debugger}></trash-card-debug-card>` : nothing;
}

const element = this.createBaseContainerElement(this.config.card_style);
this.element = this.createBaseContainerElement(this.config.card_style);

if (!element) {
if (!this.element) {
return nothing;
}

element.setHass(this.hass);
this.element.setHass(this.hass);

return html`
${this.config.debug ? html`<trash-card-debug-container .debugger=${this.debugger}></trash-card-debug-card>` : ``}
${element}`;
${this.element}`;
}
}
4 changes: 2 additions & 2 deletions src/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as sl from './translations/sl.json';
import * as lv from './translations/lv.json';
import * as se from './translations/se.json';

import type { HomeAssistant } from './utils/ha';
import type { HomeAssistant, LocalizeFunc } from './utils/ha';

const languages: Record<string, unknown> = {
da,
Expand Down Expand Up @@ -41,7 +41,7 @@ const getTranslatedString = (key: string, lang: string): string | undefined => {
}
};

export default function setupCustomlocalize (hass?: HomeAssistant) {
export default function setupCustomlocalize (hass?: HomeAssistant): LocalizeFunc {
return function (key: string) {
const lang = hass?.locale.language ?? DEFAULT_LANG;

Expand Down
8 changes: 7 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@
}
},
"card": {
"not_found": "Entity not found",
"not_found": {
"title": "No entites defined",
"description": "Please select at least one calendar entity to get TrashCard to work."
},
"empty": {
"description": "Currently no events found. This message is only available during editing."
},
"trash": {
"today": "today",
"tomorrow": "tomorrow",
Expand Down
20 changes: 12 additions & 8 deletions src/utils/getColoredStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ const isColorModesArray = (modes: TrashCardConfig['color_mode'] | TrashCardConfi
Boolean(modes && Array.isArray(modes));

const calculateContrast = (currentColorString: string, darkMode: boolean, parentElement?: null | HTMLElement): 'text' | 'background' => {
const color = new Color(`rgb(${colors[currentColorString]})`);
try {
const color = new Color(`rgb(${colors[currentColorString]})`);

if (parentElement) {
const primaryTextColor = getComputedStyle(parentElement).getPropertyValue('--primary-text-color');
const primaryBackgroundColor = getComputedStyle(parentElement).getPropertyValue('--primary-background-color');
const primaryConstrast = color.contrast(new Color(primaryTextColor));
if (parentElement) {
const primaryTextColor = getComputedStyle(parentElement).getPropertyValue('--primary-text-color');
const primaryBackgroundColor = getComputedStyle(parentElement).getPropertyValue('--primary-background-color');
const primaryConstrast = color.contrast(new Color(primaryTextColor));

return primaryConstrast > color.contrast(new Color(primaryBackgroundColor)) ? 'text' : 'background';
}
return primaryConstrast > color.contrast(new Color(primaryBackgroundColor)) ? 'text' : 'background';
}

return color.contrast(new Color(darkMode ? 'white' : 'black')) ? 'text' : 'text';
return color.contrast(new Color(darkMode ? 'white' : 'black')) ? 'text' : 'text';
} catch {
return 'text';
}
};

const getColoredStyle = (modes: TrashCardConfig['color_mode'] | TrashCardConfig['color_mode'][], item: CalendarItem, parentElement?: null | HTMLElement, darkMode = false) => {
Expand Down

0 comments on commit 498c166

Please sign in to comment.