Retain previous page's search params (#14176)

This commit is contained in:
Obada Khalili 2025-02-23 07:47:03 +02:00 committed by GitHub
parent f552667312
commit 5ac0ef6253
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 8 deletions

View File

@ -59,8 +59,8 @@ const Apps = () => {
const [activeTab, setActiveTab] = useTabSearchParams({ const [activeTab, setActiveTab] = useTabSearchParams({
defaultTab: 'all', defaultTab: 'all',
}) })
const { query: { tagIDs = [], keywords = '' }, setQuery } = useAppsQueryState() const { query: { tagIDs = [], keywords = '', isCreatedByMe: queryIsCreatedByMe = false }, setQuery } = useAppsQueryState()
const [isCreatedByMe, setIsCreatedByMe] = useState(false) const [isCreatedByMe, setIsCreatedByMe] = useState(queryIsCreatedByMe)
const [tagFilterValue, setTagFilterValue] = useState<string[]>(tagIDs) const [tagFilterValue, setTagFilterValue] = useState<string[]>(tagIDs)
const [searchKeywords, setSearchKeywords] = useState(keywords) const [searchKeywords, setSearchKeywords] = useState(keywords)
const setKeywords = useCallback((keywords: string) => { const setKeywords = useCallback((keywords: string) => {
@ -126,6 +126,12 @@ const Apps = () => {
handleTagsUpdate() handleTagsUpdate()
} }
const handleCreatedByMeChange = useCallback(() => {
const newValue = !isCreatedByMe
setIsCreatedByMe(newValue)
setQuery(prev => ({ ...prev, isCreatedByMe: newValue }))
}, [isCreatedByMe, setQuery])
return ( return (
<> <>
<div className='sticky top-0 flex justify-between items-center pt-4 px-12 pb-2 leading-[56px] bg-background-body z-10 flex-wrap gap-y-2'> <div className='sticky top-0 flex justify-between items-center pt-4 px-12 pb-2 leading-[56px] bg-background-body z-10 flex-wrap gap-y-2'>
@ -139,7 +145,7 @@ const Apps = () => {
className='mr-2' className='mr-2'
label={t('app.showMyCreatedAppsOnly')} label={t('app.showMyCreatedAppsOnly')}
isChecked={isCreatedByMe} isChecked={isCreatedByMe}
onChange={() => setIsCreatedByMe(!isCreatedByMe)} onChange={handleCreatedByMeChange}
/> />
<TagFilter type='app' value={tagFilterValue} onChange={handleTagsChange} /> <TagFilter type='app' value={tagFilterValue} onChange={handleTagsChange} />
<Input <Input

View File

@ -4,18 +4,20 @@ import { useCallback, useEffect, useMemo, useState } from 'react'
type AppsQuery = { type AppsQuery = {
tagIDs?: string[] tagIDs?: string[]
keywords?: string keywords?: string
isCreatedByMe?: boolean
} }
// Parse the query parameters from the URL search string. // Parse the query parameters from the URL search string.
function parseParams(params: ReadonlyURLSearchParams): AppsQuery { function parseParams(params: ReadonlyURLSearchParams): AppsQuery {
const tagIDs = params.get('tagIDs')?.split(';') const tagIDs = params.get('tagIDs')?.split(';')
const keywords = params.get('keywords') || undefined const keywords = params.get('keywords') || undefined
return { tagIDs, keywords } const isCreatedByMe = params.get('isCreatedByMe') === 'true'
return { tagIDs, keywords, isCreatedByMe }
} }
// Update the URL search string with the given query parameters. // Update the URL search string with the given query parameters.
function updateSearchParams(query: AppsQuery, current: URLSearchParams) { function updateSearchParams(query: AppsQuery, current: URLSearchParams) {
const { tagIDs, keywords } = query || {} const { tagIDs, keywords, isCreatedByMe } = query || {}
if (tagIDs && tagIDs.length > 0) if (tagIDs && tagIDs.length > 0)
current.set('tagIDs', tagIDs.join(';')) current.set('tagIDs', tagIDs.join(';'))
@ -26,6 +28,11 @@ function updateSearchParams(query: AppsQuery, current: URLSearchParams) {
current.set('keywords', keywords) current.set('keywords', keywords)
else else
current.delete('keywords') current.delete('keywords')
if (isCreatedByMe)
current.set('isCreatedByMe', 'true')
else
current.delete('isCreatedByMe')
} }
function useAppsQueryState() { function useAppsQueryState() {

View File

@ -1,8 +1,8 @@
'use client' 'use client'
import React, { useState } from 'react' import React, { useEffect, useState } from 'react'
import Link from 'next/link' import Link from 'next/link'
import { useSelectedLayoutSegment } from 'next/navigation' import { usePathname, useSearchParams, useSelectedLayoutSegment } from 'next/navigation'
import type { INavSelectorProps } from './nav-selector' import type { INavSelectorProps } from './nav-selector'
import NavSelector from './nav-selector' import NavSelector from './nav-selector'
import classNames from '@/utils/classnames' import classNames from '@/utils/classnames'
@ -35,6 +35,14 @@ const Nav = ({
const [hovered, setHovered] = useState(false) const [hovered, setHovered] = useState(false)
const segment = useSelectedLayoutSegment() const segment = useSelectedLayoutSegment()
const isActivated = Array.isArray(activeSegment) ? activeSegment.includes(segment!) : segment === activeSegment const isActivated = Array.isArray(activeSegment) ? activeSegment.includes(segment!) : segment === activeSegment
const pathname = usePathname()
const searchParams = useSearchParams()
const [linkLastSearchParams, setLinkLastSearchParams] = useState('')
useEffect(() => {
if (pathname === link)
setLinkLastSearchParams(searchParams.toString())
}, [pathname, searchParams])
return ( return (
<div className={` <div className={`
@ -42,7 +50,7 @@ const Nav = ({
${isActivated && 'bg-components-main-nav-nav-button-bg-active shadow-md font-semibold'} ${isActivated && 'bg-components-main-nav-nav-button-bg-active shadow-md font-semibold'}
${!curNav && !isActivated && 'hover:bg-components-main-nav-nav-button-bg-hover'} ${!curNav && !isActivated && 'hover:bg-components-main-nav-nav-button-bg-hover'}
`}> `}>
<Link href={link}> <Link href={link + (linkLastSearchParams && `?${linkLastSearchParams}`)}>
<div <div
onClick={() => setAppDetail()} onClick={() => setAppDetail()}
className={classNames(` className={classNames(`