2022-12-10 12:14:48 +03:00
|
|
|
import './settings.css';
|
|
|
|
|
2023-01-24 15:56:43 +03:00
|
|
|
import { Menu, MenuItem } from '@szhsin/react-menu';
|
2023-01-21 19:37:46 +03:00
|
|
|
import { useReducer, useRef, useState } from 'preact/hooks';
|
2023-01-14 14:42:04 +03:00
|
|
|
import { useSnapshot } from 'valtio';
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-01-25 19:54:30 +03:00
|
|
|
import logo from '../assets/logo.svg';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Avatar from '../components/avatar';
|
|
|
|
import Icon from '../components/icon';
|
2023-01-20 19:23:59 +03:00
|
|
|
import Link from '../components/link';
|
2022-12-10 12:14:48 +03:00
|
|
|
import NameText from '../components/name-text';
|
2023-01-05 05:50:27 +03:00
|
|
|
import RelativeTime from '../components/relative-time';
|
2022-12-25 18:31:50 +03:00
|
|
|
import states from '../utils/states';
|
2022-12-10 12:14:48 +03:00
|
|
|
import store from '../utils/store';
|
|
|
|
|
|
|
|
/*
|
|
|
|
Settings component that shows these settings:
|
|
|
|
- Accounts list for switching
|
|
|
|
- Dark/light/auto theme switch (done with adding/removing 'is-light' or 'is-dark' class on the body)
|
|
|
|
*/
|
|
|
|
|
2022-12-16 08:27:04 +03:00
|
|
|
function Settings({ onClose }) {
|
2023-01-14 14:42:04 +03:00
|
|
|
const snapStates = useSnapshot(states);
|
2022-12-10 12:14:48 +03:00
|
|
|
// Accounts
|
|
|
|
const accounts = store.local.getJSON('accounts');
|
|
|
|
const currentAccount = store.session.get('currentAccount');
|
|
|
|
const currentTheme = store.local.get('theme') || 'auto';
|
|
|
|
const themeFormRef = useRef();
|
|
|
|
const moreThanOneAccount = accounts.length > 1;
|
|
|
|
const [currentDefault, setCurrentDefault] = useState(0);
|
|
|
|
|
2023-01-21 19:37:46 +03:00
|
|
|
const [_, reload] = useReducer((x) => x + 1, 0);
|
|
|
|
|
2022-12-10 12:14:48 +03:00
|
|
|
return (
|
2022-12-29 11:11:58 +03:00
|
|
|
<div id="settings-container" class="sheet" tabIndex="-1">
|
2022-12-25 13:01:01 +03:00
|
|
|
<main>
|
|
|
|
{/* <button type="button" class="close-button plain large" onClick={onClose}>
|
2022-12-10 15:48:48 +03:00
|
|
|
<Icon icon="x" alt="Close" />
|
2022-12-21 15:00:45 +03:00
|
|
|
</button> */}
|
2022-12-25 13:01:01 +03:00
|
|
|
<h2>Accounts</h2>
|
2023-01-17 12:58:04 +03:00
|
|
|
<section>
|
|
|
|
<ul class="accounts-list">
|
|
|
|
{accounts.map((account, i) => {
|
|
|
|
const isCurrent = account.info.id === currentAccount;
|
|
|
|
const isDefault = i === (currentDefault || 0);
|
|
|
|
return (
|
2023-01-21 19:37:46 +03:00
|
|
|
<li key={i + account.id}>
|
2023-01-17 12:58:04 +03:00
|
|
|
<div>
|
|
|
|
{moreThanOneAccount && (
|
|
|
|
<span class={`current ${isCurrent ? 'is-current' : ''}`}>
|
|
|
|
<Icon icon="check-circle" alt="Current" />
|
|
|
|
</span>
|
|
|
|
)}
|
2023-01-21 19:37:46 +03:00
|
|
|
<Avatar
|
|
|
|
url={account.info.avatarStatic}
|
|
|
|
size="xxl"
|
|
|
|
onDblClick={async () => {
|
|
|
|
if (isCurrent) {
|
|
|
|
try {
|
|
|
|
const info = await masto.v1.accounts.fetch(
|
|
|
|
account.info.id,
|
|
|
|
);
|
|
|
|
console.log('fetched account info', info);
|
|
|
|
account.info = info;
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
reload();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2023-01-17 12:58:04 +03:00
|
|
|
<NameText
|
|
|
|
account={account.info}
|
|
|
|
showAcct
|
2022-12-10 12:14:48 +03:00
|
|
|
onClick={() => {
|
2023-01-17 12:58:04 +03:00
|
|
|
states.showAccount = `${account.info.username}@${account.instanceURL}`;
|
2022-12-10 12:14:48 +03:00
|
|
|
}}
|
2023-01-17 12:58:04 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="actions">
|
|
|
|
{isDefault && moreThanOneAccount && (
|
|
|
|
<>
|
|
|
|
<span class="tag">Default</span>{' '}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{!isCurrent && (
|
2022-12-10 12:14:48 +03:00
|
|
|
<button
|
|
|
|
type="button"
|
2023-01-17 12:58:04 +03:00
|
|
|
class="light"
|
2022-12-10 12:14:48 +03:00
|
|
|
onClick={() => {
|
2023-01-17 12:58:04 +03:00
|
|
|
store.session.set('currentAccount', account.info.id);
|
|
|
|
location.reload();
|
2022-12-10 12:14:48 +03:00
|
|
|
}}
|
|
|
|
>
|
2023-01-17 12:58:04 +03:00
|
|
|
<Icon icon="transfer" /> Switch
|
2022-12-10 12:14:48 +03:00
|
|
|
</button>
|
2022-12-25 13:01:01 +03:00
|
|
|
)}
|
2023-01-24 15:56:43 +03:00
|
|
|
<Menu
|
|
|
|
align="end"
|
|
|
|
menuButton={
|
2022-12-25 13:01:01 +03:00
|
|
|
<button
|
|
|
|
type="button"
|
2023-01-24 15:56:43 +03:00
|
|
|
title="More"
|
|
|
|
class="plain more-button"
|
2022-12-25 13:01:01 +03:00
|
|
|
>
|
2023-01-24 15:56:43 +03:00
|
|
|
<Icon icon="more" size="l" alt="More" />
|
2022-12-25 13:01:01 +03:00
|
|
|
</button>
|
2023-01-24 15:56:43 +03:00
|
|
|
}
|
|
|
|
>
|
2023-01-25 11:42:01 +03:00
|
|
|
{moreThanOneAccount && (
|
|
|
|
<MenuItem
|
|
|
|
disabled={isDefault}
|
|
|
|
onClick={() => {
|
|
|
|
// Move account to the top of the list
|
|
|
|
accounts.splice(i, 1);
|
|
|
|
accounts.unshift(account);
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
setCurrentDefault(i);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Set as default
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
2023-01-24 15:56:43 +03:00
|
|
|
<MenuItem
|
|
|
|
disabled={!isCurrent}
|
|
|
|
onClick={() => {
|
|
|
|
const yes = confirm(
|
|
|
|
'Are you sure you want to log out?',
|
|
|
|
);
|
|
|
|
if (!yes) return;
|
|
|
|
accounts.splice(i, 1);
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
location.reload();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Log out
|
|
|
|
</MenuItem>
|
|
|
|
</Menu>
|
2022-12-25 13:01:01 +03:00
|
|
|
</div>
|
2023-01-17 12:58:04 +03:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
{moreThanOneAccount && (
|
|
|
|
<p>
|
|
|
|
<small>
|
|
|
|
Note: <i>Default</i> account will always be used for first load.
|
|
|
|
Switched accounts will persist during the session.
|
|
|
|
</small>
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
<p style={{ textAlign: 'end' }}>
|
2023-01-20 19:23:59 +03:00
|
|
|
<Link to="/login" class="button" onClick={onClose}>
|
2023-01-17 12:58:04 +03:00
|
|
|
Add new account
|
2023-01-20 19:23:59 +03:00
|
|
|
</Link>
|
2022-12-25 13:01:01 +03:00
|
|
|
</p>
|
2023-01-17 12:58:04 +03:00
|
|
|
</section>
|
|
|
|
<h2>Settings</h2>
|
|
|
|
<ul class="section">
|
|
|
|
<li>
|
|
|
|
<div>
|
|
|
|
<label>Appearance</label>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<form
|
|
|
|
ref={themeFormRef}
|
|
|
|
onInput={(e) => {
|
|
|
|
console.log(e);
|
|
|
|
e.preventDefault();
|
|
|
|
const formData = new FormData(themeFormRef.current);
|
|
|
|
const theme = formData.get('theme');
|
|
|
|
const html = document.documentElement;
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-01-17 12:58:04 +03:00
|
|
|
if (theme === 'auto') {
|
|
|
|
html.classList.remove('is-light', 'is-dark');
|
|
|
|
} else {
|
|
|
|
html.classList.toggle('is-light', theme === 'light');
|
|
|
|
html.classList.toggle('is-dark', theme === 'dark');
|
|
|
|
}
|
|
|
|
document
|
|
|
|
.querySelector('meta[name="color-scheme"]')
|
|
|
|
.setAttribute('content', theme);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-01-17 12:58:04 +03:00
|
|
|
if (theme === 'auto') {
|
|
|
|
store.local.del('theme');
|
|
|
|
} else {
|
|
|
|
store.local.set('theme', theme);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div class="radio-group">
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="theme"
|
|
|
|
value="light"
|
|
|
|
defaultChecked={currentTheme === 'light'}
|
|
|
|
/>
|
|
|
|
<span>Light</span>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="theme"
|
|
|
|
value="dark"
|
|
|
|
defaultChecked={currentTheme === 'dark'}
|
|
|
|
/>
|
|
|
|
<span>Dark</span>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="theme"
|
|
|
|
value="auto"
|
|
|
|
defaultChecked={
|
|
|
|
currentTheme !== 'light' && currentTheme !== 'dark'
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<span>Auto</span>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
<li>
|
2022-12-25 13:01:01 +03:00
|
|
|
<label>
|
|
|
|
<input
|
2023-01-17 12:58:04 +03:00
|
|
|
type="checkbox"
|
|
|
|
checked={snapStates.settings.boostsCarousel}
|
|
|
|
onChange={(e) => {
|
|
|
|
states.settings.boostsCarousel = e.target.checked;
|
|
|
|
}}
|
|
|
|
/>{' '}
|
|
|
|
Boosts carousel (experimental)
|
2022-12-25 13:01:01 +03:00
|
|
|
</label>
|
2023-01-17 12:58:04 +03:00
|
|
|
</li>
|
|
|
|
</ul>
|
2023-01-13 10:30:09 +03:00
|
|
|
<h2>Hidden features</h2>
|
2023-01-17 12:58:04 +03:00
|
|
|
<section>
|
|
|
|
<div>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="light"
|
|
|
|
onClick={() => {
|
|
|
|
states.showDrafts = true;
|
|
|
|
states.showSettings = false;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Unsent drafts
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</section>
|
2022-12-25 13:01:01 +03:00
|
|
|
<h2>About</h2>
|
2023-01-17 12:58:04 +03:00
|
|
|
<section>
|
2022-12-25 13:01:01 +03:00
|
|
|
<p>
|
2023-01-25 19:54:30 +03:00
|
|
|
<img
|
|
|
|
src={logo}
|
|
|
|
alt=""
|
|
|
|
width="20"
|
|
|
|
height="20"
|
|
|
|
style={{
|
|
|
|
aspectRatio: '1/1',
|
|
|
|
verticalAlign: 'middle',
|
|
|
|
}}
|
|
|
|
/>{' '}
|
|
|
|
<a
|
|
|
|
href="https://hachyderm.io/@phanpy"
|
|
|
|
// target="_blank"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
states.showAccount = 'phanpy@hachyderm.io';
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
@phanpy
|
|
|
|
</a>
|
2023-01-30 18:16:00 +03:00
|
|
|
.
|
|
|
|
</p>
|
|
|
|
<p>
|
2023-01-17 12:58:04 +03:00
|
|
|
<a href="https://github.com/cheeaun/phanpy" target="_blank">
|
|
|
|
Built
|
|
|
|
</a>{' '}
|
|
|
|
by{' '}
|
|
|
|
<a
|
|
|
|
href="https://mastodon.social/@cheeaun"
|
|
|
|
// target="_blank"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
states.showAccount = 'cheeaun@mastodon.social';
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
@cheeaun
|
|
|
|
</a>
|
2023-01-30 18:16:00 +03:00
|
|
|
.{' '}
|
|
|
|
<a
|
|
|
|
href="https://github.com/cheeaun/phanpy/blob/main/PRIVACY.MD"
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
Privacy Policy
|
|
|
|
</a>
|
2023-01-17 12:58:04 +03:00
|
|
|
.
|
2022-12-25 13:01:01 +03:00
|
|
|
</p>
|
2023-01-17 12:58:04 +03:00
|
|
|
{__BUILD_TIME__ && (
|
|
|
|
<p>
|
|
|
|
Last build: <RelativeTime datetime={new Date(__BUILD_TIME__)} />{' '}
|
|
|
|
{__COMMIT_HASH__ && (
|
|
|
|
<>
|
|
|
|
(
|
|
|
|
<a
|
|
|
|
href={`https://github.com/cheeaun/phanpy/commit/${__COMMIT_HASH__}`}
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
<code>{__COMMIT_HASH__}</code>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</section>
|
2022-12-25 13:01:01 +03:00
|
|
|
</main>
|
2022-12-10 12:14:48 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-12-16 08:27:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Settings;
|