From da60702403188f409c918eeb42af239ad245981f Mon Sep 17 00:00:00 2001 From: thislight Date: Fri, 18 Oct 2024 11:58:17 +0800 Subject: [PATCH] add useSessionForAcctStr --- src/masto/clients.ts | 37 +++++++++++++++++++++++++++++++ src/timelines/TootBottomSheet.tsx | 18 ++------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/masto/clients.ts b/src/masto/clients.ts index 4ed109b..ebe7deb 100644 --- a/src/masto/clients.ts +++ b/src/masto/clients.ts @@ -1,6 +1,7 @@ import { Accessor, createContext, + createMemo, createRenderEffect, createResource, Signal, @@ -76,3 +77,39 @@ function useSessionsRaw() { } return store; } + +/** + * Get a session for the specific acct string. + * + * Acct string is a string in the pattern of `{username}@{site_with_protocol}`, + * like `@thislight@https://mastodon.social`, can be used to identify (tempoarily) + * an session on the tutu instance. + * + * The `site_with_protocol` is required. + * + * - If the username is present, the session matches the username and the site is returned; or, + * - If the username is not present, any session on the site is returned; or, + * - If no available session available for the pattern, an unauthorised session is returned. + * + * In an unauthorised session, the `.account` is `undefined` and the `client` is an + * unauthorised client for the site. This client may not available for some operations. + */ +export function useSessionForAcctStr(acct: Accessor) { + const allSessions = useSessions() + + return createMemo(() => { + const parts = acct().split("@", 2) + const [inputUsername, inputSite] = acct().split("@", 2); + const authedSession = allSessions().find( + (x) => + x.account.site === inputSite && + x.account.inf?.username === inputUsername, + ); + return ( + authedSession ?? { + client: createUnauthorizedClient(inputSite), + account: undefined, + } + ); + }); +} diff --git a/src/timelines/TootBottomSheet.tsx b/src/timelines/TootBottomSheet.tsx index caa0054..06add1c 100644 --- a/src/timelines/TootBottomSheet.tsx +++ b/src/timelines/TootBottomSheet.tsx @@ -15,7 +15,7 @@ import { ArrowBack as BackIcon, Close as CloseIcon, } from "@suid/icons-material"; -import { createUnauthorizedClient, useSessions } from "../masto/clients"; +import { useSessionForAcctStr } from "../masto/clients"; import { resolveCustomEmoji } from "../masto/toot"; import RegularToot from "./RegularToot"; import type { mastodon } from "masto"; @@ -45,24 +45,10 @@ const TootBottomSheet: Component = (props) => { tootReply?: boolean; }>(); const navigate = useNavigate(); - const allSession = useSessions(); const time = createTimeSource(); const [isInTyping, setInTyping] = createSignal(false); const acctText = () => decodeURIComponent(params.acct); - const session = () => { - const [inputUsername, inputSite] = acctText().split("@", 2); - const authedSession = allSession().find( - (x) => - x.account.site === inputSite && - x.account.inf?.username === inputUsername, - ); - return ( - authedSession ?? { - client: createUnauthorizedClient(inputSite), - account: undefined, - } - ); - }; + const session = useSessionForAcctStr(acctText) const pushedCount = () => { return location.state?.tootBottomSheetPushedCount || 0;