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

162 lines
6.9 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]);
2024-05-16 22:00:22 +02:00
const [expandAll, setExpandAll] = useState(false);
const expandAccount = hash === 'account-settings' || hash === 'crypto-address-settings' || hash === 'crypto-wallet-settings';
const showInterfaceSettings = expandAll || hash === 'interface-settings';
const showMediaHostingSettings = expandAll || hash === 'media-hosting-settings';
const showAccountSettings = expandAll || expandAccount;
const showSubscriptionsSettings = expandAll || hash === 'subscriptions-settings';
const showAdvancedSettings = expandAll || hash === 'advanced-settings';
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) => {
const newState = !isShowing;
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 });
}
}
}
};
2024-05-16 22:00:22 +02:00
const handleExpandAll = () => {
setExpandAll((prev) => !prev);
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
const handleKeyDown = (handler: () => void) => (e: React.KeyboardEvent) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
handler();
}
};
return (
<>
<div className={styles.overlay} role='button' tabIndex={0} onClick={closeModal} onKeyDown={handleKeyDown(closeModal)} />
<div className={styles.settingsModal}>
<div className={styles.header}>
<span className={styles.title}>{t('settings')}</span>
<span className={styles.closeButton} role='button' tabIndex={0} title='close' onClick={closeModal} onKeyDown={handleKeyDown(closeModal)} />
</div>
2024-05-16 22:00:22 +02:00
<div className={styles.expandAllSettings}>
[
<span role='button' tabIndex={0} onClick={handleExpandAll} onKeyDown={handleKeyDown(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)}>
<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)}>
<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)}>
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)}>
2025-02-05 15:20:07 +01:00
<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)}>
<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;