committed by
GitHub
84 changed files with 1775 additions and 532 deletions
@ -0,0 +1,20 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
.tb-home-load-retry { |
|||
padding: 0; |
|||
border: none; |
|||
background: none; |
|||
font: inherit; |
|||
color: var(--tb-primary-500, #305680); |
|||
cursor: pointer; |
|||
|
|||
&:hover { |
|||
text-decoration: underline; |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500, #305680); |
|||
outline-offset: 2px; |
|||
border-radius: 2px; |
|||
} |
|||
} |
|||
@ -0,0 +1,170 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
:host { |
|||
--sk-fill: rgba(0, 0, 0, 0.06); |
|||
box-sizing: border-box; |
|||
position: relative; |
|||
display: flex; |
|||
flex: 1 1 auto; |
|||
flex-direction: column; |
|||
gap: 12px; |
|||
min-height: 0; |
|||
overflow: hidden; |
|||
|
|||
&::after { |
|||
content: ''; |
|||
position: absolute; |
|||
inset: 0 auto 0 0; |
|||
width: 300%; |
|||
pointer-events: none; |
|||
background: linear-gradient(90deg, transparent 33.3%, rgba(255, 255, 255, 0.8) 50%, transparent 66.7%); |
|||
transform: translateX(-66.667%); |
|||
animation: tb-home-shimmer var(--loop-shimmer, 2.4s) linear infinite; |
|||
animation-delay: var(--sk-delay, 400ms); |
|||
} |
|||
} |
|||
|
|||
@keyframes tb-home-shimmer { |
|||
from { |
|||
transform: translateX(-66.667%); |
|||
} |
|||
to { |
|||
transform: none; |
|||
} |
|||
} |
|||
|
|||
.tb-sk-bar, .tb-sk-dot, .tb-sk-block, .tb-sk-tile { |
|||
display: block; |
|||
flex: none; |
|||
background: var(--sk-fill); |
|||
} |
|||
|
|||
.tb-sk-bar { |
|||
max-width: 100%; |
|||
height: 10px; |
|||
border-radius: 5px; |
|||
} |
|||
|
|||
.tb-sk-dot { |
|||
border-radius: 50%; |
|||
} |
|||
|
|||
.tb-sk-block { |
|||
max-width: 100%; |
|||
border-radius: 8px; |
|||
} |
|||
|
|||
.tb-sk-tiles { |
|||
display: flex; |
|||
flex-wrap: wrap; |
|||
gap: 8px; |
|||
} |
|||
|
|||
.tb-sk-tile { |
|||
width: 104px; |
|||
height: 60px; |
|||
border-radius: 8px; |
|||
} |
|||
|
|||
.tb-sk-rows { |
|||
display: flex; |
|||
flex: 1 1 auto; |
|||
flex-direction: column; |
|||
min-height: 0; |
|||
} |
|||
|
|||
.tb-sk-row { |
|||
display: flex; |
|||
flex: 1 1 40px; |
|||
align-items: center; |
|||
gap: 16px; |
|||
min-height: 30px; |
|||
max-height: 56px; |
|||
border-bottom: 1px solid var(--tb-line-soft, rgba(0, 0, 0, 0.05)); |
|||
|
|||
&:last-child { |
|||
border-bottom: none; |
|||
} |
|||
} |
|||
|
|||
.tb-sk-row > .end, .tb-sk-foot > .end { |
|||
margin-left: auto; |
|||
} |
|||
|
|||
.tb-sk-rows > .tb-sk-head { |
|||
flex: none; |
|||
min-height: 32px; |
|||
|
|||
.tb-sk-bar { |
|||
height: 8px; |
|||
} |
|||
} |
|||
|
|||
.tb-sk-rows.tall .tb-sk-row { |
|||
flex-basis: 56px; |
|||
min-height: 44px; |
|||
max-height: 80px; |
|||
} |
|||
|
|||
.tb-sk-rows.plain .tb-sk-row { |
|||
flex-basis: 36px; |
|||
min-height: 28px; |
|||
max-height: 48px; |
|||
border-bottom: none; |
|||
} |
|||
|
|||
.tb-sk-foot { |
|||
display: flex; |
|||
align-items: center; |
|||
min-height: 36px; |
|||
margin-top: auto; |
|||
padding-top: 8px; |
|||
border-top: 1px solid var(--tb-line-soft, rgba(0, 0, 0, 0.05)); |
|||
} |
|||
|
|||
.tb-sk-stack { |
|||
display: flex; |
|||
flex: 1; |
|||
flex-direction: column; |
|||
gap: 8px; |
|||
min-width: 0; |
|||
} |
|||
|
|||
.tb-sk-kicker { |
|||
display: flex; |
|||
align-items: center; |
|||
min-height: 20px; |
|||
} |
|||
|
|||
.tb-sk-chart { |
|||
display: flex; |
|||
flex: 1 1 auto; |
|||
flex-direction: column; |
|||
gap: 8px; |
|||
min-height: 0; |
|||
} |
|||
|
|||
.tb-sk-chart-plot { |
|||
display: flex; |
|||
flex: 1 1 110px; |
|||
flex-direction: column; |
|||
justify-content: space-between; |
|||
min-height: 60px; |
|||
|
|||
i { |
|||
display: block; |
|||
height: 1px; |
|||
background: var(--sk-fill); |
|||
} |
|||
} |
|||
|
|||
.tb-sk-chart-x { |
|||
display: flex; |
|||
justify-content: space-between; |
|||
} |
|||
|
|||
@media (prefers-reduced-motion: reduce) { |
|||
:host::after { |
|||
display: none; |
|||
} |
|||
} |
|||
@ -0,0 +1,208 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { ChangeDetectionStrategy, Component, computed, input } from '@angular/core'; |
|||
|
|||
export type HomeSkeletonShape = 'alarms' | 'devices' | 'notifications' | 'activity' | 'dashboards' | 'links' | 'usage' |
|||
| 'system' | 'system-cluster' | 'install' | 'license' | 'get-started-sys-admin' | 'qr'; |
|||
|
|||
type Width = number | string; |
|||
|
|||
type Part = |
|||
{kind: 'bar'; width: Width; end?: boolean} |
|||
| {kind: 'dot'; size: number; end?: boolean} |
|||
| {kind: 'block'; width: Width; height: Width} |
|||
| {kind: 'stack'; parts: Part[]}; |
|||
|
|||
type Section = |
|||
{kind: 'tiles'; count: number} |
|||
| {kind: 'rows'; rows: Part[][]; head?: Part[]; look?: 'tall' | 'plain'} |
|||
| {kind: 'chart'} |
|||
| {kind: 'foot'} |
|||
| {kind: 'kicker'; width: number} |
|||
| {kind: 'part'; part: Part}; |
|||
|
|||
const bar = (width: Width, end = false): Part => ({kind: 'bar', width, end}); |
|||
const dot = (size: number, end = false): Part => ({kind: 'dot', size, end}); |
|||
const block = (width: Width, height: Width): Part => ({kind: 'block', width, height}); |
|||
const stack = (...parts: Part[]): Part => ({kind: 'stack', parts}); |
|||
const rows = (widths: string[], cells: (width: string, index: number) => Part[], head?: Part[], look?: 'tall' | 'plain'): Section => |
|||
({kind: 'rows', rows: widths.map((width, index) => cells(width, index)), head, look}); |
|||
|
|||
const SHAPES: Record<HomeSkeletonShape, Section[]> = { |
|||
alarms: [ |
|||
{kind: 'tiles', count: 2}, |
|||
rows(['30%', '24%', '34%', '26%', '28%'], width => [dot(16), bar(width), bar('16%'), bar(36, true), dot(20)], |
|||
[bar(56), bar('12%'), bar('12%'), bar(48, true)]), |
|||
{kind: 'foot'} |
|||
], |
|||
devices: [ |
|||
{kind: 'tiles', count: 2}, |
|||
rows(['34%', '28%', '38%', '30%', '32%'], width => [bar(width), bar('20%'), dot(10), bar(40, true)], |
|||
[bar(40), bar('14%'), bar(72, true)]), |
|||
{kind: 'foot'} |
|||
], |
|||
notifications: [ |
|||
rows(['72%', '60%', '80%', '56%', '66%'], (width, index) => |
|||
[dot(32), stack(bar(width), bar(['44%', '36%', '52%', '40%', '30%'][index])), bar(28, true)], undefined, 'tall'), |
|||
{kind: 'foot'} |
|||
], |
|||
activity: [ |
|||
{kind: 'chart'} |
|||
], |
|||
dashboards: [ |
|||
rows(['46%', '38%', '52%', '34%', '42%'], width => [dot(16), bar(width), bar(48, true)], [bar(40), bar(72, true)]), |
|||
{kind: 'foot'} |
|||
], |
|||
links: [ |
|||
rows(['48%', '60%', '40%'], width => [block(20, 20), bar(width)], undefined, 'plain') |
|||
], |
|||
usage: [ |
|||
{kind: 'kicker', width: 64}, |
|||
rows(['34%', '28%', '24%', '38%', '30%'], width => [bar(width), bar(48), bar('28%', true)], undefined, 'plain') |
|||
], |
|||
system: [ |
|||
{kind: 'tiles', count: 3}, |
|||
{kind: 'chart'} |
|||
], |
|||
'system-cluster': [ |
|||
rows(['30%', '26%', '34%', '28%', '32%', '24%'], width => [bar(width), bar('18%'), bar(36, true), bar(36), bar(36)], |
|||
[bar(64), bar('12%'), bar(28, true), bar(28), bar(28)]) |
|||
], |
|||
install: [ |
|||
{kind: 'tiles', count: 6}, |
|||
{kind: 'kicker', width: 80}, |
|||
rows(['40%', '52%', '34%'], width => [dot(16), bar(width)], undefined, 'plain') |
|||
], |
|||
license: [ |
|||
{kind: 'part', part: block('100%', 72)}, |
|||
rows(['30%', '24%', '36%', '28%', '20%'], width => [bar(width), bar(56), bar('30%', true)], undefined, 'plain') |
|||
], |
|||
qr: [ |
|||
{kind: 'part', part: block('100%', '100%')} |
|||
], |
|||
'get-started-sys-admin': [ |
|||
{kind: 'part', part: block('100%', 6)}, |
|||
{kind: 'kicker', width: 160}, |
|||
rows(['50%', '38%', '44%', '56%', '40%', '48%', '36%'], width => [dot(22), bar(width)], undefined, 'plain') |
|||
] |
|||
}; |
|||
|
|||
@Component({ |
|||
selector: 'tb-home-skeleton', |
|||
template: ` |
|||
<ng-template #partTpl let-part> |
|||
@switch (part.kind) { |
|||
@case ('bar') { |
|||
<i class="tb-sk-bar" [class.end]="part.end" [style.width]="size(part.width)"></i> |
|||
} |
|||
@case ('dot') { |
|||
<i class="tb-sk-dot" [class.end]="part.end" [style.width.px]="part.size" [style.height.px]="part.size"></i> |
|||
} |
|||
@case ('block') { |
|||
<i class="tb-sk-block" [style.width]="size(part.width)" [style.height]="size(part.height)"></i> |
|||
} |
|||
@case ('stack') { |
|||
<span class="tb-sk-stack"> |
|||
@for (child of part.parts; track $index) { |
|||
<ng-container *ngTemplateOutlet="partTpl; context: {$implicit: child}"></ng-container> |
|||
} |
|||
</span> |
|||
} |
|||
} |
|||
</ng-template> |
|||
@for (section of sections(); track $index) { |
|||
@switch (section.kind) { |
|||
@case ('tiles') { |
|||
<span class="tb-sk-tiles"> |
|||
@for (tile of tiles(section); track $index) { |
|||
<i class="tb-sk-tile"></i> |
|||
} |
|||
</span> |
|||
} |
|||
@case ('rows') { |
|||
<span class="tb-sk-rows" [class.tall]="look(section) === 'tall'" [class.plain]="look(section) === 'plain'"> |
|||
@if (head(section); as head) { |
|||
<span class="tb-sk-row tb-sk-head"> |
|||
@for (part of head; track $index) { |
|||
<ng-container *ngTemplateOutlet="partTpl; context: {$implicit: part}"></ng-container> |
|||
} |
|||
</span> |
|||
} |
|||
@for (row of rowsOf(section); track $index) { |
|||
<span class="tb-sk-row"> |
|||
@for (part of row; track $index) { |
|||
<ng-container *ngTemplateOutlet="partTpl; context: {$implicit: part}"></ng-container> |
|||
} |
|||
</span> |
|||
} |
|||
</span> |
|||
} |
|||
@case ('chart') { |
|||
<span class="tb-sk-chart"> |
|||
<span class="tb-sk-chart-plot"><i></i><i></i><i></i><i></i></span> |
|||
<span class="tb-sk-chart-x"> |
|||
@for (label of chartLabels; track $index) { |
|||
<i class="tb-sk-bar" style="width: 36px"></i> |
|||
} |
|||
</span> |
|||
</span> |
|||
} |
|||
@case ('foot') { |
|||
<span class="tb-sk-foot"><i class="tb-sk-bar" style="width: 84px"></i><i class="tb-sk-bar end" style="width: 56px"></i></span> |
|||
} |
|||
@case ('kicker') { |
|||
<span class="tb-sk-kicker"><i class="tb-sk-bar" [style.width.px]="kickerWidth(section)"></i></span> |
|||
} |
|||
@case ('part') { |
|||
<ng-container *ngTemplateOutlet="partTpl; context: {$implicit: partOf(section)}"></ng-container> |
|||
} |
|||
} |
|||
} |
|||
`,
|
|||
styleUrls: ['./home-skeleton.component.scss'], |
|||
host: { |
|||
'class': 'tb-sk', |
|||
'aria-hidden': 'true', |
|||
'[style.--sk-delay.ms]': 'delay()' |
|||
}, |
|||
standalone: false, |
|||
changeDetection: ChangeDetectionStrategy.OnPush |
|||
}) |
|||
export class HomeSkeletonComponent { |
|||
|
|||
shape = input.required<HomeSkeletonShape>(); |
|||
|
|||
delay = input(400); |
|||
|
|||
readonly sections = computed(() => SHAPES[this.shape()]); |
|||
|
|||
readonly chartLabels = [0, 1, 2, 3, 4]; |
|||
|
|||
size(value: Width): string { |
|||
return typeof value === 'number' ? `${value}px` : value; |
|||
} |
|||
|
|||
tiles(section: Section): number[] { |
|||
return section.kind === 'tiles' ? Array.from({length: section.count}, (_, index) => index) : []; |
|||
} |
|||
|
|||
rowsOf(section: Section): Part[][] { |
|||
return section.kind === 'rows' ? section.rows : []; |
|||
} |
|||
|
|||
head(section: Section): Part[] | null { |
|||
return section.kind === 'rows' ? section.head ?? null : null; |
|||
} |
|||
|
|||
look(section: Section): string | null { |
|||
return section.kind === 'rows' ? section.look ?? null : null; |
|||
} |
|||
|
|||
kickerWidth(section: Section): number { |
|||
return section.kind === 'kicker' ? section.width : 0; |
|||
} |
|||
|
|||
partOf(section: Section): Part | null { |
|||
return section.kind === 'part' ? section.part : null; |
|||
} |
|||
} |
|||
@ -0,0 +1,155 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { inject, Injectable, signal } from '@angular/core'; |
|||
import { Observable } from 'rxjs'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { getCurrentAuthUser } from '@core/auth/auth.selectors'; |
|||
|
|||
const KEPT_KEY = 'tb_home_load_kept'; |
|||
const READ_KEY = 'tb_home_load_read'; |
|||
const READ_EVERY_MS = 60_000; |
|||
|
|||
interface Kept { |
|||
h: Record<string, number>; |
|||
at: Record<string, number>; |
|||
} |
|||
|
|||
export class HomeRead<T> { |
|||
|
|||
readonly reading = signal(false); |
|||
readonly failed = signal(false); |
|||
readonly fresh = signal(false); |
|||
readonly readAt = signal<number | null>(null); |
|||
readonly last: T | null; |
|||
|
|||
constructor(private load: HomeLoadService, |
|||
private card: string, |
|||
private request: () => Observable<T>, |
|||
private apply: (data: T) => void) { |
|||
this.last = load.lastRead<T>(card); |
|||
} |
|||
|
|||
start(): void { |
|||
if (this.last) { |
|||
this.apply(this.last); |
|||
this.readAt.set(this.load.readAt(this.card)); |
|||
} |
|||
this.read(); |
|||
} |
|||
|
|||
read(): void { |
|||
this.reading.set(true); |
|||
this.failed.set(false); |
|||
this.request().subscribe({ |
|||
next: data => { |
|||
this.apply(data); |
|||
this.keep(data); |
|||
this.fresh.set(true); |
|||
this.reading.set(false); |
|||
}, |
|||
error: () => { |
|||
this.failed.set(true); |
|||
this.reading.set(false); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
keep(data: T): void { |
|||
this.load.keepRead(this.card, data); |
|||
this.readAt.set(Date.now()); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* What Home keeps between visits, per user: each card's height at its width, so a skeleton takes the room the card had and |
|||
* the read lands without moving the page; when each card was last read, for the foot of a failed refresh; and the last read |
|||
* itself for the cards that show it at once (the person's own and slow-changing things). |
|||
*/ |
|||
@Injectable({ |
|||
providedIn: 'root' |
|||
}) |
|||
export class HomeLoadService { |
|||
|
|||
private store = inject(Store<AppState>); |
|||
private kept: Kept = this.readKept(); |
|||
private persistTimer: ReturnType<typeof setTimeout> | null = null; |
|||
|
|||
height(card: string, width: number): number | null { |
|||
return this.kept.h[this.heightKey(card, width)] ?? null; |
|||
} |
|||
|
|||
keepHeight(card: string, width: number, height: number): void { |
|||
const key = this.heightKey(card, width); |
|||
if (height > 0 && this.kept.h[key] !== height) { |
|||
this.kept.h[key] = height; |
|||
this.persist(); |
|||
} |
|||
} |
|||
|
|||
readAt(card: string): number | null { |
|||
return this.kept.at[this.cardKey(card)] ?? null; |
|||
} |
|||
|
|||
markRead(card: string): void { |
|||
const key = this.cardKey(card); |
|||
const now = Date.now(); |
|||
if (!this.kept.at[key] || now - this.kept.at[key] > READ_EVERY_MS) { |
|||
this.kept.at[key] = now; |
|||
this.persist(); |
|||
} |
|||
} |
|||
|
|||
lastRead<T>(card: string): T | null { |
|||
try { |
|||
const value = localStorage.getItem(`${READ_KEY}_${this.cardKey(card)}`); |
|||
return value ? JSON.parse(value) as T : null; |
|||
} catch { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
keepRead<T>(card: string, data: T): void { |
|||
try { |
|||
localStorage.setItem(`${READ_KEY}_${this.cardKey(card)}`, JSON.stringify(data)); |
|||
} catch { |
|||
return; |
|||
} |
|||
this.markRead(card); |
|||
} |
|||
|
|||
reader<T>(card: string, request: () => Observable<T>, apply: (data: T) => void): HomeRead<T> { |
|||
return new HomeRead(this, card, request, apply); |
|||
} |
|||
|
|||
private cardKey(card: string): string { |
|||
return `${getCurrentAuthUser(this.store)?.userId ?? ''}|${card}`; |
|||
} |
|||
|
|||
private heightKey(card: string, width: number): string { |
|||
return `${this.cardKey(card)}|${Math.round(width / 24)}`; |
|||
} |
|||
|
|||
private readKept(): Kept { |
|||
try { |
|||
const kept = JSON.parse(localStorage.getItem(KEPT_KEY) || '{}') || {}; |
|||
return {h: kept.h || {}, at: kept.at || {}}; |
|||
} catch { |
|||
return {h: {}, at: {}}; |
|||
} |
|||
} |
|||
|
|||
private persist(): void { |
|||
if (this.persistTimer) { |
|||
clearTimeout(this.persistTimer); |
|||
} |
|||
this.persistTimer = setTimeout(() => { |
|||
this.persistTimer = null; |
|||
try { |
|||
localStorage.setItem(KEPT_KEY, JSON.stringify(this.kept)); |
|||
} catch { |
|||
return; |
|||
} |
|||
}, 200); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue