Files
calendar/packages/platform/atoms/hooks/useOAuthClient.ts
T
33aedbae20 fix: booker embed (#22898)
* fix: make organizationId optional number type in BookerEmbed

* feat: dont call useMe for embeds

* useOAuthClient hook should re-run when url is set

* feat: Remove `?orgId=0` from /public event query params

---------

Co-authored-by: Rajiv Sahal <sahalrajiv-extc@atharvacoe.ac.in>
2025-08-07 10:29:22 +02:00

62 lines
1.7 KiB
TypeScript

import type { AxiosError } from "axios";
import { useState, useEffect } from "react";
import { usePrevious } from "react-use";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse } from "@calcom/platform-types";
import http from "../lib/http";
export interface useOAuthClientProps {
isEmbed?: boolean;
clientId: string;
apiUrl?: string;
refreshUrl?: string;
onError: (error: string) => void;
onSuccess: (data: { client: string; organizationId: number; name: string }) => void;
}
export const useOAuthClient = ({
isEmbed,
clientId,
apiUrl,
refreshUrl,
onError,
onSuccess,
}: useOAuthClientProps) => {
const prevClientId = usePrevious(clientId);
const [isInit, setIsInit] = useState<boolean>(false);
useEffect(() => {
if (apiUrl) {
http.setUrl(apiUrl);
setIsInit(true);
}
if (refreshUrl) {
http.setRefreshUrl(refreshUrl);
}
}, [apiUrl, refreshUrl]);
useEffect(() => {
if (!isEmbed && clientId && http.getUrl() && prevClientId !== clientId) {
try {
http
.get<ApiResponse<{ client: string; organizationId: number; name: string }>>(`/provider/${clientId}`)
.then((response) => {
if (response.data.status === SUCCESS_STATUS) {
onSuccess(response.data.data);
}
http.setClientIdHeader(clientId);
})
.catch((err: AxiosError) => {
if (err.response?.status === 401) {
onError("Invalid oAuth Client.");
}
});
} catch (err) {
console.error(err);
}
}
}, [isEmbed, clientId, onError, prevClientId, onSuccess, http.getUrl()]);
return { isInit };
};