* Sendgrid app and code simplification * Applying app-store-cli + impl * Fixing types * Adding features to readme * Fixing unit tests * A few last tweaks regarding UX and env vars * Applying feedback * Using calcom icons * Renaming and applying feedback * Testing user/type page fix * Standarizing Sendgrid client usage * Removing types * Reverting CloseCom changes * Stop relying on sendgrid client pkg * Fixing button and more reverting closecom changes * Revert "Stop relying on sendgrid client pkg" This reverts commit dd61851572a17a1e4051b133683af85c934bc2d0. * Revert "Removing types" This reverts commit 1ec5ed8de2f3139bbe84f867f229bc5759256806. * Is this it? * Standardizing apis * Fixing path * Fixing throwing errors the standard way * Stop relying on getInstalledAppPath * Removing seemingly troubling code * Returning error and avoiding any outer reference * Revert "Returning error and avoiding any outer reference" This reverts commit 7d32e30154423c95f54ebae81a76ab16a1c7bc94. * Revert "Removing seemingly troubling code" This reverts commit eaae772abcd04c8f046e4960116f42c5aaf87adf. * Revert "Stop relying on getInstalledAppPath" This reverts commit bcc70fc337bbe7fb5e74609abaeee7cd3ede90a3. * Revert "Fixing throwing errors the standard way" This reverts commit bb1bb410fac6f8c6ad14c3163a8433d125f7a885. * Revert "Fixing path" This reverts commit a7bd83c4fb7597594d0470cb530378c826b45481. * Revert "Standardizing apis" This reverts commit 0258a182298af3ebad321854ef4f34a65f4c700a. * Revert "Is this it?" This reverts commit 70b3f7b98e3003dfa225dc539e02a1e17abdd840. * Converting APIs to legacy style * Missing reverted CloseCom test mock * Needed for the renaming * Reverting Closecom and yarn unneeded changes * Ununsed type * Testing rearranging exports * Update apps/web/components/apps/OmniInstallAppButton.tsx Co-authored-by: Omar López <zomars@me.com> * Standardizing APIs * Fixing wrong toast message on app page Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com>
83 lines
2.7 KiB
TypeScript
83 lines
2.7 KiB
TypeScript
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
|
|
|
import type { IntegrationOAuthCallbackState } from "@calcom/app-store/types";
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
import { App } from "@calcom/types/App";
|
|
|
|
import getInstalledAppPath from "./getInstalledAppPath";
|
|
|
|
function gotoUrl(url: string, newTab?: boolean) {
|
|
if (newTab) {
|
|
window.open(url, "_blank");
|
|
return;
|
|
}
|
|
window.location.href = url;
|
|
}
|
|
|
|
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) {
|
|
const mutation = useMutation<
|
|
{ setupPending: boolean },
|
|
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;
|
|
}
|
|
if (type === "sendgrid_other_calendar") {
|
|
type = "sendgrid";
|
|
}
|
|
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();
|
|
const externalUrl = /https?:\/\//.test(json.url) && !json.url.startsWith(window.location.origin);
|
|
|
|
if (!isOmniInstall) {
|
|
gotoUrl(json.url, json.newTab);
|
|
}
|
|
|
|
// 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.
|
|
|
|
// Check first that the URL is absolute, then check that it is of different origin from the current.
|
|
if (externalUrl) {
|
|
// 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
|
|
gotoUrl(json.url, json.newTab);
|
|
}
|
|
|
|
return { setupPending: !externalUrl && json.url.endsWith("/setup") };
|
|
}, options);
|
|
|
|
return mutation;
|
|
}
|
|
|
|
export default useAddAppMutation;
|