import { render } from './iris/js/lib/preact.js';
import { Router, route } from './iris/js/lib/preact-router.es.js';
import { createHashHistory } from './iris/js/lib/history.production.min.js';
import { Component } from './iris/js/lib/preact.js';
import Helpers from './iris/js/Helpers.js';
import { html } from './iris/js/Helpers.js';
import QRScanner from './iris/js/QRScanner.js';
import PeerManager from './iris/js/PeerManager.js';
import Session from './iris/js/Session.js';
import PublicMessages from './iris/js/PublicMessages.js';
import { translate as t } from './iris/js/Translation.js';
import Settings from './iris/js/components/Settings.js';
import LogoutConfirmation from './iris/js/components/LogoutConfirmation.js';
import ChatView from './iris/js/components/ChatView.js';
import StoreView from './iris/js/components/StoreView.js';
import CheckoutView from './iris/js/components/CheckoutView.js';
import ProductView from './iris/js/components/ProductView.js';
import Login from './iris/js/components/Login.js';
import Profile from './iris/js/components/Profile.js';
import Header from './iris/js/components/Header.js';
import Footer from './iris/js/components/Footer.js';
import MessageView from './iris/js/components/MessageView.js';
import FollowsView from './iris/js/components/FollowsView.js';
import FeedView from './iris/js/components/FeedView.js';
import ExplorerView from './iris/js/components/ExplorerView.js';
import VideoCall from './iris/js/components/VideoCall.js';
import Identicon from './iris/js/components/Identicon.js';
import State from './iris/js/State.js';
import Icons from './iris/js/Icons.js';
const userAgent = navigator.userAgent.toLowerCase();
const isElectron = (userAgent.indexOf(' electron/') > -1);
if (!isElectron && ('serviceWorker' in navigator)) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('iris/serviceworker.js')
.catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
const peers = [`${window.location.protocol}//${window.location.host}/gun`];
State.init({peers});
Session.init({autologin: true});
PeerManager.init();
PublicMessages.init();
Helpers.checkColorScheme();
function handleRoute(e) {
document.title = 'GUN — the database for freedom fighters';
const activeRoute = e.url;
if (!activeRoute && window.location.hash) {
return route(window.location.hash.replace('#', '')); // bubblegum fix back navigation
}
const activeProfile = activeRoute.indexOf('/profile') === 0 ? activeRoute.replace('/profile/', '') : null;
localState.get('activeRoute').put(activeRoute);
QRScanner.cleanupScanner();
}
const APPLICATIONS = [ // TODO: move editable shortcuts to localState gun
{url: '/', text: t('home'), icon: Icons.home},
{url: '/settings', text: t('settings'), icon: Icons.settings},
{url: '/explorer', text: t('explorer'), icon: Icons.folder},
{url: '/chat', text: t('messages'), icon: Icons.chat},
{url: '/feed', text: t('feed'), icon: Icons.feed},
// {url: '/store', text: t('store'), icon: Icons.store}, // restore when it works!
{},
{url: '../stats.html', text: 'Gun node stats'},
{url: '../iris/index.html', text: 'Iris', icon: html`
`},
{url: '../infinite-scroll/index.html', text: 'Infinite scroll'},
{url: '../chat/index.html', text: 'Chat'},
{url: '../game/space.html', text: 'Space'},
{},
{url: 'https://gun.eco/docs/', text: 'Gun documentation'},
{url: 'https://examples.iris.to/components/', text: 'Iris web components'}
];
const HomeView = () => {
return html`
`;
};
class MenuView extends Component {
componentDidMount() {
State.local.get('showMenu').on(showMenu => this.setState({showMenu}));
}
render() {
const pub = Session.getPubKey();
return html`
`;
}
};
class Main extends Component {
constructor() {
super();
this.showMenu = false;
}
componentDidMount() {
localState.get('loggedIn').on(loggedIn => this.setState({loggedIn}));
}
render() {
const content = this.state.loggedIn ? html`
<${MenuView}/>
<${Router} history=${createHashHistory()} onChange=${e => handleRoute(e)}>
<${HomeView} path="/"/>
<${FeedView} path="/feed"/>
<${Login} path="/login"/>
<${ChatView} path="/chat/:id?"/>
<${MessageView} path="/message/:hash"/>
<${Settings} path="/settings" showSwitchAccount=${true}/>
<${LogoutConfirmation} path="/logout"/>
<${Profile.Profile} path="/profile/:id?"/>
<${StoreView} path="/store/:store?"/>
<${CheckoutView} path="/checkout/:store"/>
<${ProductView} path="/product/:product/:store"/>
<${ProductView} path="/product/new" store=Session.getPubKey()/>
<${ExplorerView} path="/explorer/:node"/>
<${ExplorerView} path="/explorer"/>
<${FollowsView} path="/follows/:id"/>
<${FollowsView} followers=${true} path="/followers/:id"/>
${Router}>
<${VideoCall}/>
` : '';
return html`
${content}
`;
}
}
render(html`<${Main}/>`, document.body);
$('body').css('opacity', 1); // use opacity because setting focus on display: none elements fails