rewrite client providing & fix sign in redirect
This commit is contained in:
parent
942b9dcce8
commit
0caa9aab88
5 changed files with 106 additions and 53 deletions
|
@ -1,41 +1,78 @@
|
|||
import { Accessor, createMemo, createResource } from "solid-js";
|
||||
import {
|
||||
Accessor,
|
||||
createContext,
|
||||
createRenderEffect,
|
||||
createResource,
|
||||
Signal,
|
||||
useContext,
|
||||
} from "solid-js";
|
||||
import { Account } from "../accounts/stores";
|
||||
import { createRestAPIClient, mastodon } from "masto";
|
||||
import { useLocation, useNavigate } from "@solidjs/router";
|
||||
|
||||
const restfulCache: Record<string, mastodon.rest.Client> = {}
|
||||
const restfulCache: Record<string, mastodon.rest.Client> = {};
|
||||
|
||||
export function createMastoClientFor(account: Account) {
|
||||
const cacheKey = [account.site, account.accessToken].join('')
|
||||
const cache = restfulCache[cacheKey]
|
||||
const cacheKey = [account.site, account.accessToken].join("");
|
||||
const cache = restfulCache[cacheKey];
|
||||
if (cache) return cache;
|
||||
|
||||
const client = createRestAPIClient({
|
||||
url: account.site,
|
||||
accessToken: account.accessToken,
|
||||
})
|
||||
restfulCache[cacheKey] = client
|
||||
});
|
||||
restfulCache[cacheKey] = client;
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
export function useMastoClientFor(account: Accessor<Account>) {
|
||||
return createMemo(() => createMastoClientFor(account()))
|
||||
return client;
|
||||
}
|
||||
|
||||
export function createUnauthorizedClient(site: string) {
|
||||
const cache = restfulCache[site]
|
||||
const cache = restfulCache[site];
|
||||
if (cache) return cache;
|
||||
|
||||
const client = createRestAPIClient({
|
||||
url: site
|
||||
})
|
||||
restfulCache[site] = client
|
||||
url: site,
|
||||
});
|
||||
restfulCache[site] = client;
|
||||
|
||||
return client
|
||||
return client;
|
||||
}
|
||||
|
||||
export function useInstance(client: Accessor<mastodon.rest.Client>) {
|
||||
return createResource(client, async (client) => {
|
||||
return await client.v2.instance.fetch()
|
||||
})
|
||||
return await client.v2.instance.fetch();
|
||||
});
|
||||
}
|
||||
|
||||
export type Session = {
|
||||
account: Account;
|
||||
client: mastodon.rest.Client;
|
||||
};
|
||||
|
||||
const Context = /* @__PURE__ */ createContext<Signal<Session[]>>();
|
||||
|
||||
export const Provider = Context.Provider;
|
||||
|
||||
export function useSessions() {
|
||||
const [sessions] = useSessionsRw();
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
|
||||
createRenderEffect(() => {
|
||||
if (sessions().length > 0) return;
|
||||
navigate(
|
||||
"/accounts/sign-in?back=" + encodeURIComponent(location.pathname),
|
||||
{ replace: true },
|
||||
);
|
||||
});
|
||||
|
||||
return sessions;
|
||||
}
|
||||
|
||||
export function useSessionsRw() {
|
||||
const store = useContext(Context);
|
||||
if (!store) {
|
||||
throw new TypeError("sessions are not provided");
|
||||
}
|
||||
return store;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue