useInstance: don't go stale
This commit is contained in:
parent
fdc03fe610
commit
5477a3a8c6
|
@ -3,7 +3,7 @@ import { useQuery, UseQueryOptions } from '@tanstack/react-query';
|
||||||
import { useApi } from 'soapbox/hooks';
|
import { useApi } from 'soapbox/hooks';
|
||||||
import { InstanceV1, instanceV1Schema } from 'soapbox/schemas/instance';
|
import { InstanceV1, instanceV1Schema } from 'soapbox/schemas/instance';
|
||||||
|
|
||||||
interface Opts extends Pick<UseQueryOptions<unknown>, 'enabled' | 'retry' | 'retryOnMount'> {
|
interface Opts extends Pick<UseQueryOptions<unknown>, 'enabled' | 'retry' | 'retryOnMount' | 'staleTime'> {
|
||||||
/** The base URL of the instance. */
|
/** The base URL of the instance. */
|
||||||
baseUrl?: string;
|
baseUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { useQuery, UseQueryOptions } from '@tanstack/react-query';
|
||||||
import { useApi } from 'soapbox/hooks';
|
import { useApi } from 'soapbox/hooks';
|
||||||
import { InstanceV2, instanceV2Schema } from 'soapbox/schemas/instance';
|
import { InstanceV2, instanceV2Schema } from 'soapbox/schemas/instance';
|
||||||
|
|
||||||
interface Opts extends Pick<UseQueryOptions<unknown>, 'enabled' | 'retry' | 'retryOnMount'> {
|
interface Opts extends Pick<UseQueryOptions<unknown>, 'enabled' | 'retry' | 'retryOnMount' | 'staleTime'> {
|
||||||
/** The base URL of the instance. */
|
/** The base URL of the instance. */
|
||||||
baseUrl?: string;
|
baseUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,14 @@ import { instanceV2Schema, upgradeInstance } from 'soapbox/schemas/instance';
|
||||||
|
|
||||||
import { useAppDispatch } from './useAppDispatch';
|
import { useAppDispatch } from './useAppDispatch';
|
||||||
|
|
||||||
interface Opts extends Pick<UseQueryOptions<unknown>, 'enabled' | 'retryOnMount'> {
|
interface Opts extends Pick<UseQueryOptions<unknown>, 'enabled' | 'retryOnMount' | 'staleTime'> {
|
||||||
/** The base URL of the instance. */
|
/** The base URL of the instance. */
|
||||||
baseUrl?: string;
|
baseUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the Instance for the current backend. */
|
/** Get the Instance for the current backend. */
|
||||||
export function useInstance(opts: Opts = {}) {
|
export function useInstance(opts: Opts = {}) {
|
||||||
const { baseUrl, retryOnMount = false } = opts;
|
const { baseUrl, retryOnMount = false, staleTime = Infinity } = opts;
|
||||||
|
|
||||||
function retry(failureCount: number, error: Error): boolean {
|
function retry(failureCount: number, error: Error): boolean {
|
||||||
if (error instanceof HTTPError && error.response.status === 404) {
|
if (error instanceof HTTPError && error.response.status === 404) {
|
||||||
|
@ -25,8 +25,8 @@ export function useInstance(opts: Opts = {}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const v2 = useInstanceV2({ baseUrl, retry, retryOnMount });
|
const v2 = useInstanceV2({ baseUrl, retry, retryOnMount, staleTime });
|
||||||
const v1 = useInstanceV1({ baseUrl, retry, retryOnMount, enabled: v2.isError });
|
const v1 = useInstanceV1({ baseUrl, retry, retryOnMount, staleTime, enabled: v2.isError });
|
||||||
|
|
||||||
const instance = useMemo(() => {
|
const instance = useMemo(() => {
|
||||||
if (v2.instance) {
|
if (v2.instance) {
|
||||||
|
|
Loading…
Reference in New Issue