Fix account state bugs
This commit is contained in:
@@ -19,10 +19,7 @@ import { identity } from "./lib/hooks"
|
|||||||
function Layout(props: { children?: any }) {
|
function Layout(props: { children?: any }) {
|
||||||
const location = useLocation()
|
const location = useLocation()
|
||||||
|
|
||||||
const usesAppShell = () => {
|
const usesAppShell = () => location.pathname.match(/^\/(relays|account|admin)/)
|
||||||
const path = location.pathname
|
|
||||||
return path.startsWith("/relays") || path.startsWith("/account") || path.startsWith("/admin")
|
|
||||||
}
|
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
// Reinitialize Preline components on route change
|
// Reinitialize Preline components on route change
|
||||||
@@ -32,7 +29,7 @@ function Layout(props: { children?: any }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div class="min-h-screen bg-gray-50">
|
<div class="min-h-screen bg-gray-50">
|
||||||
<Show when={identity() && usesAppShell()} fallback={<main>{props.children}</main>}>
|
<Show when={!identity.loading && identity() && usesAppShell()} fallback={<main>{props.children}</main>}>
|
||||||
<AppShell>{props.children}</AppShell>
|
<AppShell>{props.children}</AppShell>
|
||||||
</Show>
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
@@ -45,11 +42,11 @@ export default function App() {
|
|||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
if (!condition()) navigate("/", { replace: true })
|
if (!identity.loading && !condition()) navigate("/", { replace: true })
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Show when={condition()}>
|
<Show when={!identity.loading && condition()}>
|
||||||
<Page />
|
<Page />
|
||||||
</Show>
|
</Show>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -66,9 +66,26 @@ registerCommonAccountTypes(accountManager)
|
|||||||
|
|
||||||
export const [account, setAccount] = createSignal<IAccount | undefined>()
|
export const [account, setAccount] = createSignal<IAccount | undefined>()
|
||||||
|
|
||||||
export const [identity, setIdentity] = createSignal<Identity | 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 => {
|
accountManager.active$.subscribe(account => {
|
||||||
setAccount(account)
|
setAccount(account)
|
||||||
|
|
||||||
@@ -76,11 +93,11 @@ export const [identity, setIdentity] = createSignal<Identity | undefined>()
|
|||||||
|
|
||||||
if (account) {
|
if (account) {
|
||||||
localStorage.setItem("caravel.accounts.active", account.id)
|
localStorage.setItem("caravel.accounts.active", account.id)
|
||||||
getIdentity().then(setIdentity)
|
|
||||||
} else {
|
} else {
|
||||||
localStorage.removeItem("caravel.accounts.active")
|
localStorage.removeItem("caravel.accounts.active")
|
||||||
setIdentity(undefined)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
refetchIdentity()
|
||||||
})
|
})
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user