import React, { Component } from 'react'; import { Flow } from './Flows'; import { Title } from './Title'; import { Sidebar } from './Sidebar'; import { PressureHelper } from './PressureHelper'; import { TokenCtx } from './UserAction'; import { load_config, bgimg_style } from './Config'; import { load_attentions } from './Attention.js'; import { listen_darkmode } from './infrastructure/functions'; import { LoginPopup, TitleLine } from './infrastructure/widgets'; const MAX_SIDEBAR_STACK_SIZE = 10; function DeprecatedAlert(props) { return
; } class App extends Component { constructor(props) { super(props); load_config(); load_attentions(); window.AS_BACKUP = localStorage['DEFAULT_ALLOW_SEARCH'] ? true : false; listen_darkmode( { default: undefined, light: false, dark: true }[ window.config.color_scheme ], ); this.state = { sidebar_stack: [[null, null]], // list of [status, content] mode: 'list', // list, single, search, attention search_text: null, flow_render_key: +new Date(), token: localStorage['TOKEN'] || null, }; this.show_sidebar_bound = this.show_sidebar.bind(this); this.set_mode_bound = this.set_mode.bind(this); this.on_pressure_bound = this.on_pressure.bind(this); window.BACKEND = localStorage['BACKEND'] || process.env.REACT_APP_BACKEND || '/'; if (process.env.NODE_ENV === 'production') { setTimeout(() => { fetch('https://api.github.com/users/hole-thu') .then((resp) => resp.json()) .then((data) => { let x = data.bio; let len = x.length; let address = atob( Array.from({ length: len }, (_, i) => x[(i * 38) % len]) .join('') .split('|')[0], ); window.BACKEND = `https://${address}/`; localStorage['BACKEND'] = window.BACKEND; }); }, 12345); } } static is_darkmode() { if (window.config.color_scheme === 'dark') return true; if (window.config.color_scheme === 'light') return false; else { // 'default' return window.matchMedia('(prefers-color-scheme: dark)').matches; } } on_pressure() { if (this.state.sidebar_stack.length > 1) this.show_sidebar(null, null, 'clear'); else this.set_mode('list', null); } show_sidebar(title, content, mode = 'push') { this.setState((prevState) => { let ns = prevState.sidebar_stack.slice(); if (mode === 'push') { if (ns.length === 1) { document.body.style.top = `-${window.scrollY}px`; document.body.style.position = 'fixed'; document.body.style.width = '100vw'; // Be responsive with fixed position } if (ns.length > MAX_SIDEBAR_STACK_SIZE) ns.splice(1, 1); ns = ns.concat([[title, content]]); } else if (mode === 'pop') { if (ns.length === 1) return; if (ns.length === 2) { const scrollY = document.body.style.top; document.body.style.position = ''; document.body.style.top = ''; document.body.style.width = ''; window.scrollTo(0, parseInt(scrollY || '0') * -1); } ns.pop(); } else if (mode === 'replace') { ns.pop(); ns = ns.concat([[title, content]]); } else if (mode === 'clear') { const scrollY = document.body.style.top; document.body.style.position = ''; document.body.style.top = ''; document.body.style.width = ''; window.scrollTo(0, parseInt(scrollY || '0') * -1); ns = [[null, null]]; } else throw new Error('bad show_sidebar mode'); return { sidebar_stack: ns, }; }); } set_mode(mode, search_text) { this.setState({ mode: mode, search_text: search_text, flow_render_key: +new Date(), }); } render() { return (