snort/packages/app/src/index.tsx

182 lines
4.8 KiB
TypeScript
Raw Normal View History

import "./index.css";
import "@szhsin/react-menu/dist/index.css";
2023-08-25 22:24:47 +00:00
import "./fonts/inter.css";
2022-12-18 14:51:47 +00:00
2023-09-06 12:45:25 +00:00
import {default as wasmInit} from "@snort/system-query";
import WasmPath from "@snort/system-query/pkg/system_query_bg.wasm";
import { StrictMode } from "react";
import * as ReactDOM from "react-dom/client";
import { Provider } from "react-redux";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
2023-08-18 16:56:50 +00:00
import { EventPublisher, NostrSystem, ProfileLoaderService, Nip7Signer, PowWorker } from "@snort/system";
import { SnortContext } from "@snort/system-react";
2022-12-18 14:51:47 +00:00
import * as serviceWorkerRegistration from "serviceWorkerRegistration";
2023-01-31 18:56:31 +00:00
import { IntlProvider } from "IntlProvider";
2023-05-24 10:12:23 +00:00
import { unwrap } from "SnortUtils";
2023-01-20 11:11:50 +00:00
import Store from "State/Store";
import Layout from "Pages/Layout";
2023-05-15 17:38:26 +00:00
import LoginPage from "Pages/LoginPage";
import ProfilePage from "Pages/ProfilePage";
2023-03-28 14:34:01 +00:00
import { RootRoutes } from "Pages/Root";
import NotificationsPage from "Pages/Notifications";
import SettingsPage, { SettingsRoutes } from "Pages/SettingsPage";
import ErrorPage from "Pages/ErrorPage";
2023-08-21 13:58:57 +00:00
import NostrAddressPage from "Pages/NostrAddressPage";
import MessagesPage from "Pages/MessagesPage";
import DonatePage from "Pages/DonatePage";
import SearchPage from "Pages/SearchPage";
import HelpPage from "Pages/HelpPage";
import { NewUserRoutes } from "Pages/new";
2023-02-13 15:29:25 +00:00
import { WalletRoutes } from "Pages/WalletPage";
2023-02-14 11:08:25 +00:00
import NostrLinkHandler from "Pages/NostrLinkHandler";
2023-03-28 14:34:01 +00:00
import Thread from "Element/Thread";
2023-04-13 18:43:43 +00:00
import { SubscribeRoutes } from "Pages/subscribe";
2023-05-16 21:30:52 +00:00
import ZapPoolPage from "Pages/ZapPool";
2023-05-24 16:17:17 +00:00
import DebugPage from "Pages/Debug";
import { db } from "Db";
2023-06-26 10:30:37 +00:00
import { preload, RelayMetrics, UserCache, UserRelays } from "Cache";
import { LoginStore } from "Login";
2023-05-30 13:48:38 +00:00
/**
* Singleton nostr system
*/
export const System = new NostrSystem({
2023-06-15 11:03:05 +00:00
relayCache: UserRelays,
2023-06-17 18:42:09 +00:00
profileCache: UserCache,
relayMetrics: RelayMetrics,
2023-06-15 11:03:05 +00:00
authHandler: async (c, r) => {
const { publicKey, privateKey } = LoginStore.snapshot();
2023-07-04 17:18:52 +00:00
if (privateKey) {
const pub = EventPublisher.privateKey(privateKey);
return await pub.nip42Auth(c, r);
}
2023-06-15 11:03:05 +00:00
if (publicKey) {
2023-07-04 17:18:52 +00:00
const pub = new EventPublisher(new Nip7Signer(), publicKey);
2023-06-15 11:03:05 +00:00
return await pub.nip42Auth(c, r);
}
},
2023-05-30 13:48:38 +00:00
});
/**
* Singleton user profile loader
*/
2023-06-08 10:45:23 +00:00
export const ProfileLoader = new ProfileLoaderService(System, UserCache);
2022-12-18 14:51:47 +00:00
2023-08-18 16:56:50 +00:00
/**
* Singleton POW worker
*/
export const DefaultPowWorker = new PowWorker("/pow.js");
2023-01-19 11:03:51 +00:00
serviceWorkerRegistration.register();
2023-09-05 13:57:50 +00:00
async function initSite() {
2023-09-06 12:45:25 +00:00
await wasmInit(WasmPath);
2023-09-05 13:57:50 +00:00
const login = LoginStore.takeSnapshot();
db.ready = await db.isAvailable();
if (db.ready) {
await preload(login.follows.item);
}
for (const [k, v] of Object.entries(login.relays.item)) {
System.ConnectToRelay(k, v);
}
try {
if ("registerProtocolHandler" in window.navigator) {
2023-09-05 13:59:44 +00:00
window.navigator.registerProtocolHandler("web+nostr", `${window.location.protocol}//${window.location.host}/%s`);
2023-09-05 13:57:50 +00:00
console.info("Registered protocol handler for 'web+nostr'");
}
} catch (e) {
console.error("Failed to register protocol handler", e);
}
return null;
}
let didInit = false;
2023-01-28 15:40:19 +00:00
export const router = createBrowserRouter([
2023-01-12 12:00:44 +00:00
{
element: <Layout />,
errorElement: <ErrorPage />,
loader: async () => {
2023-09-05 13:57:50 +00:00
if (!didInit) {
didInit = true;
2023-09-05 13:59:44 +00:00
return await initSite();
}
return null;
},
2023-01-12 12:00:44 +00:00
children: [
2023-03-28 14:34:01 +00:00
...RootRoutes,
2023-01-12 12:00:44 +00:00
{
path: "/login",
element: <LoginPage />,
2023-01-12 12:00:44 +00:00
},
2023-02-03 18:20:17 +00:00
{
path: "/help",
element: <HelpPage />,
2023-02-03 18:20:17 +00:00
},
2023-01-12 12:00:44 +00:00
{
path: "/e/:id",
2023-03-28 14:34:01 +00:00
element: <Thread />,
2023-01-12 12:00:44 +00:00
},
{
path: "/p/:id",
element: <ProfilePage />,
2023-01-12 12:00:44 +00:00
},
{
path: "/notifications",
element: <NotificationsPage />,
2023-01-12 12:00:44 +00:00
},
{
path: "/settings",
2023-01-20 17:07:14 +00:00
element: <SettingsPage />,
children: SettingsRoutes,
2023-01-12 15:35:42 +00:00
},
{
2023-08-21 13:58:57 +00:00
path: "/nostr-address",
element: <NostrAddressPage />,
2023-01-12 09:48:39 +00:00
},
{
2023-06-26 11:29:12 +00:00
path: "/messages/:id?",
element: <MessagesPage />,
2023-01-12 09:48:39 +00:00
},
2023-01-19 00:03:24 +00:00
{
path: "/donate",
element: <DonatePage />,
},
2023-01-24 12:33:18 +00:00
{
2023-01-28 15:40:19 +00:00
path: "/search/:keyword?",
element: <SearchPage />,
2023-02-05 18:02:13 +00:00
},
2023-05-16 21:30:52 +00:00
{
path: "/zap-pool",
element: <ZapPoolPage />,
},
2023-03-31 09:51:50 +00:00
...NewUserRoutes,
...WalletRoutes,
2023-04-13 18:43:43 +00:00
...SubscribeRoutes,
2023-05-24 16:17:17 +00:00
{
path: "/debug",
element: <DebugPage />,
},
2023-01-29 19:44:53 +00:00
{
2023-03-31 09:51:50 +00:00
path: "/*",
2023-02-14 11:08:25 +00:00
element: <NostrLinkHandler />,
2023-01-29 19:44:53 +00:00
},
],
},
2023-01-12 12:00:44 +00:00
]);
2023-02-07 19:47:57 +00:00
const root = ReactDOM.createRoot(unwrap(document.getElementById("root")));
2022-12-18 14:51:47 +00:00
root.render(
2023-01-18 23:39:50 +00:00
<StrictMode>
2022-12-30 23:35:02 +00:00
<Provider store={Store}>
2023-05-16 17:54:49 +00:00
<IntlProvider>
2023-08-24 14:25:54 +00:00
<SnortContext.Provider value={System}>
<RouterProvider router={router} />
</SnortContext.Provider>
2023-05-16 17:54:49 +00:00
</IntlProvider>
2022-12-30 23:35:02 +00:00
</Provider>
2023-09-05 14:23:04 +00:00
</StrictMode>
2022-12-18 14:51:47 +00:00
);