From 42422ccdcd15fda903ffd77910858df0b5191e08 Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Mon, 24 Mar 2025 13:56:12 +0530 Subject: [PATCH] (feat:settings) routes for tab --- frontend/src/App.tsx | 2 +- frontend/src/settings/index.tsx | 93 ++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 64c4c486..33c66bd1 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -63,7 +63,7 @@ export default function App() { > } /> } /> - } /> + } /> } /> } /> diff --git a/frontend/src/settings/index.tsx b/frontend/src/settings/index.tsx index cd504858..1af2f296 100644 --- a/frontend/src/settings/index.tsx +++ b/frontend/src/settings/index.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; import { useDispatch, useSelector } from 'react-redux'; +import { useLocation, useNavigate, Routes, Route, Navigate } from 'react-router-dom'; import userService from '../api/services/userService'; import SettingsBar from '../components/SettingsBar'; @@ -24,10 +25,42 @@ import Widgets from './Widgets'; export default function Settings() { const dispatch = useDispatch(); const { t } = useTranslation(); - const [activeTab, setActiveTab] = React.useState(t('settings.general.label')); - const [widgetScreenshot, setWidgetScreenshot] = React.useState( - null, - ); + const navigate = useNavigate(); + const location = useLocation(); + const [widgetScreenshot, setWidgetScreenshot] = React.useState(null); + + const getActiveTabFromPath = () => { + const path = location.pathname; + if (path.includes('/settings/documents')) return t('settings.documents.label'); + if (path.includes('/settings/apikeys')) return t('settings.apiKeys.label'); + if (path.includes('/settings/analytics')) return t('settings.analytics.label'); + if (path.includes('/settings/logs')) return t('settings.logs.label'); + if (path.includes('/settings/tools')) return t('settings.tools.label'); + if (path.includes('/settings/widgets')) return 'Widgets'; + return t('settings.general.label'); + }; + + const [activeTab, setActiveTab] = React.useState(getActiveTabFromPath()); + + const handleTabChange = (tab: string) => { + setActiveTab(tab); + if (tab === t('settings.general.label')) navigate('/settings'); + else if (tab === t('settings.documents.label')) navigate('/settings/documents'); + else if (tab === t('settings.apiKeys.label')) navigate('/settings/apikeys'); + else if (tab === t('settings.analytics.label')) navigate('/settings/analytics'); + else if (tab === t('settings.logs.label')) navigate('/settings/logs'); + else if (tab === t('settings.tools.label')) navigate('/settings/tools'); + else if (tab === 'Widgets') navigate('/settings/widgets'); + }; + + React.useEffect(() => { + setActiveTab(getActiveTabFromPath()); + }, [location.pathname]); + + React.useEffect(() => { + const newActiveTab = getActiveTabFromPath(); + setActiveTab(newActiveTab); + }, [i18n.language]); const token = useSelector(selectToken); const documents = useSelector(selectSourceDocs); @@ -59,54 +92,32 @@ export default function Settings() { .catch((error) => console.error(error)); }; - React.useEffect(() => { - setActiveTab(t('settings.general.label')); - }, [i18n.language]); return (

{t('settings.label')}

- - {renderActiveTab()} - - {/* {activeTab === 'Widgets' && ( - - )} */} -
- ); - - function renderActiveTab() { - switch (activeTab) { - case t('settings.general.label'): - return ; - case t('settings.documents.label'): - return ( + handleTabChange(tab as string)} /> + + } /> + - ); - case 'Widgets': - return ( + } /> + } /> + } /> + } /> + } /> + - ); - case t('settings.apiKeys.label'): - return ; - case t('settings.analytics.label'): - return ; - case t('settings.logs.label'): - return ; - case t('settings.tools.label'): - return ; - default: - return null; - } - } + } /> + } /> + + + ); }