2024-10-11 05:46:48 +00:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
2024-10-11 03:15:21 +00:00
|
|
|
import { MastodonClient } from 'soapbox/api/MastodonClient';
|
2024-10-11 20:34:46 +00:00
|
|
|
import * as BuildConfig from 'soapbox/build-config';
|
2022-08-09 15:24:43 +00:00
|
|
|
|
2024-10-09 06:46:46 +00:00
|
|
|
import { useAppSelector } from './useAppSelector';
|
|
|
|
import { useOwnAccount } from './useOwnAccount';
|
2022-08-09 15:24:43 +00:00
|
|
|
|
2024-10-11 03:15:21 +00:00
|
|
|
export function useApi(): MastodonClient {
|
2024-10-09 06:46:46 +00:00
|
|
|
const { account } = useOwnAccount();
|
2024-10-11 08:44:32 +00:00
|
|
|
const authUserUrl = useAppSelector((state) => state.auth.me);
|
2024-10-23 18:03:24 +00:00
|
|
|
const accessToken = useAppSelector((state) => account ? state.auth.users[account.url]?.access_token : undefined);
|
2024-10-11 20:34:46 +00:00
|
|
|
const baseUrl = new URL(BuildConfig.BACKEND_URL || account?.url || authUserUrl || location.origin).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
|
|
|
}
|