Skip to content

Commit 3ac9e01

Browse files
committed
fix(profile/pages): getting time range
1 parent 2e28cdf commit 3ac9e01

File tree

10 files changed

+55
-15
lines changed

10 files changed

+55
-15
lines changed

app/profile/[id]/@top/@albums/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export default async function ProfileTopAlbumsSubPage({
4444
const statsMeasurement = validateStatsMeasurement(
4545
searchParams[STATS_MEASUREMENT]
4646
)
47-
const timeRange = validateTimeRange(searchParams[TIME_RANGE], statsProvider)
47+
const timeRange = validateTimeRange(
48+
searchParams[TIME_RANGE],
49+
statsProvider,
50+
createdAt
51+
)
4852
const view = validateView(searchParams[VIEW])
4953

5054
if (statsProvider === StatsProvider.SPOTIFY) return null

app/profile/[id]/@top/@artists/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export default async function ProfileTopArtistsSubPage({
5050
const statsMeasurement = validateStatsMeasurement(
5151
searchParams[STATS_MEASUREMENT]
5252
)
53-
const timeRange = validateTimeRange(searchParams[TIME_RANGE], statsProvider)
53+
const timeRange = validateTimeRange(
54+
searchParams[TIME_RANGE],
55+
statsProvider,
56+
createdAt
57+
)
5458
const view = validateView(searchParams[VIEW])
5559

5660
let items: ArtistEntity[] | RigtchStatsResponse<ArtistEntity>

app/profile/[id]/@top/@genres/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ export default async function ProfileTopGenresSubPage({
4848
const statsMeasurement = validateStatsMeasurement(
4949
searchParams[STATS_MEASUREMENT]
5050
)
51-
const timeRange = validateTimeRange(searchParams[TIME_RANGE], statsProvider)
51+
const timeRange = validateTimeRange(
52+
searchParams[TIME_RANGE],
53+
statsProvider,
54+
createdAt
55+
)
5256

5357
let items: string[] | RigtchStatsResponse<string>
5458

app/profile/[id]/@top/@tracks/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ export default async function ProfileTopTracksSubPage({
4747
searchParams[STATS_PROVIDER],
4848
createdAt
4949
)
50-
const timeRange = validateTimeRange(searchParams[TIME_RANGE], statsProvider)
50+
const timeRange = validateTimeRange(
51+
searchParams[TIME_RANGE],
52+
statsProvider,
53+
createdAt
54+
)
5155
const statsMeasurement = validateStatsMeasurement(
5256
searchParams[STATS_MEASUREMENT]
5357
)

app/profile/[id]/top/albums/page.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '@app/profile/utils/validators'
1414
import { afterParamFactory } from '@app/profile/utils/factories'
1515
import { StatsProvider, type RigtchTimeRange } from '@app/profile/enums'
16+
import { getUser } from '@app/api/fetchers'
1617

1718
export const runtime = 'edge'
1819

@@ -21,19 +22,24 @@ export default async function ProfileTopAlbumsPage({
2122
params,
2223
}: ProfilePageProps) {
2324
const userId = validateId(params.id)
25+
const token = await getServerToken()
26+
27+
if (!token) redirect('/')
28+
29+
const { createdAt } = await getUser(token, {
30+
userId,
31+
})
32+
2433
const statsMeasurement = validateStatsMeasurement(
2534
searchParams[STATS_MEASUREMENT]
2635
)
2736
const timeRange = validateTimeRange(
2837
searchParams[TIME_RANGE],
29-
StatsProvider.RIGTCH
38+
StatsProvider.RIGTCH,
39+
createdAt
3040
)
3141
const view = validateView(searchParams[VIEW])
3242

33-
const token = await getServerToken()
34-
35-
if (!token) redirect('/')
36-
3743
const items = await getRigtchTopAlbums(token, {
3844
after: afterParamFactory(timeRange as RigtchTimeRange),
3945
userId,

app/profile/[id]/top/artists/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export default async function ProfileTopArtistsPage({
4949
const statsMeasurement = validateStatsMeasurement(
5050
searchParams[STATS_MEASUREMENT]
5151
)
52-
const timeRange = validateTimeRange(searchParams[TIME_RANGE], statsProvider)
52+
const timeRange = validateTimeRange(
53+
searchParams[TIME_RANGE],
54+
statsProvider,
55+
createdAt
56+
)
5357
const view = validateView(searchParams[VIEW])
5458

5559
let items: ArtistEntity[] | RigtchStatsResponse<ArtistEntity>

app/profile/[id]/top/genres/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ export default async function ProfileTopGenresPage({
4747
const statsMeasurement = validateStatsMeasurement(
4848
searchParams[STATS_MEASUREMENT]
4949
)
50-
const timeRange = validateTimeRange(searchParams[TIME_RANGE], statsProvider)
50+
const timeRange = validateTimeRange(
51+
searchParams[TIME_RANGE],
52+
statsProvider,
53+
createdAt
54+
)
5155

5256
let items: string[] | RigtchStatsResponse<string>
5357

app/profile/[id]/top/tracks/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export default async function ProfileTopTracksPage({
4949
const statsMeasurement = validateStatsMeasurement(
5050
searchParams[STATS_MEASUREMENT]
5151
)
52-
const timeRange = validateTimeRange(searchParams[TIME_RANGE], statsProvider)
52+
const timeRange = validateTimeRange(
53+
searchParams[TIME_RANGE],
54+
statsProvider,
55+
createdAt
56+
)
5357
const view = validateView(searchParams[VIEW])
5458

5559
let items: TrackEntity[] | RigtchStatsResponse<TrackEntity>

app/profile/components/common/stats-options.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export function StatsOptions() {
3535
const view = validateView(searchParams.get(VIEW))
3636
const timeRange = validateTimeRange(
3737
searchParams.get(TIME_RANGE),
38-
statsProvider
38+
statsProvider,
39+
user?.createdAt
3940
)
4041

4142
const route = pathname.split('/').at(-1)

app/profile/utils/validators/validate-time-range.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isTimeRangeDisabled } from '../helpers'
2+
13
import {
24
DEFAULT_RIGTCH_TIME_RANCE,
35
DEFAULT_SPOTIFY_TIME_RANCE,
@@ -18,7 +20,8 @@ function isRigtchTimeRange(value: string): value is RigtchTimeRange {
1820

1921
export function validateTimeRange(
2022
timeRange: string | string[] | null | undefined,
21-
provider: StatsProvider
23+
provider: StatsProvider,
24+
userCreatedAt?: Date
2225
) {
2326
if (typeof timeRange === 'string') {
2427
if (provider === StatsProvider.SPOTIFY && isSpotifyTimeRange(timeRange))
@@ -30,5 +33,7 @@ export function validateTimeRange(
3033

3134
return provider === StatsProvider.SPOTIFY
3235
? DEFAULT_SPOTIFY_TIME_RANCE
33-
: DEFAULT_RIGTCH_TIME_RANCE
36+
: isTimeRangeDisabled(DEFAULT_RIGTCH_TIME_RANCE, userCreatedAt)
37+
? RigtchTimeRange.WEEK
38+
: DEFAULT_RIGTCH_TIME_RANCE
3439
}

0 commit comments

Comments
 (0)