|
| 1 | +import { formatBytes } from '@infrastructure/storage/EngineCacheStorage'; |
| 2 | + |
| 3 | +export interface StoragePanelPorts { |
| 4 | + isSupported(): boolean; |
| 5 | + report(): Promise<{ cachedBytes: number; entries: number }>; |
| 6 | + clear(): Promise<boolean>; |
| 7 | + /** Fetches the engine now, so the first real use is not the first download. */ |
| 8 | + preload(): Promise<void>; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Lets the user see and reclaim the space the recognition engine takes. |
| 13 | + * |
| 14 | + * Downloading is offered up front rather than only happening on first use: knowing it is |
| 15 | + * ~29 MB before it starts is the difference between a considered choice and a surprise on |
| 16 | + * mobile data. |
| 17 | + */ |
| 18 | +export class StoragePanel { |
| 19 | + private busy = false; |
| 20 | + |
| 21 | + constructor( |
| 22 | + private readonly root: HTMLElement, |
| 23 | + private readonly ports: StoragePanelPorts, |
| 24 | + ) { |
| 25 | + this.render(); |
| 26 | + void this.refresh(); |
| 27 | + } |
| 28 | + |
| 29 | + private render(): void { |
| 30 | + this.root.innerHTML = ` |
| 31 | + <section class="card"> |
| 32 | + <h2 class="card__title">Espacio en el dispositivo</h2> |
| 33 | + <p class="card__body"> |
| 34 | + El motor de reconocimiento ocupa unos 29 MB y se guarda la primera vez que lo usas, |
| 35 | + para que después funcione sin conexión. Puedes descargarlo ahora o liberarlo cuando |
| 36 | + quieras: se volverá a bajar solo la próxima vez que enciendas la cámara. |
| 37 | + </p> |
| 38 | +
|
| 39 | + <p class="storage" id="storage-figure">—</p> |
| 40 | +
|
| 41 | + <div class="actions"> |
| 42 | + <button class="button button--quiet" id="preload" type="button">Descargar ahora</button> |
| 43 | + <button class="button button--quiet" id="clear" type="button">Liberar espacio</button> |
| 44 | + </div> |
| 45 | +
|
| 46 | + <p class="status" id="storage-status" role="status"></p> |
| 47 | + <p class="card__body card__body--tight"> |
| 48 | + Los signos que le hayas enseñado no se borran con esto: son tuyos y se guardan aparte. |
| 49 | + </p> |
| 50 | + </section> |
| 51 | + `; |
| 52 | + |
| 53 | + this.button('preload').addEventListener('click', () => void this.preload()); |
| 54 | + this.button('clear').addEventListener('click', () => void this.clear()); |
| 55 | + } |
| 56 | + |
| 57 | + private button(id: string): HTMLButtonElement { |
| 58 | + return this.root.querySelector<HTMLButtonElement>(`#${id}`)!; |
| 59 | + } |
| 60 | + |
| 61 | + private say(message: string): void { |
| 62 | + this.root.querySelector<HTMLElement>('#storage-status')!.textContent = message; |
| 63 | + } |
| 64 | + |
| 65 | + private setBusy(busy: boolean): void { |
| 66 | + this.busy = busy; |
| 67 | + this.button('preload').disabled = busy; |
| 68 | + this.button('clear').disabled = busy; |
| 69 | + } |
| 70 | + |
| 71 | + private async preload(): Promise<void> { |
| 72 | + if (this.busy) return; |
| 73 | + this.setBusy(true); |
| 74 | + this.say('Descargando el motor…'); |
| 75 | + try { |
| 76 | + await this.ports.preload(); |
| 77 | + this.say('Motor descargado. Ya funciona sin conexión.'); |
| 78 | + } catch { |
| 79 | + this.say('No se pudo descargar. Comprueba la conexión.'); |
| 80 | + } finally { |
| 81 | + this.setBusy(false); |
| 82 | + await this.refresh(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private async clear(): Promise<void> { |
| 87 | + if (this.busy) return; |
| 88 | + this.setBusy(true); |
| 89 | + try { |
| 90 | + const removed = await this.ports.clear(); |
| 91 | + this.say( |
| 92 | + removed |
| 93 | + ? 'Espacio liberado. Se volverá a descargar la próxima vez.' |
| 94 | + : 'No había nada guardado.', |
| 95 | + ); |
| 96 | + } finally { |
| 97 | + this.setBusy(false); |
| 98 | + await this.refresh(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private async refresh(): Promise<void> { |
| 103 | + const figure = this.root.querySelector<HTMLElement>('#storage-figure')!; |
| 104 | + |
| 105 | + if (!this.ports.isSupported()) { |
| 106 | + figure.textContent = 'Este navegador no permite consultar el almacenamiento.'; |
| 107 | + this.setBusy(true); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const { cachedBytes, entries } = await this.ports.report(); |
| 112 | + figure.textContent = |
| 113 | + entries === 0 ? 'Nada guardado todavía' : `${formatBytes(cachedBytes)} guardados`; |
| 114 | + } |
| 115 | +} |
0 commit comments