2022-10-14 16:24:43 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
|
|
|
import useApp from "@calcom/lib/hooks/useApp";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, showToast } from "@calcom/ui";
|
|
|
|
import { FiPlus } from "@calcom/ui/components/icon";
|
2022-10-14 16:24:43 +00:00
|
|
|
|
2022-11-26 13:54:42 +00:00
|
|
|
import useAddAppMutation from "../_utils/useAddAppMutation";
|
|
|
|
import { InstallAppButton } from "../components";
|
|
|
|
|
2022-10-14 16:24:43 +00:00
|
|
|
/**
|
|
|
|
* Use this component to allow installing an app from anywhere on the app.
|
|
|
|
* Use of this component requires you to remove custom InstallAppButtonComponent so that it can manage the redirection itself
|
|
|
|
*/
|
2022-12-21 20:31:09 +00:00
|
|
|
export default function OmniInstallAppButton({
|
|
|
|
appId,
|
|
|
|
className,
|
|
|
|
returnTo,
|
|
|
|
}: {
|
|
|
|
appId: string;
|
|
|
|
className: string;
|
|
|
|
returnTo?: string;
|
|
|
|
}) {
|
2022-10-14 16:24:43 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const { data: app } = useApp(appId);
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
|
|
|
const mutation = useAddAppMutation(null, {
|
2022-12-21 20:31:09 +00:00
|
|
|
returnTo,
|
2022-11-17 21:38:34 +00:00
|
|
|
onSuccess: (data) => {
|
2022-10-14 16:24:43 +00:00
|
|
|
//TODO: viewer.appById might be replaced with viewer.apps so that a single query needs to be invalidated.
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.appById.invalidate({ appId });
|
|
|
|
utils.viewer.apps.invalidate({ extendsFeature: "EventType" });
|
2022-12-05 13:13:01 +00:00
|
|
|
if (data?.setupPending) return;
|
2022-10-14 16:24:43 +00:00
|
|
|
showToast(t("app_successfully_installed"), "success");
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
if (error instanceof Error) showToast(error.message || t("app_could_not_be_installed"), "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<InstallAppButton
|
|
|
|
type={app.type}
|
|
|
|
isProOnly={app.isProOnly}
|
|
|
|
wrapperClassName={classNames("[@media(max-width:260px)]:w-full", className)}
|
|
|
|
render={({ useDefaultComponent, ...props }) => {
|
|
|
|
if (useDefaultComponent) {
|
|
|
|
props = {
|
2022-11-23 02:55:25 +00:00
|
|
|
...props,
|
2022-10-14 16:24:43 +00:00
|
|
|
onClick: () => {
|
|
|
|
mutation.mutate({ type: app.type, variant: app.variant, slug: app.slug, isOmniInstall: true });
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
loading={mutation.isLoading}
|
|
|
|
color="secondary"
|
|
|
|
className="[@media(max-width:260px)]:w-full [@media(max-width:260px)]:justify-center"
|
2023-01-23 23:08:01 +00:00
|
|
|
StartIcon={FiPlus}
|
2022-10-14 16:24:43 +00:00
|
|
|
{...props}>
|
|
|
|
{t("install")}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|