Skip to content

Commit f6fa5e1

Browse files
committed
fix(homepage): re-introduce ad slot on initial recent song sample
1 parent 9f58a58 commit f6fa5e1

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { PageDto, SongPreviewDtoType } from '@nbw/database';
77
import axiosInstance from '@web/lib/axios';
88

99
interface RecentSongsState {
10-
recentSongs: (SongPreviewDtoType | null)[];
10+
recentSongs: (SongPreviewDtoType | null | undefined)[];
1111
recentError: string;
1212
isLoading: boolean;
1313
hasMore: boolean;
@@ -28,6 +28,20 @@ type RecentSongsStore = RecentSongsState & RecentSongsActions;
2828

2929
const adCount = 1;
3030
const pageSize = 12;
31+
const fetchCount = pageSize - adCount;
32+
33+
function injectAdSlots(
34+
songs: SongPreviewDtoType[],
35+
): Array<SongPreviewDtoType | undefined> {
36+
const songsWithAds: Array<SongPreviewDtoType | undefined> = [...songs];
37+
38+
for (let i = 0; i < adCount; i++) {
39+
const adPosition = Math.floor(Math.random() * (songsWithAds.length + 1));
40+
songsWithAds.splice(adPosition, 0, undefined);
41+
}
42+
43+
return songsWithAds;
44+
}
3145

3246
export const useRecentSongsStore = create<RecentSongsStore>((set, get) => ({
3347
// Initial state
@@ -65,8 +79,6 @@ export const useRecentSongsStore = create<RecentSongsStore>((set, get) => ({
6579
set({ isLoading: true });
6680

6781
try {
68-
const fetchCount = pageSize - adCount;
69-
7082
const params: Record<string, any> = {
7183
page,
7284
limit: fetchCount, // TODO: fix constants
@@ -83,20 +95,15 @@ export const useRecentSongsStore = create<RecentSongsStore>((set, get) => ({
8395
{ params },
8496
);
8597

86-
const newSongs: Array<SongPreviewDtoType | undefined> =
87-
response.data.content;
88-
89-
for (let i = 0; i < adCount; i++) {
90-
const adPosition = Math.floor(Math.random() * newSongs.length) + 1;
91-
newSongs.splice(adPosition, 0, undefined);
92-
}
98+
const fetchedSongs = response.data.content;
99+
const newSongs = injectAdSlots(fetchedSongs);
93100

94101
set((state) => ({
95102
recentSongs: [
96103
...state.recentSongs.filter((song) => song !== null),
97-
...response.data.content,
104+
...newSongs,
98105
],
99-
hasMore: response.data.content.length >= fetchCount,
106+
hasMore: fetchedSongs.length >= fetchCount,
100107
recentError: '',
101108
}));
102109
} catch (error) {
@@ -113,7 +120,7 @@ export const useRecentSongsStore = create<RecentSongsStore>((set, get) => ({
113120
set({
114121
selectedCategory: category,
115122
page: 1,
116-
recentSongs: Array(12).fill(null),
123+
recentSongs: Array(pageSize).fill(null),
117124
hasMore: true,
118125
});
119126
},
@@ -126,7 +133,7 @@ export const useRecentSongsStore = create<RecentSongsStore>((set, get) => ({
126133
}
127134

128135
set({
129-
recentSongs: [...recentSongs, ...Array(12).fill(null)],
136+
recentSongs: [...recentSongs, ...Array(pageSize).fill(null)],
130137
page: get().page + 1,
131138
});
132139
},

0 commit comments

Comments
 (0)