Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ cypress/videos/*
/playwright-report/
/playwright/.cache/
data/
tsconfig.tsbuildinfo
7 changes: 6 additions & 1 deletion app/item/[tag]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ ${getFiltersText(filter)}` : ''}`,
}

async function getItemData(searchParams, params) {
let range = searchParams.range || 'day'
let validRanges = ['active', 'hour', 'day', 'week', 'month', 'year', 'full']
let range = validRanges.includes(searchParams.range) ? searchParams.range : 'day'
let tag = params.tag as string
let itemFilter = getItemFilterFromUrl(searchParams)

Expand Down Expand Up @@ -278,6 +279,10 @@ async function fetchPrices(api: API, tag: string, range: string, isBazaar: boole
if (isBazaar) {
if (range === 'full') {
return api.getBazaarPricesByRange(tag, new Date(0), new Date())
} else if (range === 'month') {
let monthAgo = new Date()
monthAgo.setMonth(monthAgo.getMonth() - 1)
return api.getBazaarPricesByRange(tag, monthAgo, new Date())
} else {
return api.getBazaarPrices(tag, range as any)
}
Expand Down
8 changes: 6 additions & 2 deletions components/ItemPriceRange/ItemPriceRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getURLSearchParam } from '../../utils/Parser/URLParser'
import styles from './ItemPriceRange.module.css'
import { isClientSideRendering } from '../../utils/SSRUtils'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { VALID_RANGES } from '../../hooks/useValidRange'

export enum DateRange {
ACTIVE = 'active',
Expand Down Expand Up @@ -38,7 +39,10 @@ export function ItemPriceRange(props: Props) {
let pathname = usePathname()
let router = useRouter()
let searchParams = useSearchParams()
let [selectedDateRange, _setSelectedDateRange] = useState(searchParams.get('range') || DEFAULT_DATE_RANGE)
let [selectedDateRange, _setSelectedDateRange] = useState<DateRange>(() => {
let urlRange = searchParams.get('range')
return urlRange && VALID_RANGES.includes(urlRange) ? (urlRange as DateRange) : DEFAULT_DATE_RANGE
})
let urlUpdateCountRef = useRef<number[]>([])

useEffect(() => {
Expand All @@ -61,7 +65,7 @@ export function ItemPriceRange(props: Props) {

useEffect(() => {
let range = getURLSearchParam('range')
if (!range) {
if (!range || !VALID_RANGES.includes(range)) {
return
}
DEFAULT_DATE_RANGE = range as DateRange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AUCTION_GRAPH_LEGEND_SELECTION } from '../../../utils/SettingsUtils'
import ActiveAuctions from '../../ActiveAuctions/ActiveAuctions'
import ItemFilter, { getPrefillFilter } from '../../ItemFilter/ItemFilter'
import { DateRange, DEFAULT_DATE_RANGE, ItemPriceRange } from '../../ItemPriceRange/ItemPriceRange'
import { useValidRange } from '../../../hooks/useValidRange'
import Number from '../../Number/Number'
import GoogleSignIn from '../../GoogleSignIn/GoogleSignIn'
import RecentAuctions from '../../RecentAuctions/RecentAuctions'
Expand Down Expand Up @@ -82,7 +83,7 @@ let mounted = true

function AuctionHousePriceGraph(props: Props) {
let searchParams = useSearchParams()
let [fetchspan, setFetchspan] = useState((searchParams.get('range') as DateRange) || DEFAULT_DATE_RANGE)
let [fetchspan, setFetchspan] = useValidRange()
let [isLoading, setIsLoading] = useState(false)
let [noDataFound, setNoDataFound] = useState(false)
let [avgPrice, setAvgPrice] = useState(0)
Expand Down
9 changes: 7 additions & 2 deletions components/PriceGraph/BazaarPriceGraph/BazaarPriceGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getURLSearchParam } from '../../../utils/Parser/URLParser'
import { BAZAAR_GRAPH_LEGEND_SELECTION, BAZAAR_GRAPH_TYPE } from '../../../utils/SettingsUtils'
import { isClientSideRendering } from '../../../utils/SSRUtils'
import { DateRange, DEFAULT_DATE_RANGE, ItemPriceRange } from '../../ItemPriceRange/ItemPriceRange'
import { getValidatedRange } from '../../../hooks/useValidRange'
import Number from '../../Number/Number'
import RelatedItems from '../../RelatedItems/RelatedItems'
import ShareButton from '../../ShareButton/ShareButton'
Expand Down Expand Up @@ -92,7 +93,7 @@ function BazaarPriceGraph(props: Props) {
}, [graphType])

function init() {
fetchspan = (getURLSearchParam('range') as DateRange) || DEFAULT_DATE_RANGE
fetchspan = getValidatedRange(getURLSearchParam('range'))
setFetchspan(fetchspan)

graphType = (localStorage.getItem(BAZAAR_GRAPH_TYPE) as GRAPH_TYPE) || DEFAULT_GRAPH_TYPE
Expand Down Expand Up @@ -200,6 +201,10 @@ function BazaarPriceGraph(props: Props) {
function loadBazaarPrices(tag: string, fetchspan: DateRange): Promise<BazaarPrice[]> {
if (fetchspan === DateRange.ALL) {
return api.getBazaarPricesByRange(tag, new Date(0), new Date())
} else if (fetchspan === DateRange.MONTH) {
let monthAgo = new Date()
monthAgo.setMonth(monthAgo.getMonth() - 1)
return api.getBazaarPricesByRange(tag, monthAgo, new Date())
} else {
return api.getBazaarPrices(tag, fetchspan as any)
}
Expand Down Expand Up @@ -375,7 +380,7 @@ function BazaarPriceGraph(props: Props) {
<div>
<Suspense>
<ItemPriceRange
dateRangesToDisplay={[DateRange.HOUR, DateRange.DAY, DateRange.WEEK, DateRange.ALL]}
dateRangesToDisplay={[DateRange.HOUR, DateRange.DAY, DateRange.WEEK, DateRange.MONTH, DateRange.ALL]}
onRangeChange={onRangeChange}
disableAllTime={false}
item={props.item}
Expand Down
18 changes: 18 additions & 0 deletions hooks/useValidRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useState, Dispatch, SetStateAction } from 'react'
import { useSearchParams } from 'next/navigation'
import { DateRange, DEFAULT_DATE_RANGE } from '../components/ItemPriceRange/ItemPriceRange'

export const VALID_RANGES = Object.values(DateRange) as string[]

export function getValidatedRange(urlRange: string | null | undefined, defaultRange: DateRange = DEFAULT_DATE_RANGE): DateRange {
return urlRange && VALID_RANGES.includes(urlRange) ? (urlRange as DateRange) : defaultRange
}

export function useValidRange(defaultRange: DateRange = DEFAULT_DATE_RANGE): [DateRange, Dispatch<SetStateAction<DateRange>>] {
let searchParams = useSearchParams()

return useState<DateRange>(() => {
let urlRange = searchParams.get('range')
return getValidatedRange(urlRange, defaultRange)
})
}
Loading