2019-11-02 02:27:36 -07:00
|
|
|
import { useState, useEffect } from 'react';
|
2021-06-05 07:29:32 -07:00
|
|
|
import { API_PATH } from '../constants/constants';
|
|
|
|
import { WALReplayStatus } from '../types/types';
|
2019-11-02 02:27:36 -07:00
|
|
|
|
2020-02-03 06:14:25 -08:00
|
|
|
export type APIResponse<T> = { status: string; data: T };
|
2019-11-12 05:35:47 -08:00
|
|
|
|
|
|
|
export interface FetchState<T> {
|
|
|
|
response: APIResponse<T>;
|
|
|
|
error?: Error;
|
|
|
|
isLoading: boolean;
|
|
|
|
}
|
|
|
|
|
2021-06-05 07:29:32 -07:00
|
|
|
export interface FetchStateReady {
|
|
|
|
ready: boolean;
|
|
|
|
isUnexpected: boolean;
|
|
|
|
isLoading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FetchStateReadyInterval {
|
|
|
|
ready: boolean;
|
|
|
|
isUnexpected: boolean;
|
|
|
|
walReplayStatus: WALReplayStatus;
|
|
|
|
}
|
|
|
|
|
2019-11-12 05:35:47 -08:00
|
|
|
export const useFetch = <T extends {}>(url: string, options?: RequestInit): FetchState<T> => {
|
2020-02-03 06:14:25 -08:00
|
|
|
const [response, setResponse] = useState<APIResponse<T>>({ status: 'start fetching' } as any);
|
2019-11-12 05:35:47 -08:00
|
|
|
const [error, setError] = useState<Error>();
|
2020-02-03 06:14:25 -08:00
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(true);
|
2019-11-02 02:27:36 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchData = async () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
try {
|
2020-02-03 06:14:25 -08:00
|
|
|
const res = await fetch(url, { cache: 'no-store', credentials: 'same-origin', ...options });
|
2019-11-02 02:27:36 -07:00
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(res.statusText);
|
|
|
|
}
|
2019-11-12 05:35:47 -08:00
|
|
|
const json = (await res.json()) as APIResponse<T>;
|
2019-11-02 02:27:36 -07:00
|
|
|
setResponse(json);
|
|
|
|
setIsLoading(false);
|
|
|
|
} catch (error) {
|
|
|
|
setError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fetchData();
|
|
|
|
}, [url, options]);
|
|
|
|
return { response, error, isLoading };
|
|
|
|
};
|
2021-06-05 07:29:32 -07:00
|
|
|
|
2021-06-15 13:37:16 -07:00
|
|
|
let wasReady = false;
|
2021-06-05 07:29:32 -07:00
|
|
|
|
|
|
|
// This is used on the starting page to periodically check if the server is ready yet,
|
|
|
|
// and check the status of the WAL replay.
|
|
|
|
export const useFetchReadyInterval = (pathPrefix: string, options?: RequestInit): FetchStateReadyInterval => {
|
|
|
|
const [ready, setReady] = useState<boolean>(false);
|
|
|
|
const [isUnexpected, setIsUnexpected] = useState<boolean>(false);
|
|
|
|
const [walReplayStatus, setWALReplayStatus] = useState<WALReplayStatus>({} as any);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-06-15 13:37:16 -07:00
|
|
|
if (wasReady) {
|
|
|
|
setReady(true);
|
|
|
|
} else {
|
|
|
|
// This helps avoid a memory leak.
|
|
|
|
let mounted = true;
|
2021-06-05 07:29:32 -07:00
|
|
|
|
2021-06-15 13:37:16 -07:00
|
|
|
const fetchStatus = async () => {
|
|
|
|
try {
|
|
|
|
let res = await fetch(`${pathPrefix}/-/ready`, { cache: 'no-store', credentials: 'same-origin', ...options });
|
|
|
|
if (res.status === 200) {
|
|
|
|
if (mounted) {
|
|
|
|
setReady(true);
|
|
|
|
}
|
|
|
|
wasReady = true;
|
|
|
|
clearInterval(interval);
|
|
|
|
} else if (res.status !== 503) {
|
|
|
|
if (mounted) {
|
|
|
|
setIsUnexpected(true);
|
|
|
|
}
|
|
|
|
clearInterval(interval);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (mounted) {
|
|
|
|
setIsUnexpected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = await fetch(`${pathPrefix}/${API_PATH}/status/walreplay`, {
|
|
|
|
cache: 'no-store',
|
|
|
|
credentials: 'same-origin',
|
|
|
|
});
|
|
|
|
if (res.ok) {
|
|
|
|
const data = (await res.json()) as WALReplayStatus;
|
|
|
|
if (mounted) {
|
|
|
|
setWALReplayStatus(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (mounted) {
|
|
|
|
setIsUnexpected(true);
|
2021-06-05 07:29:32 -07:00
|
|
|
}
|
2021-06-15 13:37:16 -07:00
|
|
|
clearInterval(interval);
|
|
|
|
return;
|
2021-06-05 07:29:32 -07:00
|
|
|
}
|
2021-06-15 13:37:16 -07:00
|
|
|
};
|
2021-06-05 07:29:32 -07:00
|
|
|
|
2021-06-15 13:37:16 -07:00
|
|
|
fetchStatus();
|
|
|
|
const interval = setInterval(fetchStatus, 1000);
|
|
|
|
return () => {
|
|
|
|
clearInterval(interval);
|
|
|
|
mounted = false;
|
|
|
|
};
|
|
|
|
}
|
2021-06-05 07:29:32 -07:00
|
|
|
}, [pathPrefix, options]);
|
2021-06-15 13:37:16 -07:00
|
|
|
|
2021-06-05 07:29:32 -07:00
|
|
|
return { ready, isUnexpected, walReplayStatus };
|
|
|
|
};
|