From e882a8c0e80168ad301aa4d515b17c2b33ef84d9 Mon Sep 17 00:00:00 2001 From: thislight Date: Sun, 14 Jul 2024 21:36:41 +0800 Subject: [PATCH] accounts/stores: added useAccts --- src/accounts/stores.ts | 19 +++++++++++++++++-- src/timelines/Home.tsx | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/accounts/stores.ts b/src/accounts/stores.ts index a990fc7..96c6499 100644 --- a/src/accounts/stores.ts +++ b/src/accounts/stores.ts @@ -1,6 +1,9 @@ import { persistentAtom } from "@nanostores/persistent"; +import { useStore } from "@nanostores/solid"; +import { useNavigate } from "@solidjs/router"; import { createOAuthAPIClient, createRestAPIClient } from "masto"; import { action } from "nanostores"; +import { createRenderEffect } from "solid-js"; export type Account = { site: string; @@ -25,7 +28,7 @@ interface OAuth2AccessToken { async function oauth2TokenViaAuthCode(app: RegisteredApp, authCode: string) { const resp = await fetch(new URL("./oauth/token", app.site), { - method: 'post', + method: "post", body: JSON.stringify({ grant_type: "authorization_code", code: authCode, @@ -97,7 +100,7 @@ export const $registeredApps = persistentAtom<{ async function getAppAccessToken(app: RegisteredApp) { const resp = await fetch(new URL("./oauth/token", app.site), { - method: 'post', + method: "post", headers: { "Content-Type": "application/json", }, @@ -172,3 +175,15 @@ export const getOrRegisterApp = action( return all[site]; }, ); + +export function useAccts() { + const accts = useStore($accounts); + const naviagte = useNavigate(); + + createRenderEffect(() => { + if (accts().length > 0) return; + naviagte("/accounts/sign-in"); + }); + + return accts; +} diff --git a/src/timelines/Home.tsx b/src/timelines/Home.tsx index 743b5af..696c0a2 100644 --- a/src/timelines/Home.tsx +++ b/src/timelines/Home.tsx @@ -8,7 +8,7 @@ import { untrack, onMount, } from "solid-js"; -import { $accounts } from "../accounts/stores"; +import { $accounts, useAccts } from "../accounts/stores"; import { useDocumentTitle } from "../utils"; import { useStore } from "@nanostores/solid"; import { useMastoClientFor } from "../masto/clients"; @@ -152,7 +152,7 @@ const TimelinePanel: Component<{ const Home: Component = () => { let panelList: HTMLDivElement; useDocumentTitle("Timelines"); - const accounts = useStore($accounts); + const accounts = useAccts(); const now = createTimeSource(); const client = useMastoClientFor(() => accounts()[0]);