Split up some state stuff
This commit is contained in:
@@ -3,7 +3,6 @@ import { Router, Route, useLocation, useNavigate } from "@solidjs/router"
|
||||
import type { Component } from "solid-js"
|
||||
import AppShell from "./components/AppShell"
|
||||
import Home from "./pages/Home"
|
||||
import Login from "./pages/Login"
|
||||
import RelayList from "./pages/relays/RelayList"
|
||||
import RelayNew from "./pages/relays/RelayNew"
|
||||
import RelayDetail from "./pages/relays/RelayDetail"
|
||||
@@ -59,7 +58,6 @@ export default function App() {
|
||||
return (
|
||||
<Router root={Layout}>
|
||||
<Route path="/" component={Home} />
|
||||
<Route path="/login" component={Login} />
|
||||
<Route path="/relays" component={requireTenant(RelayList)} />
|
||||
<Route path="/relays/new" component={requireTenant(RelayNew)} />
|
||||
<Route path="/relays/:id" component={requireTenant(RelayDetail)} />
|
||||
|
||||
+15
-78
@@ -1,20 +1,12 @@
|
||||
import { createEffect, createResource, createSignal, onCleanup } from "solid-js"
|
||||
import { AccountManager } from "applesauce-accounts"
|
||||
import type { IAccount } from "applesauce-accounts"
|
||||
import { registerCommonAccountTypes } from "applesauce-accounts/accounts"
|
||||
import { EventStore } from "applesauce-core"
|
||||
import { getProfilePicture } from "applesauce-core/helpers/profile"
|
||||
import { createOutboxMap, selectOptimalRelays, setFallbackRelays } from "applesauce-core/helpers/relay-selection"
|
||||
import { includeMailboxes } from "applesauce-core/observable"
|
||||
import { createEventLoaderForStore } from "applesauce-loaders/loaders"
|
||||
import { Relay as NostrRelay, RelayManagement } from "applesauce-relay"
|
||||
import { RelayPool } from "applesauce-relay"
|
||||
import { NostrConnectSigner } from "applesauce-signers"
|
||||
import { map, of } from "rxjs"
|
||||
import {
|
||||
createRelay,
|
||||
deactivateRelay,
|
||||
getIdentity,
|
||||
getRelay,
|
||||
getTenant,
|
||||
listRelays,
|
||||
@@ -28,77 +20,22 @@ import {
|
||||
type Tenant,
|
||||
type UpdateRelayInput,
|
||||
} from "./api"
|
||||
import { account, eventStore, pool } from "./state"
|
||||
|
||||
export type UnsignedEvent = {
|
||||
kind: number
|
||||
content: string
|
||||
created_at: number
|
||||
tags: string[][]
|
||||
}
|
||||
|
||||
export type SignedEvent = UnsignedEvent & {
|
||||
id: string
|
||||
pubkey: string
|
||||
sig: string
|
||||
}
|
||||
|
||||
export type EventSigner = {
|
||||
signEvent(event: UnsignedEvent): Promise<SignedEvent>
|
||||
}
|
||||
|
||||
export const PLATFORM_NAME = import.meta.env.VITE_PLATFORM_NAME || "Caravel"
|
||||
|
||||
export const eventStore = new EventStore()
|
||||
export const pool = new RelayPool()
|
||||
|
||||
createEventLoaderForStore(eventStore, pool, {
|
||||
lookupRelays: ["wss://purplepag.es/", "wss://relay.damus.io/", "wss://indexer.coracle.social/"],
|
||||
extraRelays: ["wss://relay.damus.io/", "wss://nos.lol/", "wss://relay.primal.net/"],
|
||||
})
|
||||
|
||||
NostrConnectSigner.subscriptionMethod = pool.subscription.bind(pool)
|
||||
NostrConnectSigner.publishMethod = pool.publish.bind(pool)
|
||||
|
||||
export const accountManager = new AccountManager()
|
||||
|
||||
registerCommonAccountTypes(accountManager)
|
||||
|
||||
export const [account, setAccount] = createSignal<IAccount | undefined>()
|
||||
|
||||
export const [identity, { refetch: refetchIdentity, mutate: setIdentity }] = createResource(
|
||||
() => account()?.pubkey,
|
||||
pubkey => {
|
||||
if (pubkey) return getIdentity()
|
||||
}
|
||||
)
|
||||
|
||||
;(() => {
|
||||
try {
|
||||
accountManager.fromJSON(JSON.parse(localStorage.getItem("caravel.accounts")!))
|
||||
} catch {
|
||||
// pass
|
||||
}
|
||||
|
||||
const active = localStorage.getItem("caravel.accounts.active")
|
||||
|
||||
if (active) {
|
||||
accountManager.setActive(active)
|
||||
}
|
||||
|
||||
accountManager.active$.subscribe(account => {
|
||||
setAccount(account)
|
||||
|
||||
localStorage.setItem("caravel.accounts", JSON.stringify(accountManager.toJSON(true)))
|
||||
|
||||
if (account) {
|
||||
localStorage.setItem("caravel.accounts.active", account.id)
|
||||
} else {
|
||||
localStorage.removeItem("caravel.accounts.active")
|
||||
}
|
||||
|
||||
refetchIdentity()
|
||||
})
|
||||
})()
|
||||
export {
|
||||
PLATFORM_NAME,
|
||||
accountManager,
|
||||
account,
|
||||
setAccount,
|
||||
identity,
|
||||
refetchIdentity,
|
||||
setIdentity,
|
||||
eventStore,
|
||||
pool,
|
||||
type EventSigner,
|
||||
type SignedEvent,
|
||||
type UnsignedEvent,
|
||||
} from "./state"
|
||||
|
||||
export function useProfilePicture(pubkey: () => string | undefined) {
|
||||
const [picture, setPicture] = createSignal<string | undefined>()
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { createResource, createSignal } from "solid-js"
|
||||
import { AccountManager } from "applesauce-accounts"
|
||||
import type { IAccount } from "applesauce-accounts"
|
||||
import { registerCommonAccountTypes } from "applesauce-accounts/accounts"
|
||||
import { EventStore } from "applesauce-core"
|
||||
import { createEventLoaderForStore } from "applesauce-loaders/loaders"
|
||||
import { RelayPool } from "applesauce-relay"
|
||||
import { NostrConnectSigner } from "applesauce-signers"
|
||||
import { getIdentity } from "./api"
|
||||
|
||||
export type UnsignedEvent = {
|
||||
kind: number
|
||||
content: string
|
||||
created_at: number
|
||||
tags: string[][]
|
||||
}
|
||||
|
||||
export type SignedEvent = UnsignedEvent & {
|
||||
id: string
|
||||
pubkey: string
|
||||
sig: string
|
||||
}
|
||||
|
||||
export type EventSigner = {
|
||||
signEvent(event: UnsignedEvent): Promise<SignedEvent>
|
||||
}
|
||||
|
||||
export const PLATFORM_NAME = import.meta.env.VITE_PLATFORM_NAME || "Caravel"
|
||||
|
||||
export const eventStore = new EventStore()
|
||||
export const pool = new RelayPool()
|
||||
|
||||
createEventLoaderForStore(eventStore, pool, {
|
||||
lookupRelays: ["wss://purplepag.es/", "wss://relay.damus.io/", "wss://indexer.coracle.social/"],
|
||||
extraRelays: ["wss://relay.damus.io/", "wss://nos.lol/", "wss://relay.primal.net/"],
|
||||
})
|
||||
|
||||
NostrConnectSigner.subscriptionMethod = pool.subscription.bind(pool)
|
||||
NostrConnectSigner.publishMethod = pool.publish.bind(pool)
|
||||
|
||||
export const accountManager = new AccountManager()
|
||||
|
||||
registerCommonAccountTypes(accountManager)
|
||||
|
||||
export const [account, setAccount] = createSignal<IAccount | undefined>()
|
||||
|
||||
export const [identity, { refetch: refetchIdentity, mutate: setIdentity }] = createResource(
|
||||
() => account()?.pubkey,
|
||||
pubkey => {
|
||||
if (pubkey) return getIdentity()
|
||||
}
|
||||
)
|
||||
|
||||
;(() => {
|
||||
try {
|
||||
accountManager.fromJSON(JSON.parse(localStorage.getItem("caravel.accounts")!))
|
||||
} catch {
|
||||
// pass
|
||||
}
|
||||
|
||||
const active = localStorage.getItem("caravel.accounts.active")
|
||||
|
||||
if (active) {
|
||||
accountManager.setActive(active)
|
||||
}
|
||||
|
||||
accountManager.active$.subscribe(account => {
|
||||
setAccount(account)
|
||||
|
||||
localStorage.setItem("caravel.accounts", JSON.stringify(accountManager.toJSON(true)))
|
||||
|
||||
if (account) {
|
||||
localStorage.setItem("caravel.accounts.active", account.id)
|
||||
} else {
|
||||
localStorage.removeItem("caravel.accounts.active")
|
||||
}
|
||||
|
||||
refetchIdentity()
|
||||
})
|
||||
})()
|
||||
@@ -5,7 +5,7 @@ import ExternalLinkIcon from "../components/ExternalLinkIcon"
|
||||
import PricingTable from "../components/PricingTable"
|
||||
import RelayForm, { type RelayFormValues } from "../components/RelayForm"
|
||||
import Modal from "../components/Modal"
|
||||
import Login from "./Login"
|
||||
import Login from "../views/Login"
|
||||
import { account, createRelayForActiveTenant } from "../lib/hooks"
|
||||
import { slugify } from "../lib/slugify"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user