From a224d5baf826722a32406c7eaa5da136652df6b9 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 31 May 2024 14:04:28 -0500 Subject: [PATCH] Transition between loading states in the album Ui --- frontend/src/routes/Home.tsx | 82 ++++++++++++++++++++++--------- frontend/wailsjs/go/main/App.d.ts | 4 +- frontend/wailsjs/go/main/App.js | 8 +-- session.go | 2 +- 4 files changed, 67 insertions(+), 29 deletions(-) diff --git a/frontend/src/routes/Home.tsx b/frontend/src/routes/Home.tsx index 4b5f1ac..3d1ddfe 100644 --- a/frontend/src/routes/Home.tsx +++ b/frontend/src/routes/Home.tsx @@ -1,49 +1,90 @@ -import { For, Show, createEffect, createMemo, createResource, createSignal, onCleanup, onMount } from "solid-js"; -import { GetAlbumCover, GetRandomAlbums, LoadRandomAlbums } from "../../wailsjs/go/main/App"; +import { batch, createEffect, createMemo, createResource, createSignal, onMount } from "solid-js"; +import { GetAlbumCover, GetRandomAlbums, TriggerAlbumReload } from "../../wailsjs/go/main/App"; import { main } from "../../wailsjs/go/models"; +import { createStore } from "solid-js/store"; + +type AlbumsData = { + status: "loading" | "ok", + albums: Array +} export function Home() { const [hidden, setHidden] = createSignal(true); - const [albums, {mutate, refetch}] = createResource>(GetRandomAlbums); + + const [albumsStore, setAlbumsStore] = createStore({ + status: "loading", + albums: new Array(20).fill(null), + }); onMount(() => { // Fade in the UI setTimeout(() => setHidden(false), 150); + // Load the albums + loadAlbums(); }); - const reload = () => { - LoadRandomAlbums(); - mutate((new Array(20)).map(() => null)); - setTimeout(refetch, 100); + // Loads the random albums from Go. + // This function, when called multiple times, returns the same set. + const loadAlbums = async() => { + const response = await GetRandomAlbums(); + + // Update the albums + batch(() => { + for (let i = 0; i < response.length; i += 1) { + const album = response[i]; + + setAlbumsStore("albums", i, album); + } + }); }; + const triggerAlbumReload = () => { + // Assign all albums to null + batch(() => { + for (let i = 0; i < 20; i += 1) { + setAlbumsStore("albums", i, null); + } + }); + + TriggerAlbumReload(); + setTimeout(loadAlbums, 50); + }; + + const els = albumsStore.albums.map((_, idx) => ()); + return (

Random albums

- - {(album) => } - + {els}
); } -function Album(props: { album: main.Album | null }) { - const [coverBytes, {mutate, refetch}] = createResource | null>(async() => GetAlbumCover(props.album?.id ?? "")); +function Album(props: { albums: Array, idx: number }) { + const [coverBytes, {mutate, refetch}] = createResource | null>(async() => GetAlbumCover(props.albums[props.idx]?.id ?? "")); + const [albumName, setAlbumName] = createSignal(""); + const [artistName, setArtistName] = createSignal(""); + + const album = createMemo(() => props.albums[props.idx]); createEffect(() => { - if (!props.album) { + const a = album(); + if (a === null) { mutate(null); return; + } else { + setAlbumName(a.name); + setArtistName(a.albumArtist); } refetch(); @@ -57,27 +98,24 @@ function Album(props: { album: main.Album | null }) { return `data:;base64,${bytes}`; }); - const isNull = createMemo(() => !props.album); + const isNull = createMemo(() => !album()); const opacity = createMemo(() => `${isNull() ? "opacity-0" : "opacity-100"} transition-opacity`); return (
- -
- - +
- +
- {props.album?.name ?? "..."} + {albumName()}
- {props.album?.albumArtist ?? "..."} + {artistName()}
diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 70c8fbf..8910e86 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -8,6 +8,6 @@ export function GetRandomAlbums():Promise>; export function Greet(arg1:string):Promise; -export function LoadRandomAlbums():Promise; - export function Login(arg1:string,arg2:string,arg3:string):Promise; + +export function TriggerAlbumReload():Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index bfe213e..c78fa10 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -14,10 +14,10 @@ export function Greet(arg1) { return window['go']['main']['App']['Greet'](arg1); } -export function LoadRandomAlbums() { - return window['go']['main']['App']['LoadRandomAlbums'](); -} - export function Login(arg1, arg2, arg3) { return window['go']['main']['App']['Login'](arg1, arg2, arg3); } + +export function TriggerAlbumReload() { + return window['go']['main']['App']['TriggerAlbumReload'](); +} diff --git a/session.go b/session.go index f8454a1..84dcba9 100644 --- a/session.go +++ b/session.go @@ -69,7 +69,7 @@ func (a *App) Login(server, username, password string) (bool, error) { } // Triggers a reload of random albums -func (a *App) LoadRandomAlbums() { +func (a *App) TriggerAlbumReload() { go loadAlbums(serverUrl) }