33/* eslint-disable @typescript-eslint/no-deprecated */
44/* eslint-disable @typescript-eslint/no-floating-promises */
55import { Menu , Transition } from '@headlessui/react'
6- import {
7- useQueryState ,
8- parseAsStringLiteral ,
9- } from '@pythnetwork/react-hooks/nuqs'
106import { useWallet } from '@solana/wallet-adapter-react'
117import type { TransactionAccount } from '@sqds/mesh/lib/types'
128import { useRouter } from 'next/router'
13- import { useContext , useEffect , useState , useMemo , Fragment } from 'react'
9+ import { useContext , useEffect , useState , useMemo , Fragment , useCallback } from 'react'
1410
1511import { Proposal } from './Proposal'
1612import { ProposalRow } from './ProposalRow'
@@ -20,6 +16,7 @@ import { useMultisigContext } from '../../../contexts/MultisigContext'
2016import ClusterSwitch from '../../ClusterSwitch'
2117import { Select } from '../../Select'
2218import Loadbar from '../../loaders/Loadbar'
19+ import { usePathname , useSearchParams } from 'next/navigation'
2320
2421type ProposalType = 'priceFeed' | 'governance' | 'lazer'
2522
@@ -65,15 +62,15 @@ const Proposals = () => {
6562 const router = useRouter ( )
6663 const [ currentProposal , setCurrentProposal ] = useState < TransactionAccount > ( )
6764 const [ currentProposalPubkey , setCurrentProposalPubkey ] = useState < string > ( )
68- const [ statusFilter , setStatusFilter ] = useQueryState (
65+ const [ statusFilter , setStatusFilter ] = useSeacrhParamFromEnum (
6966 'status' ,
70- parseAsStringLiteral ( PROPOSAL_STATUS_FILTERS ) . withDefault (
71- DEFAULT_PROPOSAL_STATUS_FILTER
72- )
67+ PROPOSAL_STATUS_FILTERS ,
68+ DEFAULT_PROPOSAL_STATUS_FILTER
7369 )
74- const [ voteStatus , setVoteStatus ] = useQueryState (
70+ const [ voteStatus , setVoteStatus ] = useSeacrhParamFromEnum (
7571 'voteStatus' ,
76- parseAsStringLiteral ( VOTE_STATUSES ) . withDefault ( DEFAULT_VOTE_STATUS )
72+ VOTE_STATUSES ,
73+ DEFAULT_VOTE_STATUS
7774 )
7875 const { cluster } = useContext ( ClusterContext )
7976 const { publicKey : walletPublicKey } = useWallet ( )
@@ -407,3 +404,29 @@ const Proposals = () => {
407404}
408405
409406export default Proposals
407+
408+ const useSeacrhParamFromEnum = < T extends string > ( param : string , validValues : readonly T [ ] , defaultValue : T ) => {
409+ const router = useRouter ( ) ;
410+ const pathname = usePathname ( ) ;
411+ const searchParams = useSearchParams ( ) ;
412+ const value = useMemo (
413+ ( ) => {
414+ const current = searchParams . get ( param ) ;
415+ return isInEnum ( validValues , current ) ? current : defaultValue ;
416+ } ,
417+ [ searchParams , param , validValues , defaultValue ]
418+ ) ;
419+ const updateValue = useCallback (
420+ ( newValue : T ) => {
421+ const newSearchParams = new URLSearchParams ( searchParams . toString ( ) ) ;
422+ newSearchParams . set ( param , newValue ) ;
423+ router . push ( `${ pathname } ?${ newSearchParams . toString ( ) } ` )
424+ } ,
425+ [ searchParams , pathname , router , param ]
426+ ) ;
427+
428+ return [ value , updateValue ] as const ;
429+ }
430+
431+ const isInEnum = < T , > ( values : readonly T [ ] , value : unknown ) : value is T =>
432+ values . includes ( value as T ) ;
0 commit comments