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