Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Coflnet/hypixel-react in…
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
matthias-luger committed Jul 23, 2024
2 parents ae04c9b + 53bd11d commit 96a5a08
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 13 additions & 3 deletions components/FlipTracking/FlipTracking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Image from 'next/image'
import { ChangeEvent, useEffect, useState } from 'react'
import { Form, ListGroup } from 'react-bootstrap'
import { Item, Menu, useContextMenu } from 'react-contexify'
import { useForceUpdate, useWasAlreadyLoggedIn } from '../../utils/Hooks'
import { useForceUpdate, useQueryParamState, useWasAlreadyLoggedIn } from '../../utils/Hooks'
import { getSettingsObject, IGNORE_FLIP_TRACKING_PROFIT, setSetting } from '../../utils/SettingsUtils'
import { isClientSideRendering } from '../../utils/SSRUtils'
import styles from './FlipTracking.module.css'
Expand Down Expand Up @@ -85,8 +85,6 @@ const TRACKED_FLIP_CONTEXT_MENU_ID = 'tracked-flip-context-menu'

export function FlipTracking(props: Props) {
let [trackedFlips, setTrackedFlips] = useState<FlipTrackingFlip[]>(props.trackedFlips || [])
let [orderBy, setOrderBy] = useState<SortOption>(SORT_OPTIONS[0])
let [filterBy, setFilterBy] = useState(FILTER_OPTIONS[0])
let [ignoreProfitMap, setIgnoreProfitMap] = useState(getSettingsObject(IGNORE_FLIP_TRACKING_PROFIT, {}))
let [rangeStartDate, setRangeStartDate] = useState(new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7))
let [rangeEndDate, setRangeEndDate] = useState(new Date())
Expand All @@ -97,6 +95,18 @@ export function FlipTracking(props: Props) {
let wasAlreadyLoggedIn = useWasAlreadyLoggedIn()
let forceUpdate = useForceUpdate()

let [_orderBy, _setOrderBy] = useQueryParamState<string>('order', SORT_OPTIONS[0].value)
let [_filterBy, _setFilterBy] = useQueryParamState<string>('filter', FILTER_OPTIONS[0].value)

let orderBy = SORT_OPTIONS.find(o => o.value === _orderBy)!
let filterBy = FILTER_OPTIONS.find(f => f.value === _filterBy)!
let setOrderBy = (newValue: SortOption) => {
_setOrderBy(newValue.value)
}
let setFilterBy = (newValue: FilterOption) => {
_setFilterBy(newValue.value)
}

const { show } = useContextMenu({
id: TRACKED_FLIP_CONTEXT_MENU_ID
})
Expand Down
24 changes: 24 additions & 0 deletions utils/Hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import api from '../api/ApiHelper'
import { CUSTOM_EVENTS } from '../api/ApiTypes.d'
import { getCurrentCoflCoins, subscribeToCoflcoinChange } from './CoflCoinsUtils'
import { isClientSideRendering } from './SSRUtils'
import { getURLSearchParam } from './Parser/URLParser'
import { useRouter } from 'next/navigation'

export function useForceUpdate() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -129,3 +131,25 @@ export function useStateWithRef<T>(defaultValue: T): [T, Dispatch<SetStateAction

return [state, setState, stateRef]
}

export function useQueryParamState<T>(key: string, defaultValue: T): [T, Dispatch<SetStateAction<T>>] {
const [state, setState] = useState<T>(getDefaultValue() || defaultValue)
const router = useRouter()

function getDefaultValue(): T | undefined {
let param = getURLSearchParam(key)
if (!param) {
return undefined
}
return JSON.parse(decodeURIComponent(param)) as T
}

function _setState(newState: T) {
setState(newState)
let urlparams = new URLSearchParams(window.location.search)
urlparams.set(key, encodeURIComponent(JSON.stringify(newState)))
router.replace(`${window.location.pathname}?${urlparams.toString()}`)
}

return [state, _setState]
}

0 comments on commit 96a5a08

Please sign in to comment.