Skip to content

Commit 713b113

Browse files
committed
feat: enhance FeaturedSongsProvider to determine initial timespan based on available songs
- Added a helper function to find the first non-empty timespan or default to 'week'. - Updated state initialization to use the determined initial timespan for featured songs.
1 parent a5d8d40 commit 713b113

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

apps/frontend/src/modules/browse/components/client/context/FeaturedSongs.context.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,38 @@ export function FeaturedSongsProvider({
2525
children: React.ReactNode;
2626
initialFeaturedSongs: FeaturedSongsDto;
2727
}) {
28+
// Helper function to find the first non-empty timespan or default to 'week'
29+
const getInitialTimespan = (): TimespanType => {
30+
// Check if all timespans have songs
31+
const allHaveSongs = TIMESPANS.every(
32+
(ts) => initialFeaturedSongs[ts]?.length > 0,
33+
);
34+
35+
// If all have songs, default to 'week'
36+
if (allHaveSongs) {
37+
return 'week';
38+
}
39+
40+
// Otherwise, find the first timespan that has songs
41+
for (const ts of TIMESPANS) {
42+
if (initialFeaturedSongs[ts]?.length > 0) {
43+
return ts;
44+
}
45+
}
46+
47+
// If none have songs, default to 'week'
48+
return 'week';
49+
};
50+
51+
const initialTimespan = getInitialTimespan();
52+
2853
// Featured songs
2954
const [featuredSongs] = useState<FeaturedSongsDto>(initialFeaturedSongs);
3055
const [featuredSongsPage, setFeaturedSongsPage] = useState<SongPreviewDto[]>(
31-
initialFeaturedSongs.week,
56+
initialFeaturedSongs[initialTimespan],
3257
);
3358

34-
const [timespan, setTimespan] = useState<TimespanType>('week');
59+
const [timespan, setTimespan] = useState<TimespanType>(initialTimespan);
3560

3661
const timespanEmpty = Object.keys(featuredSongs).reduce(
3762
(result, timespan) => {

0 commit comments

Comments
 (0)