2023-02-16 22:39:57 +00:00
import type { UseMutationOptions } from "@tanstack/react-query" ;
import { useMutation } from "@tanstack/react-query" ;
2022-03-23 22:00:30 +00:00
import type { IntegrationOAuthCallbackState } from "@calcom/app-store/types" ;
2022-04-04 20:26:14 +00:00
import { WEBAPP_URL } from "@calcom/lib/constants" ;
2023-02-16 22:39:57 +00:00
import type { App } from "@calcom/types/App" ;
2022-03-23 22:00:30 +00:00
2022-09-15 19:53:09 +00:00
import getInstalledAppPath from "./getInstalledAppPath" ;
2022-11-13 15:51:31 +00:00
function gotoUrl ( url : string , newTab? : boolean ) {
if ( newTab ) {
window . open ( url , "_blank" ) ;
return ;
}
window . location . href = url ;
}
2022-11-17 21:38:34 +00:00
type CustomUseMutationOptions =
| Omit < UseMutationOptions < unknown , unknown , unknown , unknown > , "mutationKey" | "mutationFn" | "onSuccess" >
| undefined ;
2022-12-05 13:13:01 +00:00
type AddAppMutationData = { setupPending : boolean } | void ;
2022-11-17 21:38:34 +00:00
type UseAddAppMutationOptions = CustomUseMutationOptions & {
2023-01-10 02:01:57 +00:00
onSuccess ? : ( data : AddAppMutationData ) = > void ;
installGoogleVideo? : boolean ;
2022-12-21 20:31:09 +00:00
returnTo? : string ;
2022-11-17 21:38:34 +00:00
} ;
2022-12-21 20:31:09 +00:00
function useAddAppMutation ( _type : App [ "type" ] | null , allOptions? : UseAddAppMutationOptions ) {
const { returnTo , . . . options } = allOptions || { } ;
2022-10-14 16:24:43 +00:00
const mutation = useMutation <
2022-12-05 13:13:01 +00:00
AddAppMutationData ,
2022-10-14 16:24:43 +00:00
Error ,
{ type ? : App [ "type" ] ; variant? : string ; slug? : string ; isOmniInstall? : boolean } | ""
> ( async ( variables ) = > {
let type : string | null | undefined ;
let isOmniInstall ;
if ( variables === "" ) {
type = _type ;
} else {
isOmniInstall = variables . isOmniInstall ;
type = variables . type ;
}
2022-11-22 20:44:08 +00:00
if ( type ? . endsWith ( "_other_calendar" ) ) {
type = type . split ( "_other_calendar" ) [ 0 ] ;
2022-11-17 21:38:34 +00:00
}
2023-01-10 02:01:57 +00:00
if ( options ? . installGoogleVideo && type !== "google_calendar" )
throw new Error ( "Could not install Google Meet" ) ;
2022-10-14 16:24:43 +00:00
const state : IntegrationOAuthCallbackState = {
returnTo :
2022-12-21 20:31:09 +00:00
returnTo ||
2022-10-14 16:24:43 +00:00
WEBAPP_URL +
2022-12-21 20:31:09 +00:00
getInstalledAppPath (
{ variant : variables && variables . variant , slug : variables && variables . slug } ,
location . search
) ,
2023-01-10 02:01:57 +00:00
. . . ( type === "google_calendar" && { installGoogleVideo : options?.installGoogleVideo } ) ,
2022-10-14 16:24:43 +00:00
} ;
const stateStr = encodeURIComponent ( JSON . stringify ( state ) ) ;
const searchParams = ` ?state= ${ stateStr } ` ;
const res = await fetch ( ` /api/integrations/ ${ type } /add ` + searchParams ) ;
if ( ! res . ok ) {
const errorBody = await res . json ( ) ;
throw new Error ( errorBody . message || "Something went wrong" ) ;
}
const json = await res . json ( ) ;
2022-11-17 21:38:34 +00:00
const externalUrl = /https?:\/\// . test ( json . url ) && ! json . url . startsWith ( window . location . origin ) ;
2022-10-18 11:18:39 +00:00
if ( ! isOmniInstall ) {
2022-11-13 15:51:31 +00:00
gotoUrl ( json . url , json . newTab ) ;
2022-12-05 13:13:01 +00:00
return ;
2022-10-18 11:18:39 +00:00
}
2022-10-25 08:18:19 +00:00
2022-10-14 16:24:43 +00:00
// Skip redirection only if it is an OmniInstall and redirect URL isn't of some other origin
// This allows installation of apps like Stripe to still redirect to their authentication pages.
2022-10-25 08:18:19 +00:00
// Check first that the URL is absolute, then check that it is of different origin from the current.
2022-11-17 21:38:34 +00:00
if ( externalUrl ) {
2022-10-25 08:18:19 +00:00
// TODO: For Omni installation to authenticate and come back to the page where installation was initiated, some changes need to be done in all apps' add callbacks
2022-11-13 15:51:31 +00:00
gotoUrl ( json . url , json . newTab ) ;
2022-12-05 13:13:01 +00:00
return ;
2022-10-14 16:24:43 +00:00
}
2022-11-17 21:38:34 +00:00
2022-11-22 20:44:08 +00:00
return { setupPending : externalUrl || json . url . endsWith ( "/setup" ) } ;
2022-10-14 16:24:43 +00:00
} , options ) ;
2022-03-23 22:00:30 +00:00
return mutation ;
}
export default useAddAppMutation ;