Files
5chan/src/components/settings-modal/settings-modal.tsx
T

170 lines
7.3 KiB
TypeScript
Raw Normal View History

2025-01-31 12:31:38 +01:00
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 CryptoAddressSetting from './crypto-address-setting';
2024-05-15 21:20:14 +02:00
import CryptoWalletsSetting from './crypto-wallets-setting';
import InterfaceSettings from './interface-settings';
import MediaHostingSettings from './media-hosting-settings';
import AdvancedSettings from './advanced-settings';
2025-02-05 15:20:07 +01:00
import SubscriptionsSetting from './subscriptions-setting';
2024-05-10 17:03:33 +02:00
const SettingsModal = () => {
const { t } = useTranslation();
const location = useLocation();
const navigate = useNavigate();
const hash = location.hash.slice(1);
2025-01-31 12:31:38 +01:00
const closeModal = useCallback(() => {
const newPath = location.pathname.replace(/\/settings$/, '');
navigate(newPath);
2025-01-31 12:31:38 +01:00
}, [location.pathname, navigate]);
2025-01-30 22:05:54 +01:00
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);
2024-05-10 17:03:33 +02:00
const [showAccountSettings, setShowAccountSettings] = useState(false);
2025-02-05 15:20:07 +01:00
const [showSubscriptionsSettings, setShowSubscriptionsSettings] = useState(false);
const [showAdvancedSettings, setShowAdvancedSettings] = useState(false);
2024-05-16 22:00:22 +02:00
const [expandAll, setExpandAll] = useState(false);
const getExpandedCount = () => {
return (
Number(showInterfaceSettings) + Number(showMediaHostingSettings) + Number(showAccountSettings) + 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';
2025-02-05 15:20:07 +01:00
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) {
const expandAccount = hash === 'account-settings' || hash === 'crypto-address-settings' || hash === 'crypto-wallet-settings';
setShowInterfaceSettings(hash === 'interface-settings');
setShowMediaHostingSettings(hash === 'media-hosting-settings');
setShowAccountSettings(expandAccount);
2025-02-05 15:20:07 +01:00
setShowSubscriptionsSettings(hash === 'subscriptions-settings');
setShowAdvancedSettings(hash === 'advanced-settings');
}
}, [hash]);
2024-05-16 22:00:22 +02:00
const handleExpandAll = () => {
const newExpandState = !expandAll;
setExpandAll(newExpandState);
setShowInterfaceSettings(newExpandState);
setShowMediaHostingSettings(newExpandState);
2024-05-16 22:00:22 +02:00
setShowAccountSettings(newExpandState);
2025-02-05 15:20:07 +01:00
setShowSubscriptionsSettings(newExpandState);
setShowAdvancedSettings(newExpandState);
const baseSettingsPath = location.pathname.split('#')[0];
navigate(baseSettingsPath, { replace: true });
2024-05-16 22:00:22 +02:00
};
2024-05-10 17:03:33 +02:00
return (
<>
<div className={styles.overlay} onClick={closeModal} />
<div className={styles.settingsModal}>
<div className={styles.header}>
<span className={styles.title}>{t('settings')}</span>
<span className={styles.closeButton} title='close' onClick={closeModal} />
</div>
2024-05-16 22:00:22 +02:00
<div className={styles.expandAllSettings}>
2024-05-31 17:09:20 +02:00
[<span onClick={handleExpandAll}>{expandAll ? t('collapse_all_settings') : t('expand_all_settings')}</span>]
2024-05-16 22:00:22 +02:00
</div>
<div id='interface-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('interface-settings', showInterfaceSettings, setShowInterfaceSettings)}>
<span className={showInterfaceSettings ? styles.hideButton : styles.showButton} />
2024-05-31 16:51:22 +02:00
{t('interface')}
</label>
</div>
{showInterfaceSettings && <InterfaceSettings />}
<div id='media-hosting-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('media-hosting-settings', showMediaHostingSettings, setShowMediaHostingSettings)}>
<span className={showMediaHostingSettings ? styles.hideButton : styles.showButton} />
{t('media_hosting')}
</label>
</div>
{showMediaHostingSettings && <MediaHostingSettings />}
<div id='account-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('account-settings', showAccountSettings, setShowAccountSettings)}>
2024-05-10 17:03:33 +02:00
<span className={showAccountSettings ? styles.hideButton : styles.showButton} />
{t('bitsocial_account')}
2024-05-10 17:03:33 +02:00
</label>
</div>
{showAccountSettings && (
<>
<AccountSettings />
<div className={styles.subSectionHeader}>{t('crypto_address')}</div>
<CryptoAddressSetting />
<div className={styles.subSectionHeader}>{t('crypto_wallets')}</div>
<CryptoWalletsSetting />
</>
)}
2025-02-05 15:20:07 +01:00
<div id='subscriptions-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('subscriptions-settings', showSubscriptionsSettings, setShowSubscriptionsSettings)}>
<span className={showSubscriptionsSettings ? styles.hideButton : styles.showButton} />
{t('board_subscriptions')}
2025-02-05 15:20:07 +01:00
</label>
</div>
{showSubscriptionsSettings && <SubscriptionsSetting />}
<div id='advanced-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('advanced-settings', showAdvancedSettings, setShowAdvancedSettings)}>
<span className={showAdvancedSettings ? styles.hideButton : styles.showButton} />
{t('advanced_settings')}
2024-06-18 11:50:17 +02:00
</label>
</div>
{showAdvancedSettings && <AdvancedSettings />}
</div>
</>
);
};
2024-05-09 16:04:24 +02:00
export default SettingsModal;