soapbox/src/hooks/useApi.ts

16 wiersze
584 B
TypeScript
Czysty Zwykły widok Historia

2024-10-11 05:46:48 +00:00
import { useMemo } from 'react';
import { MastodonClient } from 'soapbox/api/MastodonClient';
2024-10-09 06:46:46 +00:00
import { useAppSelector } from './useAppSelector';
import { useOwnAccount } from './useOwnAccount';
export function useApi(): MastodonClient {
2024-10-09 06:46:46 +00:00
const { account } = useOwnAccount();
const accessToken = useAppSelector((state) => account ? state.auth.users.get(account.url)?.access_token : undefined);
const baseUrl = account ? new URL(account.url).origin : location.origin;
2024-10-09 06:46:46 +00:00
2024-10-11 05:46:48 +00:00
return useMemo(() => {
return new MastodonClient(baseUrl, accessToken);
}, [baseUrl, accessToken]);
2024-10-09 06:46:46 +00:00
}