Skip to content

Commit

Permalink
Adds hash routing to settings page. (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
bLopata authored Jan 8, 2025
1 parent 96c4345 commit e65a315
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions www/app/settings/SettingsLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import SubscriptionSettings from '@/components/settings/SubscriptionSettings';
import { SecuritySettings } from '@/components/settings/SecuritySettings';
import { AccountSettings } from '@/components/settings/AccountSettings';
Expand All @@ -18,14 +18,29 @@ export default function SettingsLayout({
subscription,
products,
}: SettingsProps) {
const [activeTab, setActiveTab] = useState('account');

const navItems = [
const navItems = useMemo(() => [
{ id: 'account', label: 'Account' },
{ id: 'security', label: 'Security' },
{ id: 'subscription', label: 'Subscription' },
{ id: 'support', label: 'Support' },
];
], []);

const [activeTab, setActiveTab] = useState('account'); // Default to 'account' initially

useEffect(() => {
const handleHashChange = () => {
const hash = window.location.hash.replace('#', '');
if (navItems.some(item => item.id === hash)) {
setActiveTab(hash);
}
};

// Check initial hash
handleHashChange();

window.addEventListener('hashchange', handleHashChange);
return () => window.removeEventListener('hashchange', handleHashChange);
}, [navItems]);

return (
<div className={`flex-1 flex flex-col bg-background text-foreground`}>
Expand All @@ -37,12 +52,14 @@ export default function SettingsLayout({
{navItems.map((item) => (
<li key={item.id} className="mb-2">
<button
onClick={() => setActiveTab(item.id)}
className={`w-full text-left p-2 rounded transition-colors ${
activeTab === item.id
onClick={() => {
setActiveTab(item.id);
window.location.hash = item.id;
}}
className={`w-full text-left p-2 rounded transition-colors ${activeTab === item.id
? 'bg-accent text-foreground'
: 'hover:bg-gray-200 hover:text-gray-900 dark:hover:bg-neon-green/20 dark:hover:text-neon-green'
}`}
}`}
>
{item.label}
</button>
Expand Down

0 comments on commit e65a315

Please sign in to comment.