11"use client" ;
22
33import { useState , useEffect } from "react" ;
4- import { useSearchParams } from "next/navigation" ;
4+ import { usePathname , useRouter } from "next/navigation" ;
55import Link from "next/link" ;
66import ProfileTab from "@/app/_components/FeatureComponents/SettingsPage/ProfileTab" ;
77import PreferencesTab from "@/app/_components/FeatureComponents/SettingsPage/PreferencesTab" ;
@@ -35,8 +35,8 @@ type Tab =
3535
3636function SettingsPageContent ( ) {
3737 const { user, torrentPreferences } = usePreferences ( ) ;
38- const searchParams = useSearchParams ( ) ;
39- const [ activeTab , setActiveTab ] = useState < Tab > ( "profile" ) ;
38+ const pathname = usePathname ( ) ;
39+ const router = useRouter ( ) ;
4040 const [ isUploadModalOpen , setIsUploadModalOpen ] = useState ( false ) ;
4141 const { toggleSidebar } = useSidebar ( ) ;
4242 const torrentsEnabled = torrentPreferences ?. enabled ?? false ;
@@ -45,31 +45,25 @@ function SettingsPageContent() {
4545 setIsUploadModalOpen ( true ) ;
4646 } ;
4747
48+ const validTabs : Tab [ ] = [
49+ "profile" ,
50+ "preferences" ,
51+ "upload" ,
52+ "encryption" ,
53+ ...( torrentsEnabled ? [ "torrents" as Tab ] : [ ] ) ,
54+ ...( user ?. isAdmin ? [ "users" as Tab , "audit-logs" as Tab ] : [ ] ) ,
55+ ] ;
56+
57+ const tabFromPath = pathname . split ( "/" ) [ 2 ] as Tab | undefined ;
58+ const activeTab : Tab =
59+ tabFromPath && validTabs . includes ( tabFromPath ) ? tabFromPath : "profile" ;
60+
4861 useEffect ( ( ) => {
4962 if ( activeTab === "torrents" && ! torrentsEnabled ) {
50- setActiveTab ( " profile") ;
63+ router . replace ( "/settings/ profile") ;
5164 }
5265 } , [ torrentsEnabled , activeTab ] ) ;
5366
54- useEffect ( ( ) => {
55- const tabParam = searchParams . get ( "tab" ) ;
56- if ( tabParam ) {
57- const validTabs : Tab [ ] = [
58- "profile" ,
59- "preferences" ,
60- "encryption" ,
61- "users" ,
62- "audit-logs" ,
63- ...( torrentsEnabled ? [ "torrents" as Tab ] : [ ] ) ,
64- ] ;
65- if ( validTabs . includes ( tabParam as Tab ) ) {
66- setActiveTab ( tabParam as Tab ) ;
67- } else if ( tabParam === "torrents" && ! torrentsEnabled ) {
68- setActiveTab ( "profile" ) ;
69- }
70- }
71- } , [ searchParams , torrentsEnabled ] ) ;
72-
7367 if ( ! user ) {
7468 return null ;
7569 }
@@ -116,7 +110,7 @@ function SettingsPageContent() {
116110 < SettingsSidebar
117111 tabs = { tabs }
118112 activeTab = { activeTab }
119- onTabChange = { setActiveTab }
113+ onTabChange = { ( tab ) => router . push ( `/settings/ ${ tab } ` ) }
120114 />
121115 }
122116 >
@@ -125,7 +119,7 @@ function SettingsPageContent() {
125119 < div className = "lg:hidden mb-6" >
126120 < Select
127121 value = { activeTab }
128- onChange = { ( e ) => setActiveTab ( e . target . value as Tab ) }
122+ onChange = { ( e ) => router . push ( `/settings/ ${ e . target . value } ` ) }
129123 >
130124 { tabs . map ( ( tab ) => (
131125 < option key = { tab . id } value = { tab . id } >
0 commit comments