2022-11-17 21:38:34 +00:00
import { useMutation , UseMutationOptions } 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" ;
2022-03-23 22:00:30 +00:00
import { App } from "@calcom/types/App" ;
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 ;
type UseAddAppMutationOptions = CustomUseMutationOptions & {
onSuccess : ( data : { setupPending : boolean } ) = > void ;
} ;
function useAddAppMutation ( _type : App [ "type" ] | null , options? : UseAddAppMutationOptions ) {
2022-10-14 16:24:43 +00:00
const mutation = useMutation <
2022-11-17 21:38:34 +00:00
{ setupPending : boolean } ,
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-17 21:38:34 +00:00
if ( type === "sendgrid_other_calendar" ) {
type = "sendgrid" ;
}
2022-10-14 16:24:43 +00:00
const state : IntegrationOAuthCallbackState = {
returnTo :
WEBAPP_URL +
getInstalledAppPath (
{ variant : variables && variables . variant , slug : variables && variables . slug } ,
location . search
) ,
} ;
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-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-10-14 16:24:43 +00:00
}
2022-11-17 21:38:34 +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 ;