add useSessionForAcctStr

This commit is contained in:
thislight 2024-10-18 11:58:17 +08:00
parent 634060ed46
commit da60702403
2 changed files with 39 additions and 16 deletions

View file

@ -1,6 +1,7 @@
import { import {
Accessor, Accessor,
createContext, createContext,
createMemo,
createRenderEffect, createRenderEffect,
createResource, createResource,
Signal, Signal,
@ -76,3 +77,39 @@ function useSessionsRaw() {
} }
return store; 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<string>) {
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,
}
);
});
}

View file

@ -15,7 +15,7 @@ import {
ArrowBack as BackIcon, ArrowBack as BackIcon,
Close as CloseIcon, Close as CloseIcon,
} from "@suid/icons-material"; } from "@suid/icons-material";
import { createUnauthorizedClient, useSessions } from "../masto/clients"; import { useSessionForAcctStr } from "../masto/clients";
import { resolveCustomEmoji } from "../masto/toot"; import { resolveCustomEmoji } from "../masto/toot";
import RegularToot from "./RegularToot"; import RegularToot from "./RegularToot";
import type { mastodon } from "masto"; import type { mastodon } from "masto";
@ -45,24 +45,10 @@ const TootBottomSheet: Component = (props) => {
tootReply?: boolean; tootReply?: boolean;
}>(); }>();
const navigate = useNavigate(); const navigate = useNavigate();
const allSession = useSessions();
const time = createTimeSource(); const time = createTimeSource();
const [isInTyping, setInTyping] = createSignal(false); const [isInTyping, setInTyping] = createSignal(false);
const acctText = () => decodeURIComponent(params.acct); const acctText = () => decodeURIComponent(params.acct);
const session = () => { const session = useSessionForAcctStr(acctText)
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 pushedCount = () => { const pushedCount = () => {
return location.state?.tootBottomSheetPushedCount || 0; return location.state?.tootBottomSheetPushedCount || 0;