| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | import { usePathname, useRouter, useSearchParams } from 'next/navigation' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { useState } from 'react' | 
					
						
							| 
									
										
										
										
											2024-02-26 12:52:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type UseTabSearchParamsOptions = { | 
					
						
							|  |  |  |   defaultTab: string | 
					
						
							|  |  |  |   routingBehavior?: 'push' | 'replace' | 
					
						
							|  |  |  |   searchParamName?: string | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   disableSearchParams?: boolean | 
					
						
							| 
									
										
										
										
											2024-02-26 12:52:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Custom hook to manage tab state via URL search parameters in a Next.js application. | 
					
						
							|  |  |  |  * This hook allows for syncing the active tab with the browser's URL, enabling bookmarking and sharing of URLs with a specific tab activated. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {UseTabSearchParamsOptions} options Configuration options for the hook: | 
					
						
							|  |  |  |  * - `defaultTab`: The tab to default to when no tab is specified in the URL. | 
					
						
							|  |  |  |  * - `routingBehavior`: Optional. Determines how changes to the active tab update the browser's history ('push' or 'replace'). Default is 'push'. | 
					
						
							|  |  |  |  * - `searchParamName`: Optional. The name of the search parameter that holds the tab state in the URL. Default is 'category'. | 
					
						
							|  |  |  |  * @returns A tuple where the first element is the active tab and the second element is a function to set the active tab. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export const useTabSearchParams = ({ | 
					
						
							|  |  |  |   defaultTab, | 
					
						
							|  |  |  |   routingBehavior = 'push', | 
					
						
							|  |  |  |   searchParamName = 'category', | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   disableSearchParams = false, | 
					
						
							| 
									
										
										
										
											2024-02-26 12:52:59 +08:00
										 |  |  | }: UseTabSearchParamsOptions) => { | 
					
						
							| 
									
										
										
										
											2025-04-15 17:05:50 +08:00
										 |  |  |   const pathnameFromHook = usePathname() | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  |   const router = useRouter() | 
					
						
							|  |  |  |   const pathName = pathnameFromHook || window?.location?.pathname | 
					
						
							| 
									
										
										
										
											2024-02-26 12:52:59 +08:00
										 |  |  |   const searchParams = useSearchParams() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const [activeTab, setTab] = useState<string>( | 
					
						
							|  |  |  |     !disableSearchParams | 
					
						
							|  |  |  |       ? (searchParams.get(searchParamName) || defaultTab) | 
					
						
							|  |  |  |       : defaultTab, | 
					
						
							|  |  |  |   ) | 
					
						
							| 
									
										
										
										
											2024-02-26 12:52:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const setActiveTab = (newActiveTab: string) => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     setTab(newActiveTab) | 
					
						
							|  |  |  |     if (disableSearchParams) | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  |     router[`${routingBehavior}`](`${pathName}?${searchParamName}=${newActiveTab}`) | 
					
						
							| 
									
										
										
										
											2024-02-26 12:52:59 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return [activeTab, setActiveTab] as const | 
					
						
							|  |  |  | } |