Add permanent settings

This commit is contained in:
Reckless_Satoshi 2022-11-01 07:38:59 -07:00
parent f8251d1b31
commit 87e0a8b4ae
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
8 changed files with 79 additions and 32 deletions

View File

@ -53,8 +53,11 @@ const App = (): JSX.Element => {
<I18nextProvider i18n={i18n}> <I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<CssBaseline /> <CssBaseline />
<TorConnection /> {window.NativeRobosats === undefined ? (
<UnsafeAlert className='unsafeAlert' /> <UnsafeAlert settings={settings} setSettings={setSettings} />
) : (
<TorConnection />
)}
<Main settings={settings} setSettings={setSettings} /> <Main settings={settings} setSettings={setSettings} />
</ThemeProvider> </ThemeProvider>
</I18nextProvider> </I18nextProvider>

View File

@ -230,7 +230,6 @@ const Main = ({ settings, setSettings }: MainProps): JSX.Element => {
<div> <div>
<UserGenPage <UserGenPage
setPage={setPage} setPage={setPage}
currentOrder={currentOrder}
setCurrentOrder={setCurrentOrder} setCurrentOrder={setCurrentOrder}
match={props.match} match={props.match}
theme={theme} theme={theme}

View File

@ -73,12 +73,12 @@ class UserGenPage extends Component {
requestBody.then((body) => requestBody.then((body) =>
apiClient.post('/api/user/', body).then((data) => { apiClient.post('/api/user/', body).then((data) => {
this.setState({ found: data.found, bad_request: data.bad_request }); this.setState({ found: data.found, bad_request: data.bad_request });
this.props.setOrder( this.props.setCurrentOrder(
data.active_order_id data.active_order_id
? data.active_order_id ? data.active_order_id
: data.last_order_id : data.last_order_id
? data.last_order_id ? data.last_order_id
: this.props.order, : null,
); );
// Add nick and token to App state (token only if not a bad request) // Add nick and token to App state (token only if not a bad request)
data.bad_request data.bad_request

View File

@ -24,6 +24,7 @@ import {
SettingsOverscan, SettingsOverscan,
Link, Link,
} from '@mui/icons-material'; } from '@mui/icons-material';
import { systemClient } from '../../services/System';
interface SettingsFormProps { interface SettingsFormProps {
dense?: boolean; dense?: boolean;
@ -58,7 +59,10 @@ const SettingsForm = ({
</ListItemIcon> </ListItemIcon>
<SelectLanguage <SelectLanguage
language={settings.language} language={settings.language}
setLanguage={(language) => setSettings({ ...settings, language })} setLanguage={(language) => {
setSettings({ ...settings, language });
systemClient.setCookie('settings_language', language);
}}
/> />
</ListItem> </ListItem>
@ -103,9 +107,11 @@ const SettingsForm = ({
<LightMode sx={{ width: '0.67em', height: '0.67em', color: '#666' }} /> <LightMode sx={{ width: '0.67em', height: '0.67em', color: '#666' }} />
</Paper> </Paper>
} }
onChange={(e) => onChange={(e) => {
setSettings({ ...settings, mode: e.target.checked ? 'dark' : 'light' }) const mode = e.target.checked ? 'dark' : 'light';
} setSettings({ ...settings, mode });
systemClient.setCookie('settings_mode', mode);
}}
/> />
} }
/> />
@ -120,7 +126,11 @@ const SettingsForm = ({
min={settings.frontend == 'basic' ? 12 : 10} min={settings.frontend == 'basic' ? 12 : 10}
max={settings.frontend == 'basic' ? 16 : 14} max={settings.frontend == 'basic' ? 16 : 14}
step={1} step={1}
onChange={(e) => setSettings({ ...settings, fontSize: e.target.value })} onChange={(e) => {
const fontSize = e.target.value;
setSettings({ ...settings, fontSize });
systemClient.setCookie(`settings_fontsize_${settings.frontend}`, fontSize);
}}
valueLabelDisplay='off' valueLabelDisplay='off'
marks={fontSizes.map(({ label, value }) => ({ marks={fontSizes.map(({ label, value }) => ({
label: <Typography variant='caption'>{t(label)}</Typography>, label: <Typography variant='caption'>{t(label)}</Typography>,
@ -137,7 +147,7 @@ const SettingsForm = ({
<ToggleButtonGroup <ToggleButtonGroup
exclusive={true} exclusive={true}
value={settings.network} value={settings.network}
onChange={(e, value) => setSettings({ ...settings, network: value })} onChange={(e, network) => setSettings({ ...settings, network })}
> >
<ToggleButton value='mainnet' color='primary'> <ToggleButton value='mainnet' color='primary'>
{t('Mainnet')} {t('Mainnet')}

View File

@ -9,21 +9,10 @@ class UnsafeAlert extends Component {
super(props); super(props);
this.state = { this.state = {
show: true, show: true,
isSelfhosted: this.isSelfhosted(),
}; };
} }
isSelfhosted() { // To do. Read from Coordinators state Obj.
const http = new XMLHttpRequest();
try {
http.open('HEAD', `${location.protocol}//${getHost()}/selfhosted`, false);
http.send();
return http.status === 200;
} catch {
return false;
}
}
safe_urls = [ safe_urls = [
'robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion', 'robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion',
'robotestagw3dcxmd66r4rgksb4nmmr43fh77bzn2ia2eucduyeafnyd.onion', 'robotestagw3dcxmd66r4rgksb4nmmr43fh77bzn2ia2eucduyeafnyd.onion',
@ -32,6 +21,26 @@ class UnsafeAlert extends Component {
'r7r4sckft6ptmk4r2jajiuqbowqyxiwsle4iyg4fijtoordc6z7a.b32.i2p', 'r7r4sckft6ptmk4r2jajiuqbowqyxiwsle4iyg4fijtoordc6z7a.b32.i2p',
]; ];
checkClient() {
const http = new XMLHttpRequest();
const unsafeClient = !this.safe_urls.includes(getHost());
try {
http.open('HEAD', `${location.protocol}//${getHost()}/selfhosted`, false);
http.send();
this.props.setSettings({
...this.props.settings,
unsafeClient,
selfhostedClient: http.status === 200,
});
} catch {
this.props.setSettings({ ...this.props.settings, unsafeClient, selfhostedClient: false });
}
}
componentDidMount() {
this.checkClient();
}
render() { render() {
const { t } = this.props; const { t } = this.props;
@ -41,7 +50,7 @@ class UnsafeAlert extends Component {
} }
// Show selfhosted notice // Show selfhosted notice
if (this.state.isSelfhosted) { else if (this.props.settings.selfhostedClient) {
return ( return (
<div> <div>
<Paper elevation={6} className='alertUnsafe'> <Paper elevation={6} className='alertUnsafe'>
@ -65,7 +74,7 @@ class UnsafeAlert extends Component {
} }
// Show unsafe alert // Show unsafe alert
if (!window.NativeRobosats && !this.safe_urls.includes(getHost())) { else if (this.props.settings.unsafeClient) {
return ( return (
<div> <div>
<MediaQuery minWidth={800}> <MediaQuery minWidth={800}>

View File

@ -1,8 +1,13 @@
import { systemClient } from '../services/System';
import { baseSettings, Settings } from './Settings.model'; import { baseSettings, Settings } from './Settings.model';
const fontSizeCookie = systemClient.getCookie('settings_fontsize_basic');
const fontSize = fontSizeCookie !== '' ? Number(fontSizeCookie) : 14;
export const defaultSettings: Settings = { export const defaultSettings: Settings = {
...baseSettings, ...baseSettings,
frontend: 'basic', frontend: 'basic',
fontSize: fontSize,
}; };
export default defaultSettings; export default defaultSettings;

View File

@ -1,9 +1,13 @@
import { systemClient } from '../services/System';
import { baseSettings, Settings } from './Settings.model'; import { baseSettings, Settings } from './Settings.model';
const fontSizeCookie = systemClient.getCookie('settings_fontsize_pro');
const fontSize = fontSizeCookie !== '' ? Number(fontSizeCookie) : 12;
export const defaultSettings: Settings = { export const defaultSettings: Settings = {
...baseSettings, ...baseSettings,
fontSize: 12,
frontend: 'pro', frontend: 'pro',
fontSize: fontSize,
}; };
export default defaultSettings; export default defaultSettings;

View File

@ -1,4 +1,5 @@
import i18n from '../i18n/Web'; import i18n from '../i18n/Web';
import { systemClient } from '../services/System';
import type Coordinator from './Coordinator.model'; import type Coordinator from './Coordinator.model';
export type Language = export type Language =
@ -27,20 +28,36 @@ export interface Settings {
freezeViewports: boolean; freezeViewports: boolean;
network: 'mainnet' | 'testnet' | undefined; network: 'mainnet' | 'testnet' | undefined;
coordinator: Coordinator | undefined; coordinator: Coordinator | undefined;
unsafeClient: boolean;
hostedClient: boolean;
} }
const modeCookie: 'light' | 'dark' | '' = systemClient.getCookie('settings_mode');
const mode: 'light' | 'dark' =
modeCookie !== ''
? modeCookie
: window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
const languageCookie = systemClient.getCookie('settings_language');
const language: Language =
languageCookie !== ''
? languageCookie
: i18n.resolvedLanguage == null
? 'en'
: i18n.resolvedLanguage.substring(0, 2);
export const baseSettings: Settings = { export const baseSettings: Settings = {
frontend: 'basic', frontend: 'basic',
mode: mode: mode,
window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light',
fontSize: 14, fontSize: 14,
language: language: language,
i18n.resolvedLanguage == null ? 'en' : (i18n.resolvedLanguage.substring(0, 2) as Language),
freezeViewports: false, freezeViewports: false,
network: undefined, network: undefined,
coordinator: undefined, coordinator: undefined,
unsafeClient: false,
hostedClient: false,
}; };
export default Settings; export default Settings;