import { useCallback, useEffect, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import styles from './settings-modal.module.css'; import AccountSettings from './account-settings'; import AvatarSettings from './avatar-settings'; import CryptoAddressSetting from './crypto-address-setting'; import CryptoWalletsSetting from './crypto-wallets-setting'; import InterfaceSettings from './interface-settings'; import MediaHostingSettings from './media-hosting-settings'; import AdvancedSettings from './advanced-settings'; import SubscriptionsSetting from './subscriptions-setting'; const SettingsModal = () => { const { t } = useTranslation(); const location = useLocation(); const navigate = useNavigate(); const hash = location.hash.slice(1); const closeModal = useCallback(() => { const newPath = location.pathname.replace(/\/settings$/, ''); navigate(newPath); }, [location.pathname, navigate]); useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { closeModal(); } }; document.addEventListener('keydown', handleEscape); return () => { document.removeEventListener('keydown', handleEscape); }; }, [closeModal]); const [showInterfaceSettings, setShowInterfaceSettings] = useState(false); const [showMediaHostingSettings, setShowMediaHostingSettings] = useState(false); const [showAccountSettings, setShowAccountSettings] = useState(false); const [showAvatarSettings, setShowAvatarSettings] = useState(false); const [showCryptoAddressSetting, setShowCryptoAddressSetting] = useState(false); const [showCryptoWalletSettings, setShowCryptoWalletSettings] = useState(false); const [showSubscriptionsSettings, setShowSubscriptionsSettings] = useState(false); const [showAdvancedSettings, setShowAdvancedSettings] = useState(false); const [expandAll, setExpandAll] = useState(false); const getExpandedCount = () => { return ( Number(showInterfaceSettings) + Number(showMediaHostingSettings) + Number(showAccountSettings) + Number(showAvatarSettings) + Number(showCryptoAddressSetting) + Number(showCryptoWalletSettings) + Number(showSubscriptionsSettings) + Number(showAdvancedSettings) ); }; const getExpandedCategoryId = (excludeCategoryId?: string) => { if (showInterfaceSettings && 'interface-settings' !== excludeCategoryId) return 'interface-settings'; if (showMediaHostingSettings && 'media-hosting-settings' !== excludeCategoryId) return 'media-hosting-settings'; if (showAccountSettings && 'account-settings' !== excludeCategoryId) return 'account-settings'; if (showAvatarSettings && 'avatar-settings' !== excludeCategoryId) return 'avatar-settings'; if (showCryptoAddressSetting && 'crypto-address-settings' !== excludeCategoryId) return 'crypto-address-settings'; if (showCryptoWalletSettings && 'crypto-wallet-settings' !== excludeCategoryId) return 'crypto-wallet-settings'; if (showSubscriptionsSettings && 'subscriptions-settings' !== excludeCategoryId) return 'subscriptions-settings'; if (showAdvancedSettings && 'advanced-settings' !== excludeCategoryId) return 'advanced-settings'; return null; }; const handleCategoryClick = (categoryId: string, isShowing: boolean, setShowing: (value: boolean) => void) => { const newState = !isShowing; setShowing(newState); const currentPath = location.pathname; const baseSettingsPath = currentPath.split('#')[0]; const currentExpandedCount = getExpandedCount(); if (newState) { if (currentExpandedCount === 0) { navigate(`${baseSettingsPath}#${categoryId}`, { replace: true }); } else { navigate(baseSettingsPath, { replace: true }); } } else { if (currentExpandedCount === 1) { navigate(baseSettingsPath, { replace: true }); } else if (currentExpandedCount === 2) { const remainingCategory = getExpandedCategoryId(categoryId); if (remainingCategory) { navigate(`${baseSettingsPath}#${remainingCategory}`, { replace: true }); } } } }; useEffect(() => { if (hash) { setShowInterfaceSettings(hash === 'interface-settings'); setShowMediaHostingSettings(hash === 'media-hosting-settings'); setShowAccountSettings(hash === 'account-settings'); setShowAvatarSettings(hash === 'avatar-settings'); setShowCryptoAddressSetting(hash === 'crypto-address-settings'); setShowCryptoWalletSettings(hash === 'crypto-wallet-settings'); setShowSubscriptionsSettings(hash === 'subscriptions-settings'); setShowAdvancedSettings(hash === 'advanced-settings'); } }, [hash]); const handleExpandAll = () => { const newExpandState = !expandAll; setExpandAll(newExpandState); setShowInterfaceSettings(newExpandState); setShowMediaHostingSettings(newExpandState); setShowAccountSettings(newExpandState); setShowAvatarSettings(newExpandState); setShowCryptoAddressSetting(newExpandState); setShowCryptoWalletSettings(newExpandState); setShowSubscriptionsSettings(newExpandState); setShowAdvancedSettings(newExpandState); const baseSettingsPath = location.pathname.split('#')[0]; navigate(baseSettingsPath, { replace: true }); }; return ( <>