Skip to content

Commit 75a106a

Browse files
committed
Add url param saving to Stream and Project listing as well
1 parent 6b123d1 commit 75a106a

File tree

6 files changed

+95
-26
lines changed

6 files changed

+95
-26
lines changed

src/components/ActionBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ProjectFilter } from '~/types'
2323
import { useCurrentChainSymbolicName } from '~/utils/chains'
2424
import { Route as R, routeOptions } from '~/utils/routes'
2525

26-
enum TabOption {
26+
export enum TabOption {
2727
Any = 'all',
2828
Owned = 'my',
2929
}
@@ -32,7 +32,7 @@ export function isOwnedTabOption(value: unknown) {
3232
return value === TabOption.Owned
3333
}
3434

35-
function isTabOption(value: unknown): value is TabOption {
35+
export function isTabOption(value: unknown): value is TabOption {
3636
return value === TabOption.Any || value === TabOption.Owned
3737
}
3838

src/hooks/useUrlParams.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ export function useUrlParams<T extends string>(params: UrlParamConfig<T>[]) {
3232
const newParams = new URLSearchParams(prevParams)
3333

3434
params.forEach(({ param, value, defaultValue }) => {
35-
if (value !== defaultValue) {
36-
newParams.set(param, value || defaultValue)
35+
if (value === '' || value === undefined) {
36+
newParams.delete(param)
37+
} else if (value !== defaultValue) {
38+
newParams.set(param, value)
3739
} else {
3840
newParams.delete(param)
3941
}

src/pages/OperatorsPage.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ export const OperatorsPage = () => {
4848
const wallet = useWalletAccount()
4949
const isWalletLoading = useIsWalletLoading()
5050

51-
const { orderBy, orderDirection, setOrder } = useTableOrder<string>({
52-
orderBy: DEFAULT_ORDER_BY,
53-
orderDirection: DEFAULT_ORDER_DIRECTION,
54-
})
55-
5651
const [params] = useSearchParams()
5752
const tab = params.get('tab')
5853
const selectedTab = isTabOption(tab) ? tab : DEFAULT_TAB
5954
const [searchQuery, setSearchQuery] = useState(params.get('search') || '')
6055

56+
const { orderBy, orderDirection, setOrder } = useTableOrder<string>({
57+
orderBy: params.get('orderBy') || DEFAULT_ORDER_BY,
58+
orderDirection:
59+
(params.get('orderDir') as OrderDirection) || DEFAULT_ORDER_DIRECTION,
60+
})
61+
6162
useUrlParams([
6263
{
6364
param: 'tab',

src/pages/ProjectListingPage.tsx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { keepPreviousData, useInfiniteQuery } from '@tanstack/react-query'
22
import isEqual from 'lodash/isEqual'
33
import React, { useReducer } from 'react'
44
import { useSearchParams } from 'react-router-dom'
5-
import ActionBar, { isOwnedTabOption } from '~/components/ActionBar'
5+
import ActionBar, {
6+
isOwnedTabOption,
7+
isTabOption,
8+
TabOption,
9+
} from '~/components/ActionBar'
610
import Layout from '~/components/Layout'
711
import { MaxSearchPhraseLength, Minute } from '~/consts'
812
import { Projects, ProjectsContainer } from '~/marketplace/components/Projects'
@@ -13,12 +17,13 @@ import useModal from '~/shared/hooks/useModal'
1317
import { useWalletAccount } from '~/shared/stores/wallet'
1418
import { ProjectFilter } from '~/types'
1519
import { useCurrentChainId } from '~/utils/chains'
20+
import { useUrlParams } from '~/hooks/useUrlParams'
21+
import { TheGraph } from '~/shared/types'
1622

17-
const DefaultFilter: ProjectFilter = {
18-
search: '',
19-
}
20-
21-
const PageSize = 16
23+
const PAGE_SIZE = 20
24+
const DEFAULT_TAB = TabOption.Any
25+
const DEFAULT_SEARCH = ''
26+
const DEFAULT_TYPE = ''
2227

2328
function filterReducer(
2429
state: ProjectFilter,
@@ -32,13 +37,38 @@ function filterReducer(
3237
}
3338

3439
export default function ProjectListingPage() {
40+
const [params] = useSearchParams()
41+
42+
const DefaultFilter: ProjectFilter = {
43+
search: params.get('search') || DEFAULT_SEARCH,
44+
type: (params.get('type') as TheGraph.ProjectType) || DEFAULT_TYPE,
45+
}
46+
3547
const [filter, setFilter] = useReducer(filterReducer, DefaultFilter)
3648

3749
const account = useWalletAccount()
3850

39-
const [params] = useSearchParams()
51+
const tab = params.get('tab')
52+
const selectedTab = isTabOption(tab) ? tab : DEFAULT_TAB
53+
const owner = isOwnedTabOption(selectedTab) ? account : undefined
4054

41-
const owner = isOwnedTabOption(params.get('tab')) ? account : undefined
55+
useUrlParams([
56+
{
57+
param: 'tab',
58+
value: selectedTab,
59+
defaultValue: DEFAULT_TAB,
60+
},
61+
{
62+
param: 'search',
63+
value: filter.search,
64+
defaultValue: DEFAULT_SEARCH,
65+
},
66+
{
67+
param: 'type',
68+
value: filter.type,
69+
defaultValue: DEFAULT_TYPE,
70+
},
71+
])
4272

4373
const { api: createProductModal } = useModal('marketplace.createProduct')
4474

@@ -51,7 +81,7 @@ export default function ProjectListingPage() {
5181

5282
const params = {
5383
chainId: currentChainId,
54-
first: PageSize,
84+
first: PAGE_SIZE,
5585
owner,
5686
projectType,
5787
skip,

src/pages/SponsorshipsPage.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from '~/utils/chains'
2929
import { Route as R, routeOptions } from '~/utils/routes'
3030
import { useUrlParams } from '~/hooks/useUrlParams'
31+
import { OrderDirection } from '~/types'
3132

3233
enum TabOption {
3334
AllSponsorships = 'all',
@@ -61,8 +62,9 @@ export const SponsorshipsPage = () => {
6162
const isWalletLoading = useIsWalletLoading()
6263

6364
const { orderBy, orderDirection, setOrder } = useTableOrder<string>({
64-
orderBy: DEFAULT_ORDER_BY,
65-
orderDirection: DEFAULT_ORDER_DIRECTION,
65+
orderBy: params.get('orderBy') || DEFAULT_ORDER_BY,
66+
orderDirection:
67+
(params.get('orderDir') as OrderDirection) || DEFAULT_ORDER_DIRECTION,
6668
})
6769

6870
const tab = params.get('tab')

src/pages/StreamsPage.tsx

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ import { useWalletAccount } from '~/shared/stores/wallet'
2727
import { COLORS, TABLET } from '~/shared/utils/styled'
2828
import { useCurrentChainFullName, useCurrentChainSymbolicName } from '~/utils/chains'
2929
import { Route as R, routeOptions } from '~/utils/routes'
30+
import { useUrlParams } from '~/hooks/useUrlParams'
31+
import { OrderDirection } from '~/types'
3032

31-
export function StreamsPage() {
32-
const [search, setSearch] = useState('')
33+
const DEFAULT_ORDER_BY = 'peerCount'
34+
const DEFAULT_ORDER_DIRECTION = 'desc'
35+
const DEFAULT_TAB = StreamsTabOption.All
36+
const DEFAULT_SEARCH = ''
3337

38+
export function StreamsPage() {
3439
const [params] = useSearchParams()
3540

41+
const [search, setSearch] = useState(params.get('search') || DEFAULT_SEARCH)
42+
3643
const tabParam = params.get('tab')
3744

38-
const tab = isStreamsTabOption(tabParam) ? tabParam : StreamsTabOption.All
45+
const tab = isStreamsTabOption(tabParam) ? tabParam : DEFAULT_TAB
3946

4047
const navigate = useNavigate()
4148

@@ -55,10 +62,37 @@ export function StreamsPage() {
5562
)
5663

5764
const {
58-
orderBy = 'peerCount',
59-
orderDirection = 'desc',
65+
orderBy = DEFAULT_ORDER_BY,
66+
orderDirection = DEFAULT_ORDER_DIRECTION,
6067
setOrder,
61-
} = useTableOrder<StreamsOrderBy>()
68+
} = useTableOrder<StreamsOrderBy>({
69+
orderBy: (params.get('orderBy') as StreamsOrderBy) || DEFAULT_ORDER_BY,
70+
orderDirection:
71+
(params.get('orderDir') as OrderDirection) || DEFAULT_ORDER_DIRECTION,
72+
})
73+
74+
useUrlParams([
75+
{
76+
param: 'tab',
77+
value: tab,
78+
defaultValue: DEFAULT_TAB,
79+
},
80+
{
81+
param: 'search',
82+
value: search,
83+
defaultValue: DEFAULT_SEARCH,
84+
},
85+
{
86+
param: 'orderBy',
87+
value: orderBy,
88+
defaultValue: DEFAULT_ORDER_BY,
89+
},
90+
{
91+
param: 'orderDir',
92+
value: orderDirection,
93+
defaultValue: DEFAULT_ORDER_DIRECTION,
94+
},
95+
])
6296

6397
const streamsQuery = useStreamsQuery({
6498
orderBy,

0 commit comments

Comments
 (0)