2023-02-07 20:04:50 +00:00
|
|
|
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-24 20:28:39 +00:00
|
|
|
import { compress, expand_filter, flat_merge, get_diff, pow, default as wasmInit } from "@snort/system-wasm";
|
2023-09-22 19:28:56 +00:00
|
|
|
import WasmPath from "@snort/system-wasm/pkg/system_wasm_bg.wasm";
|
2023-09-06 12:45:25 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
import { StrictMode } from "react";
|
|
|
|
import * as ReactDOM from "react-dom/client";
|
|
|
|
import { createBrowserRouter, RouterProvider } from "react-router-dom";
|
2023-09-24 20:28:39 +00:00
|
|
|
import {
|
|
|
|
NostrSystem,
|
|
|
|
ProfileLoaderService,
|
|
|
|
QueryOptimizer,
|
|
|
|
FlatReqFilter,
|
|
|
|
ReqFilter,
|
|
|
|
PowMiner,
|
|
|
|
NostrEvent,
|
2023-10-10 09:37:53 +00:00
|
|
|
mapEventToProfile,
|
2023-09-24 20:28:39 +00:00
|
|
|
} from "@snort/system";
|
2023-08-26 21:48:15 +00:00
|
|
|
import { SnortContext } from "@snort/system-react";
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-02-07 20:04:50 +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-02-07 20:04:50 +00:00
|
|
|
import Layout from "Pages/Layout";
|
2023-05-15 17:38:26 +00:00
|
|
|
import LoginPage from "Pages/LoginPage";
|
2023-10-06 10:13:49 +00:00
|
|
|
import ProfilePage from "Pages/Profile/ProfilePage";
|
2023-09-07 14:54:25 +00:00
|
|
|
import { RootRoutes, RootTabRoutes } from "Pages/Root";
|
2023-02-07 20:04:50 +00:00
|
|
|
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";
|
2023-02-07 20:04:50 +00:00
|
|
|
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-09-28 09:26:10 +00:00
|
|
|
import { ThreadRoute } from "Element/Event/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";
|
2023-05-25 09:31:47 +00:00
|
|
|
import { db } from "Db";
|
2023-10-09 13:35:21 +00:00
|
|
|
import { preload, RelayMetrics, SystemDb, UserCache, UserRelays } from "Cache";
|
2023-05-25 09:31:47 +00:00
|
|
|
import { LoginStore } from "Login";
|
2023-09-07 14:54:25 +00:00
|
|
|
import { SnortDeckLayout } from "Pages/DeckLayout";
|
2023-09-27 08:41:26 +00:00
|
|
|
import FreeNostrAddressPage from "./Pages/FreeNostrAddressPage";
|
2023-10-11 14:41:36 +00:00
|
|
|
import { removeUndefined } from "@snort/shared";
|
2023-05-30 13:48:38 +00:00
|
|
|
|
2023-09-11 14:33:16 +00:00
|
|
|
const WasmQueryOptimizer = {
|
|
|
|
expandFilter: (f: ReqFilter) => {
|
|
|
|
return expand_filter(f) as Array<FlatReqFilter>;
|
|
|
|
},
|
|
|
|
getDiff: (prev: Array<ReqFilter>, next: Array<ReqFilter>) => {
|
|
|
|
return get_diff(prev, next) as Array<FlatReqFilter>;
|
|
|
|
},
|
|
|
|
flatMerge: (all: Array<FlatReqFilter>) => {
|
|
|
|
return flat_merge(all) as Array<ReqFilter>;
|
|
|
|
},
|
|
|
|
compress: (all: Array<ReqFilter>) => {
|
|
|
|
return compress(all) as Array<ReqFilter>;
|
2023-09-12 14:02:16 +00:00
|
|
|
},
|
2023-09-11 14:33:16 +00:00
|
|
|
} as QueryOptimizer;
|
|
|
|
|
2023-09-24 20:28:39 +00:00
|
|
|
export class WasmPowWorker implements PowMiner {
|
|
|
|
minePow(ev: NostrEvent, target: number): Promise<NostrEvent> {
|
|
|
|
const res = pow(ev, target);
|
|
|
|
return Promise.resolve(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-22 15:55:26 +00:00
|
|
|
queryOptimizer: WasmQueryOptimizer,
|
2023-10-09 13:35:21 +00:00
|
|
|
db: SystemDb,
|
2023-06-15 11:03:05 +00:00
|
|
|
authHandler: async (c, r) => {
|
2023-09-21 21:34:36 +00:00
|
|
|
const { id } = LoginStore.snapshot();
|
|
|
|
const pub = LoginStore.getPublisher(id);
|
|
|
|
if (pub) {
|
2023-06-15 11:03:05 +00:00
|
|
|
return await pub.nip42Auth(c, r);
|
|
|
|
}
|
|
|
|
},
|
2023-05-30 13:48:38 +00:00
|
|
|
});
|
|
|
|
|
2023-10-10 09:37:53 +00:00
|
|
|
async function fetchProfile(key: string) {
|
|
|
|
const rsp = await fetch(`${CONFIG.httpCache}/profile/${key}`);
|
|
|
|
if (rsp.ok) {
|
|
|
|
try {
|
|
|
|
const data = (await rsp.json()) as NostrEvent;
|
|
|
|
if (data) {
|
|
|
|
return mapEventToProfile(data);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add profile loader fn
|
|
|
|
*/
|
|
|
|
if (CONFIG.httpCache) {
|
|
|
|
System.ProfileLoader.loaderFn = async (keys: Array<string>) => {
|
2023-10-11 14:41:36 +00:00
|
|
|
return removeUndefined(await Promise.all(keys.map(a => fetchProfile(a))));
|
2023-10-10 09:37:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
}
|
2023-09-12 21:53:32 +00:00
|
|
|
|
|
|
|
// inject analytics script
|
|
|
|
// <script defer data-domain="snort.social" src="http://analytics.v0l.io/js/script.js"></script>
|
|
|
|
if (login.preferences.telemetry ?? true) {
|
|
|
|
const sc = document.createElement("script");
|
2023-09-12 22:02:51 +00:00
|
|
|
sc.src = "https://analytics.v0l.io/js/script.js";
|
2023-09-12 21:53:32 +00:00
|
|
|
sc.defer = true;
|
2023-10-12 13:45:30 +00:00
|
|
|
sc.setAttribute("data-domain", CONFIG.hostname);
|
2023-09-12 21:53:32 +00:00
|
|
|
document.head.appendChild(sc);
|
|
|
|
}
|
2023-09-05 13:57:50 +00:00
|
|
|
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 />,
|
2023-05-25 09:31:47 +00:00
|
|
|
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();
|
2023-05-25 09:31:47 +00:00
|
|
|
}
|
|
|
|
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",
|
2023-02-07 20:04:50 +00:00
|
|
|
element: <LoginPage />,
|
2023-01-12 12:00:44 +00:00
|
|
|
},
|
2023-02-03 18:20:17 +00:00
|
|
|
{
|
|
|
|
path: "/help",
|
2023-02-07 20:04:50 +00:00
|
|
|
element: <HelpPage />,
|
2023-02-03 18:20:17 +00:00
|
|
|
},
|
2023-01-12 12:00:44 +00:00
|
|
|
{
|
|
|
|
path: "/e/:id",
|
2023-09-07 14:54:25 +00:00
|
|
|
element: <ThreadRoute />,
|
2023-01-12 12:00:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/p/:id",
|
2023-02-07 20:04:50 +00:00
|
|
|
element: <ProfilePage />,
|
2023-01-12 12:00:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/notifications",
|
2023-02-07 20:04:50 +00:00
|
|
|
element: <NotificationsPage />,
|
2023-01-12 12:00:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/settings",
|
2023-01-20 17:07:14 +00:00
|
|
|
element: <SettingsPage />,
|
2023-02-07 20:04:50 +00:00
|
|
|
children: SettingsRoutes,
|
2023-01-12 15:35:42 +00:00
|
|
|
},
|
2023-09-27 08:41:26 +00:00
|
|
|
{
|
|
|
|
path: "/free-nostr-address",
|
|
|
|
element: <FreeNostrAddressPage />,
|
|
|
|
},
|
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?",
|
2023-02-07 20:04:50 +00:00
|
|
|
element: <MessagesPage />,
|
2023-01-12 09:48:39 +00:00
|
|
|
},
|
2023-01-19 00:03:24 +00:00
|
|
|
{
|
|
|
|
path: "/donate",
|
2023-02-07 20:04:50 +00:00
|
|
|
element: <DonatePage />,
|
2023-01-19 18:00:56 +00:00
|
|
|
},
|
2023-01-24 12:33:18 +00:00
|
|
|
{
|
2023-01-28 15:40:19 +00:00
|
|
|
path: "/search/:keyword?",
|
2023-02-07 20:04:50 +00:00
|
|
|
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-10-10 09:37:53 +00:00
|
|
|
...(CONFIG.features.subscriptions ? 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-02-07 20:04:50 +00:00
|
|
|
],
|
|
|
|
},
|
2023-09-07 14:54:25 +00:00
|
|
|
{
|
|
|
|
path: "/deck",
|
|
|
|
element: <SnortDeckLayout />,
|
|
|
|
loader: async () => {
|
|
|
|
if (!didInit) {
|
|
|
|
didInit = true;
|
|
|
|
return await initSite();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2023-09-14 18:45:29 +00:00
|
|
|
children: RootTabRoutes,
|
|
|
|
},
|
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>
|
2023-09-21 20:02:59 +00:00
|
|
|
<IntlProvider>
|
|
|
|
<SnortContext.Provider value={System}>
|
|
|
|
<RouterProvider router={router} />
|
|
|
|
</SnortContext.Provider>
|
|
|
|
</IntlProvider>
|
2023-09-12 21:58:37 +00:00
|
|
|
</StrictMode>,
|
2022-12-18 14:51:47 +00:00
|
|
|
);
|