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

202 lines
9.4 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';
2024-07-12 20:22:09 +02:00
import AvatarSettings from './avatar-settings';
import BlockedAddressesSetting from './blocked-addresses-setting';
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 P2pOptions from './p2p-options';
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);
2024-05-10 17:03:33 +02:00
const [showAccountSettings, setShowAccountSettings] = useState(false);
2024-07-12 20:22:09 +02:00
const [showAvatarSettings, setShowAvatarSettings] = useState(false);
const [showCryptoAddressSetting, setShowCryptoAddressSetting] = useState(false);
const [showCryptoWalletSettings, setShowCryptoWalletSettings] = useState(false);
2025-02-05 15:20:07 +01:00
const [showSubscriptionsSettings, setShowSubscriptionsSettings] = useState(false);
const [showBlockedAddressesSetting, setShowBlockedAddressesSetting] = useState(false);
const [showP2pOptionsSettings, setShowP2pOptionsSettings] = useState(false);
2024-05-16 22:00:22 +02:00
const [expandAll, setExpandAll] = useState(false);
const getExpandedCount = () => {
return (
Number(showInterfaceSettings) +
Number(showAccountSettings) +
Number(showAvatarSettings) +
Number(showCryptoAddressSetting) +
Number(showCryptoWalletSettings) +
2025-02-05 15:20:07 +01:00
Number(showSubscriptionsSettings) +
Number(showBlockedAddressesSetting) +
Number(showP2pOptionsSettings)
);
};
const getExpandedCategoryId = (excludeCategoryId?: string) => {
if (showInterfaceSettings && 'interface-settings' !== excludeCategoryId) return 'interface-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';
2025-02-05 15:20:07 +01:00
if (showSubscriptionsSettings && 'subscriptions-settings' !== excludeCategoryId) return 'subscriptions-settings';
if (showBlockedAddressesSetting && 'blocked-addresses-settings' !== excludeCategoryId) return 'blocked-addresses-settings';
if (showP2pOptionsSettings && 'p2p-options-settings' !== excludeCategoryId) return 'p2p-options-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');
setShowAccountSettings(hash === 'account-settings');
setShowAvatarSettings(hash === 'avatar-settings');
setShowCryptoAddressSetting(hash === 'crypto-address-settings');
setShowCryptoWalletSettings(hash === 'crypto-wallet-settings');
2025-02-05 15:20:07 +01:00
setShowSubscriptionsSettings(hash === 'subscriptions-settings');
setShowBlockedAddressesSetting(hash === 'blocked-addresses-settings');
setShowP2pOptionsSettings(hash === 'p2p-options-settings');
}
}, [hash]);
2024-05-16 22:00:22 +02:00
const handleExpandAll = () => {
const newExpandState = !expandAll;
setExpandAll(newExpandState);
setShowInterfaceSettings(newExpandState);
2024-05-16 22:00:22 +02:00
setShowAccountSettings(newExpandState);
2024-07-12 20:22:09 +02:00
setShowAvatarSettings(newExpandState);
2024-05-16 22:00:22 +02:00
setShowCryptoAddressSetting(newExpandState);
setShowCryptoWalletSettings(newExpandState);
2025-02-05 15:20:07 +01:00
setShowSubscriptionsSettings(newExpandState);
setShowBlockedAddressesSetting(newExpandState);
setShowP2pOptionsSettings(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='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 id='avatar-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('avatar-settings', showAvatarSettings, setShowAvatarSettings)}>
2024-07-12 20:22:09 +02:00
<span className={showAvatarSettings ? styles.hideButton : styles.showButton} />
{t('avatar')}
</label>
</div>
{showAvatarSettings && <AvatarSettings />}
<div id='crypto-address-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('crypto-address-settings', showCryptoAddressSetting, setShowCryptoAddressSetting)}>
<span className={showCryptoAddressSetting ? styles.hideButton : styles.showButton} />
2024-05-15 22:12:32 +02:00
{t('crypto_address')}
</label>
</div>
{showCryptoAddressSetting && <CryptoAddressSetting />}
<div id='crypto-wallet-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('crypto-wallet-settings', showCryptoWalletSettings, setShowCryptoWalletSettings)}>
<span className={showCryptoWalletSettings ? styles.hideButton : styles.showButton} />
2024-05-15 22:12:32 +02:00
{t('crypto_wallets')}
</label>
</div>
2024-05-15 21:20:14 +02:00
{showCryptoWalletSettings && <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('subscriptions')}
</label>
</div>
{showSubscriptionsSettings && <SubscriptionsSetting />}
<div id='blocked-addresses-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('blocked-addresses-settings', showBlockedAddressesSetting, setShowBlockedAddressesSetting)}>
<span className={showBlockedAddressesSetting ? styles.hideButton : styles.showButton} />
{t('blocked_addresses')}
</label>
</div>
{showBlockedAddressesSetting && <BlockedAddressesSetting />}
<div id='p2p-options-settings' className={`${styles.setting} ${styles.category}`}>
<label onClick={() => handleCategoryClick('p2p-options-settings', showP2pOptionsSettings, setShowP2pOptionsSettings)}>
<span className={showP2pOptionsSettings ? styles.hideButton : styles.showButton} />
{t('p2p_options')}
2024-06-18 11:50:17 +02:00
</label>
</div>
{showP2pOptionsSettings && <P2pOptions />}
</div>
</>
);
};
2024-05-09 16:04:24 +02:00
export default SettingsModal;