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

349 lines
12 KiB
TypeScript
Raw Normal View History

2024-05-10 21:58:45 +02:00
import { useEffect, useMemo, useState } from 'react';
2024-05-09 16:04:24 +02:00
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
2024-05-10 21:58:45 +02:00
import { createAccount, deleteAccount, exportAccount, importAccount, setAccount, setActiveAccount, useAccount, useAccounts } from '@plebbit/plebbit-react-hooks';
import stringify from 'json-stringify-pretty-compact';
import styles from './settings-modal.module.css';
import useTheme from '../../hooks/use-theme';
import packageJson from '../../../package.json';
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-10 17:03:33 +02:00
const AccountSettings = () => {
2024-05-10 21:58:45 +02:00
const { t } = useTranslation();
2024-05-10 17:03:33 +02:00
const account = useAccount();
2024-05-10 21:58:45 +02:00
const { accounts } = useAccounts();
const [text, setText] = useState('');
const [switchToLastAccount, setSwitchToLastAccount] = useState(false);
const accountJson = useMemo(
() => stringify({ account: { ...account, plebbit: undefined, karma: undefined, plebbitReactOptions: undefined, unreadNotificationCount: undefined } }),
[account],
);
useEffect(() => {
setText(accountJson);
}, [accountJson]);
useEffect(() => {
if (switchToLastAccount && accounts.length > 0) {
const lastAccount = accounts[accounts.length - 1];
setActiveAccount(lastAccount.name);
setSwitchToLastAccount(false);
}
}, [accounts, switchToLastAccount]);
const _createAccount = async () => {
try {
await createAccount();
} catch (error) {
if (error instanceof Error) {
alert(error.message);
console.log(error);
} else {
console.error('An unknown error occurred:', error);
}
}
setSwitchToLastAccount(true);
};
const _deleteAccount = (accountName: string) => {
if (!accountName) {
return;
} else if (window.confirm(t('delete_confirm', { value: accountName, interpolation: { escapeValue: false } }))) {
if (window.confirm(t('double_confirm'))) {
deleteAccount(accountName);
}
} else {
return;
}
};
const saveAccount = async () => {
try {
const newAccount = JSON.parse(text).account;
// force keeping the same id, makes it easier to copy paste
await setAccount({ ...newAccount, id: account?.id });
alert(`Saved ${newAccount.name}`);
} catch (error) {
if (error instanceof Error) {
alert(error.message);
console.log(error);
} else {
console.error('An unknown error occurred:', error);
}
}
};
const _exportAccount = async () => {
try {
const accountString = await exportAccount();
const accountObject = JSON.parse(accountString);
const formattedAccountJson = JSON.stringify(accountObject, null, 2);
// Create a Blob from the JSON string
const blob = new Blob([formattedAccountJson], { type: 'application/json' });
// Create a URL for the Blob
const fileUrl = URL.createObjectURL(blob);
// Create a temporary download link
const link = document.createElement('a');
link.href = fileUrl;
link.download = `${account.name}.json`;
// Append the link, trigger the download, then remove the link
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Release the Blob URL
URL.revokeObjectURL(fileUrl);
} catch (error) {
if (error instanceof Error) {
alert(error.message);
console.log(error);
} else {
console.error('An unknown error occurred:', error);
}
}
};
const _importAccount = async () => {
// Create a file input element
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.json';
// Handle file selection
fileInput.onchange = async (event) => {
try {
const files = (event.target as HTMLInputElement).files;
if (!files || files.length === 0) {
throw new Error('No file selected.');
}
const file = files[0];
// Read the file content
const reader = new FileReader();
reader.onload = async (e) => {
const fileContent = e.target!.result; // Non-null assertion
if (typeof fileContent !== 'string') {
throw new Error('File content is not a string.');
}
const newAccount = JSON.parse(fileContent);
await importAccount(fileContent);
setSwitchToLastAccount(true);
alert(`Imported ${newAccount.account?.name}`);
};
reader.readAsText(file);
} catch (error) {
if (error instanceof Error) {
alert(error.message);
console.log(error);
} else {
console.error('An unknown error occurred:', error);
}
}
};
// Trigger file selection dialog
fileInput.click();
};
const accountsOptions = accounts.map((account) => (
<option key={account?.id} value={account?.name}>
u/{account?.author?.shortAddress}
</option>
));
2024-05-10 17:03:33 +02:00
return (
2024-05-10 21:58:45 +02:00
<div className={styles.accountSettings}>
2024-05-10 22:23:18 +02:00
<div>
<select value={account?.name} onChange={(e) => setActiveAccount(e.target.value)}>
{accountsOptions}
</select>
<button className={styles.createAccount} onClick={_createAccount}>
+
</button>
</div>
<div></div>
2024-05-10 21:58:45 +02:00
<textarea value={text} onChange={(e) => setText(e.target.value)} autoCorrect='off' autoComplete='off' spellCheck='false' />
<div>
2024-05-10 22:23:18 +02:00
<button onClick={saveAccount}>Save</button> <button onClick={() => setText(accountJson)}>Reset</button>
2024-05-10 21:58:45 +02:00
</div>
<div>
<button onClick={_importAccount}>Import</button> full account data
</div>
<div>
<button onClick={_exportAccount}>Export</button> full account data
</div>
<div className={styles.deleteAccount}>
<button onClick={() => _deleteAccount(account?.name)}>Delete</button> this account
</div>
</div>
2024-05-10 17:03:33 +02:00
);
};
const PlebbitOptionsSettings = () => {
return (
<>
2024-05-10 21:58:45 +02:00
<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 [showPlebbitOptionsSettings, setShowPlebbitOptionsSettings] = useState(false);
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>
<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} />
Account
</label>
</div>
{showAccountSettings && <AccountSettings />}
<div className={`${styles.setting} ${styles.category}`}>
<label onClick={() => setShowPlebbitOptionsSettings(!showPlebbitOptionsSettings)}>
<span className={showPlebbitOptionsSettings ? styles.hideButton : styles.showButton} />
Plebbit Options
</label>
</div>
{showPlebbitOptionsSettings && <PlebbitOptionsSettings />}
</div>
</>
);
};
2024-05-09 16:04:24 +02:00
export default SettingsModal;