Skip to content

Commit 62e90ea

Browse files
author
NellowTCS
committed
Some tiny state fixes
1 parent 27df322 commit 62e90ea

4 files changed

Lines changed: 39 additions & 16 deletions

File tree

Build/src/hooks/useKomorebi.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { KomorebiEngine } from "../core/engine/engine";
33
import type {
44
Track,
55
Playlist,
6+
PlaylistFolder,
67
EngineState,
78
QueueState,
89
} from "../core/engine/types";
@@ -31,6 +32,7 @@ export interface UseKomorebiReturn {
3132
isLoading: boolean;
3233

3334
state: EngineState;
35+
songs: Track[];
3436
currentTrack: Track | null;
3537
isPlaying: boolean;
3638
currentTime: number;
@@ -75,6 +77,14 @@ export interface UseKomorebiReturn {
7577
getSongsByAlbum: (album: string) => Track[];
7678
}
7779

80+
declare global {
81+
interface MusicLibrary {
82+
songs: Track[];
83+
playlists: (Playlist | PlaylistFolder)[];
84+
favorites: string[];
85+
}
86+
}
87+
7888
const DEFAULT_QUEUE: QueueState = {
7989
tracks: [],
8090
currentIndex: -1,
@@ -123,6 +133,7 @@ export function useKomorebi(
123133
const [error, setError] = useState<string | null>(null);
124134

125135
const [state, setState] = useState<EngineState>(createInitialState);
136+
const [songs, setSongs] = useState<Track[]>([]);
126137
const [currentTrack, setCurrentTrack] = useState<Track | null>(null);
127138
const [currentTime, setCurrentTime] = useState(0);
128139
const [duration, setDuration] = useState(0);
@@ -180,6 +191,8 @@ export function useKomorebi(
180191
engine.on("ready", handleReady);
181192

182193
const handleLibraryChange = () => {
194+
console.log("[handleLibraryChange] called, songs:", library.getState().songs.length);
195+
setSongs([...library.getState().songs]);
183196
setState(engine.getState());
184197
};
185198
library.on("songadded", handleLibraryChange);
@@ -213,6 +226,8 @@ export function useKomorebi(
213226

214227
loadLibrary().then(() => {
215228
setIsReady(true);
229+
console.log("[loadLibrary] setting songs:", library.getState().songs.length);
230+
setSongs([...library.getState().songs]);
216231
setState(engine.getState());
217232
});
218233

@@ -381,6 +396,7 @@ export function useKomorebi(
381396
}, []);
382397

383398
const addSong = useCallback((song: Track) => {
399+
console.log("[addSong] called", { songId: song.id, title: song.title });
384400
libraryRef.current?.addSong(song);
385401
}, []);
386402

@@ -437,6 +453,7 @@ export function useKomorebi(
437453
isLoading,
438454

439455
state,
456+
songs,
440457
currentTrack,
441458
isPlaying: state.state === "playing",
442459
currentTime,

Build/src/ui/components/features/Home.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ const PlaylistCardItem = React.memo<{
8282

8383
export const Home: React.FC<HomeProps> = ({ komorebi, onAddMusic }) => {
8484
const { t } = useTranslation();
85-
const { currentTrack, library, playSong, getFavorites } = komorebi;
85+
const { currentTrack, library, songs: komorebiSongs, playSong, getFavorites } = komorebi;
8686
const { goToSongs } = useNavigation();
8787

8888
const libraryState = library.getState();
89-
const songs = libraryState.songs;
89+
const songs = komorebiSongs;
9090
const playlists = libraryState.playlists;
9191
const favorites = libraryState.favorites;
9292

Build/src/ui/components/features/Playlist.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const PlaylistView = memo(function PlaylistView({
2727
komorebi,
2828
}: PlaylistViewProps) {
2929
const { t } = useTranslation();
30-
const library = komorebi.library;
30+
const { songs, library } = komorebi;
3131
const libraryState = library.getState();
3232
const [playlistSearchQuery, setPlaylistSearchQuery] = useState("");
3333
const [playlistImages, setPlaylistImages] = useState<Record<string, string>>(
@@ -170,7 +170,7 @@ export const PlaylistView = memo(function PlaylistView({
170170
isCancelled = true;
171171
clearTimeout(timeoutId);
172172
};
173-
}, [libraryState.playlists, libraryState.songs]);
173+
}, [libraryState.playlists, songs]);
174174

175175
const filteredPlaylists = useMemo(
176176
() =>
@@ -266,10 +266,10 @@ export const PlaylistView = memo(function PlaylistView({
266266
const allSongs: Playlist = {
267267
id: "all-songs",
268268
name: t("allSongs"),
269-
songs: libraryState.songs,
269+
songs: songs,
270270
};
271271
komorebi.playSong(allSongs.songs[0], allSongs);
272-
}, [libraryState.songs, komorebi, t]);
272+
}, [songs, komorebi, t]);
273273

274274
const handleShare = useCallback(
275275
(playlist: Playlist) => {
@@ -425,7 +425,7 @@ export const PlaylistView = memo(function PlaylistView({
425425
>
426426
<Icon name="music" size={16} decorative />
427427
{t("allSongs")}
428-
<span className={styles.songCount}>{libraryState.songs.length}</span>
428+
<span className={styles.songCount}>{songs.length}</span>
429429
</button>
430430

431431
<button
@@ -434,7 +434,7 @@ export const PlaylistView = memo(function PlaylistView({
434434
handlePlaylistSelect({
435435
id: "favorites",
436436
name: t("favorites.favorites"),
437-
songs: libraryState.songs.filter((s) =>
437+
songs: songs.filter((s) =>
438438
libraryState.favorites.includes(s.id),
439439
),
440440
})

Build/src/ui/components/layout/MainContent.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,14 @@ export const MainContent = ({
218218
}: MainContentProps) => {
219219
const { t } = useTranslation();
220220
const { state: navState, goToSongs, goHome } = useNavigation();
221-
const library = komorebi.library;
222-
const libraryState = library.getState();
221+
const { songs, library } = komorebi;
222+
console.log("[MainContent] songs:", songs.length, "library songs:", library.getState().songs.length);
223+
224+
const libraryState: MusicLibrary = {
225+
songs,
226+
playlists: library.getState().playlists,
227+
favorites: library.getState().favorites,
228+
};
223229

224230
const [songSearchQuery, setSongSearchQuery] = useState("");
225231
const [sortBy, setSortBy] = useState<
@@ -274,24 +280,24 @@ export const MainContent = ({
274280

275281
const songsToDisplay = React.useMemo(() => {
276282
if (navState.view === "artist" && navState.artist) {
277-
return libraryState.songs.filter(
283+
return songs.filter(
278284
(song: Track) => song.artist === navState.artist,
279285
);
280286
} else if (navState.view === "album" && navState.album) {
281-
return libraryState.songs.filter(
287+
return songs.filter(
282288
(song: Track) => song.album === navState.album,
283289
);
284290
} else if (engineState.currentPlaylist) {
285291
return engineState.currentPlaylist.songs;
286292
} else {
287-
return libraryState.songs;
293+
return songs;
288294
}
289295
}, [
290296
navState.view,
291297
navState.artist,
292298
navState.album,
293299
engineState.currentPlaylist,
294-
libraryState.songs,
300+
songs,
295301
]);
296302

297303
const filteredSongs = React.useMemo(() => {
@@ -370,7 +376,7 @@ export const MainContent = ({
370376
e.stopPropagation();
371377
const wasFavorite = library.isFavorite(songId);
372378
toggleFavorite(songId);
373-
const song = libraryState.songs.find((s) => s.id === songId);
379+
const song = songs.find((s) => s.id === songId);
374380
if (song) {
375381
toast.success(
376382
wasFavorite
@@ -583,7 +589,7 @@ export const MainContent = ({
583589
{/* Add To Popover */}
584590
<AddToPopover
585591
songs={selectedSongs
586-
.map((id) => libraryState.songs.find((s) => s.id === id))
592+
.map((id) => songs.find((s) => s.id === id))
587593
.filter((s): s is Track => s !== undefined)}
588594
library={libraryState}
589595
onCreatePlaylist={createPlaylist}

0 commit comments

Comments
 (0)