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

201 lines
7.5 KiB
TypeScript
Raw Normal View History

import { useState } from 'react';
2024-05-09 16:04:24 +02:00
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import styles from './settings-modal.module.css';
import useTheme from '../../hooks/use-theme';
import packageJson from '../../../package.json';
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';
2024-05-09 16:04:24 +02:00
const commitRef = process.env.REACT_APP_COMMIT_REF;
const isElectron = window.isElectron === true;
const CheckForUpdates = () => {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const checkForUpdates = async () => {
try {
setLoading(true);
const packageRes = await fetch('https://raw.githubusercontent.com/plebbit/plebchan/master/package.json', { cache: 'no-cache' });
const packageData = await packageRes.json();
let updateAvailable = false;
if (packageJson.version !== packageData.version) {
const newVersionText = t('new_stable_version', { newVersion: packageData.version, oldVersion: packageJson.version });
const updateActionText = isElectron
? t('download_latest_desktop', { link: 'https://github.com/plebbit/plebchan/releases/latest', interpolation: { escapeValue: false } })
: t('refresh_to_update');
alert(newVersionText + ' ' + updateActionText);
updateAvailable = true;
}
if (commitRef && commitRef.length > 0) {
const commitRes = await fetch('https://api.github.com/repos/plebbit/plebchan/commits?per_page=1&sha=development', { cache: 'no-cache' });
const commitData = await commitRes.json();
const latestCommitHash = commitData[0].sha;
if (latestCommitHash.trim() !== commitRef.trim()) {
const newVersionText =
t('new_development_version', { newCommit: latestCommitHash.slice(0, 7), oldCommit: commitRef.slice(0, 7) }) + ' ' + t('refresh_to_update');
alert(newVersionText);
updateAvailable = true;
}
}
if (!updateAvailable) {
alert(
commitRef
? `${t('latest_development_version', { commit: commitRef.slice(0, 7), link: 'https://plebchan.eth.limo/#/', interpolation: { escapeValue: false } })}`
: `${t('latest_stable_version', { version: packageJson.version })}`,
);
}
} catch (error) {
alert('Failed to fetch latest version info: ' + error);
} finally {
setLoading(false);
}
};
return (
2024-05-10 17:03:33 +02:00
<button className={styles.checkForUpdatesButton} onClick={checkForUpdates} disabled={loading}>
{t('check')}
</button>
);
};
const Style = () => {
const [theme, setTheme] = useTheme();
2024-05-09 16:04:24 +02:00
return (
<select className={styles.themeSettings} value={theme} onChange={(e) => setTheme(e.target.value)}>
<option value='yotsuba'>Yotsuba</option>
<option value='yotsuba-b'>Yotsuba B</option>
<option value='futaba'>Futaba</option>
<option value='burichan'>Burichan</option>
<option value='tomorrow'>Tomorrow</option>
<option value='photon'>Photon</option>
</select>
);
};
// prettier-ignore
const availableLanguages = ['ar', 'bn', 'cs', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fil', 'fr', 'he', 'hi', 'hu', 'id', 'it', 'ja', 'ko', 'mr', 'nl', 'no', 'pl', 'pt', 'ro', 'ru', 'sq', 'sv', 'te', 'th', 'tr', 'uk', 'ur', 'vi', 'zh'];
const InterfaceLanguage = () => {
const { i18n } = useTranslation();
const { changeLanguage, language } = i18n;
const onSelectLanguage = (e: React.ChangeEvent<HTMLSelectElement>) => {
changeLanguage(e.target.value);
};
return (
<div className={styles.languageSettings}>
<select value={language} onChange={onSelectLanguage}>
{availableLanguages.map((lang) => (
<option key={lang} value={lang}>
{lang}
</option>
))}
</select>
2024-05-09 16:04:24 +02:00
</div>
);
};
2024-05-15 22:23:11 +02:00
// const PlebbitOptionsSettings = () => {
// return (
// <>
// <div className={styles.setting}></div>
// </>
// );
// };
2024-05-10 17:03:33 +02:00
const SettingsModal = () => {
const { t } = useTranslation();
const navigate = useNavigate();
const closeModal = () => {
navigate(-1);
};
2024-05-10 17:03:33 +02:00
const [showAccountSettings, setShowAccountSettings] = useState(false);
const [showCryptoAddressSetting, setShowCryptoAddressSetting] = useState(false);
const [showCryptoWalletSettings, setShowCryptoWalletSettings] = useState(false);
2024-05-16 22:00:22 +02:00
const [expandAll, setExpandAll] = useState(false);
const handleExpandAll = () => {
const newExpandState = !expandAll;
setExpandAll(newExpandState);
setShowAccountSettings(newExpandState);
setShowCryptoAddressSetting(newExpandState);
setShowCryptoWalletSettings(newExpandState);
};
2024-05-10 17:03:33 +02:00
return (
<>
<div className={styles.overlay} onClick={closeModal}></div>
<div className={styles.settingsModal}>
<div className={styles.header}>
<span className={styles.version}>
<a href={`https://github.com/plebbit/plebchan/releases/tag/v${packageJson.version}`} target='_blank' rel='noopener noreferrer'>
v{packageJson.version}
</a>
{commitRef && (
<a href={`https://github.com/plebbit/plebchan/commit/${commitRef}`} target='_blank' rel='noopener noreferrer'>
#{commitRef.slice(0, 7)}
</a>
)}
</span>
<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}>
[<span onClick={handleExpandAll}>{expandAll ? 'Collapse All Settings' : 'Expand All Settings'}</span>]
</div>
<div className={styles.setting}>
2024-05-10 17:03:33 +02:00
{t('update')}: <CheckForUpdates />
</div>
<div className={styles.setting}>
2024-05-10 17:03:33 +02:00
{t('style')}: <Style />
</div>
<div className={styles.setting}>
2024-05-10 17:03:33 +02:00
{t('interface_language')}: <InterfaceLanguage />
</div>
2024-05-10 17:03:33 +02:00
<div className={`${styles.setting} ${styles.category}`}>
<label onClick={() => setShowAccountSettings(!showAccountSettings)}>
<span className={showAccountSettings ? styles.hideButton : styles.showButton} />
2024-05-15 22:12:32 +02:00
{t('account')}
2024-05-10 17:03:33 +02:00
</label>
</div>
{showAccountSettings && <AccountSettings />}
<div className={`${styles.setting} ${styles.category}`}>
<label onClick={() => setShowCryptoAddressSetting(!showCryptoAddressSetting)}>
<span className={showCryptoAddressSetting ? styles.hideButton : styles.showButton} />
2024-05-15 22:12:32 +02:00
{t('crypto_address')}
</label>
</div>
{showCryptoAddressSetting && <CryptoAddressSetting />}
<div className={`${styles.setting} ${styles.category}`}>
<label onClick={() => setShowCryptoWalletSettings(!showCryptoWalletSettings)}>
<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 />}
2024-05-15 22:23:11 +02:00
{/* <div className={`${styles.setting} ${styles.category}`}>
2024-05-10 17:03:33 +02:00
<label onClick={() => setShowPlebbitOptionsSettings(!showPlebbitOptionsSettings)}>
<span className={showPlebbitOptionsSettings ? styles.hideButton : styles.showButton} />
2024-05-15 22:12:32 +02:00
{t('plebbit_options')}
2024-05-10 17:03:33 +02:00
</label>
</div>
2024-05-15 22:23:11 +02:00
{showPlebbitOptionsSettings && <PlebbitOptionsSettings />} */}
</div>
</>
);
};
2024-05-09 16:04:24 +02:00
export default SettingsModal;