71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import { useEffect } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
|
|
const client = createApiClient();
|
|
|
|
export function usePublicationQueries() {
|
|
const brand = useQuery<Awaited<ReturnType<typeof client.getAdminBrand>>>({
|
|
queryKey: ["publication-brand"],
|
|
queryFn: () => client.getAdminBrand(),
|
|
});
|
|
const workspace = useQuery<Awaited<ReturnType<typeof client.getAdminWorkspace>>>({
|
|
queryKey: ["publication-workspace"],
|
|
queryFn: () => client.getAdminWorkspace(),
|
|
});
|
|
const readiness = useQuery<Awaited<ReturnType<typeof client.getAdminReleaseReadiness>>>({
|
|
queryKey: ["publication-readiness"],
|
|
queryFn: () => client.getAdminReleaseReadiness(),
|
|
});
|
|
const releases = useQuery<Awaited<ReturnType<typeof client.listAdminReleases>>>({
|
|
queryKey: ["publication-releases"],
|
|
queryFn: () => client.listAdminReleases(),
|
|
});
|
|
const miniAppReleases = useQuery<Awaited<ReturnType<typeof client.listAdminMiniAppReleases>>>({
|
|
queryKey: ["publication-miniapp-releases"],
|
|
queryFn: () => client.listAdminMiniAppReleases(),
|
|
});
|
|
const miniAppSettings = useQuery<Awaited<ReturnType<typeof client.getAdminMiniAppSettings>>>({
|
|
queryKey: ["publication-miniapp-settings"],
|
|
queryFn: () => client.getAdminMiniAppSettings(),
|
|
});
|
|
|
|
useEffect(() => {
|
|
const building = miniAppReleases.data?.find(
|
|
(task) =>
|
|
task.buildStatus === "building" &&
|
|
Date.now() - new Date(task.updatedAt).getTime() < 30 * 60 * 1000,
|
|
);
|
|
if (!building) return;
|
|
const timer = setInterval(() => void miniAppReleases.refetch(), 1500);
|
|
return () => clearInterval(timer);
|
|
}, [miniAppReleases]);
|
|
|
|
const loading = [brand, workspace, readiness, releases, miniAppReleases, miniAppSettings].some(
|
|
(query) => query.isLoading,
|
|
);
|
|
const error = [brand, workspace, readiness, releases, miniAppReleases, miniAppSettings].find(
|
|
(query) => query.isError,
|
|
)?.error;
|
|
const retry = () => {
|
|
void brand.refetch();
|
|
void workspace.refetch();
|
|
void readiness.refetch();
|
|
void releases.refetch();
|
|
void miniAppReleases.refetch();
|
|
void miniAppSettings.refetch();
|
|
};
|
|
|
|
return {
|
|
brand,
|
|
workspace,
|
|
readiness,
|
|
releases,
|
|
miniAppReleases,
|
|
miniAppSettings,
|
|
loading,
|
|
error,
|
|
retry,
|
|
};
|
|
}
|