2023-09-28 18:04:51 +00:00
|
|
|
import { useState } from "react";
|
2022-10-19 09:33:40 +00:00
|
|
|
|
2023-01-05 17:00:16 +00:00
|
|
|
import { getLayout } from "@calcom/features/settings/layouts/SettingsLayout";
|
2022-10-19 09:33:40 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-09-28 18:04:51 +00:00
|
|
|
import type { RouterOutputs } from "@calcom/trpc/react";
|
|
|
|
import { Meta, showToast, SettingsToggle, SkeletonContainer, SkeletonText } from "@calcom/ui";
|
2022-10-19 09:33:40 +00:00
|
|
|
|
2023-04-18 18:45:32 +00:00
|
|
|
import PageWrapper from "@components/PageWrapper";
|
|
|
|
|
2023-09-28 18:04:51 +00:00
|
|
|
const SkeletonLoader = ({ title, description }: { title: string; description: string }) => {
|
|
|
|
return (
|
|
|
|
<SkeletonContainer>
|
|
|
|
<Meta title={title} description={description} borderInShellHeader={true} />
|
|
|
|
<div className="border-subtle space-y-6 border border-t-0 px-4 py-8 sm:px-6">
|
|
|
|
<SkeletonText className="h-8 w-full" />
|
|
|
|
</div>
|
|
|
|
</SkeletonContainer>
|
|
|
|
);
|
|
|
|
};
|
2022-12-07 20:53:44 +00:00
|
|
|
|
2023-09-28 18:04:51 +00:00
|
|
|
const ProfileImpersonationView = ({ user }: { user: RouterOutputs["viewer"]["me"] }) => {
|
2022-10-19 09:33:40 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
2023-09-28 18:04:51 +00:00
|
|
|
const [disableImpersonation, setDisableImpersonation] = useState<boolean | undefined>(
|
|
|
|
user?.disableImpersonation
|
|
|
|
);
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const mutation = trpc.viewer.updateProfile.useMutation({
|
2022-10-19 09:33:40 +00:00
|
|
|
onSuccess: () => {
|
|
|
|
showToast(t("profile_updated_successfully"), "success");
|
|
|
|
},
|
2022-12-20 23:13:31 +00:00
|
|
|
onSettled: () => {
|
|
|
|
utils.viewer.me.invalidate();
|
|
|
|
},
|
|
|
|
onMutate: async ({ disableImpersonation }) => {
|
|
|
|
await utils.viewer.me.cancel();
|
|
|
|
const previousValue = utils.viewer.me.getData();
|
|
|
|
|
2023-09-28 18:04:51 +00:00
|
|
|
setDisableImpersonation(disableImpersonation);
|
|
|
|
|
2022-12-20 23:13:31 +00:00
|
|
|
return { previousValue };
|
|
|
|
},
|
|
|
|
onError: (error, variables, context) => {
|
|
|
|
if (context?.previousValue) {
|
|
|
|
utils.viewer.me.setData(undefined, context.previousValue);
|
2023-09-28 18:04:51 +00:00
|
|
|
setDisableImpersonation(context.previousValue?.disableImpersonation);
|
2022-12-20 23:13:31 +00:00
|
|
|
}
|
2022-10-19 09:33:40 +00:00
|
|
|
showToast(`${t("error")}, ${error.message}`, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-09-28 18:04:51 +00:00
|
|
|
<Meta
|
|
|
|
title={t("impersonation")}
|
|
|
|
description={t("impersonation_description")}
|
|
|
|
borderInShellHeader={true}
|
|
|
|
/>
|
|
|
|
<div>
|
|
|
|
<SettingsToggle
|
|
|
|
toggleSwitchAtTheEnd={true}
|
|
|
|
title={t("user_impersonation_heading")}
|
|
|
|
description={t("user_impersonation_description")}
|
|
|
|
checked={!disableImpersonation}
|
|
|
|
onCheckedChange={(checked) => {
|
|
|
|
mutation.mutate({ disableImpersonation: !checked });
|
|
|
|
}}
|
|
|
|
disabled={mutation.isLoading}
|
|
|
|
switchContainerClassName="py-6 px-4 sm:px-6 border-subtle rounded-b-xl border border-t-0"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-10-19 09:33:40 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-09-28 18:04:51 +00:00
|
|
|
const ProfileImpersonationViewWrapper = () => {
|
|
|
|
const { data: user, isLoading } = trpc.viewer.me.useQuery();
|
|
|
|
const { t } = useLocale();
|
2022-10-19 09:33:40 +00:00
|
|
|
|
2023-09-28 18:04:51 +00:00
|
|
|
if (isLoading || !user)
|
|
|
|
return <SkeletonLoader title={t("impersonation")} description={t("impersonation_description")} />;
|
|
|
|
|
|
|
|
return <ProfileImpersonationView user={user} />;
|
2022-12-07 20:53:44 +00:00
|
|
|
};
|
|
|
|
|
2023-09-28 18:04:51 +00:00
|
|
|
ProfileImpersonationViewWrapper.getLayout = getLayout;
|
|
|
|
ProfileImpersonationViewWrapper.PageWrapper = PageWrapper;
|
|
|
|
|
|
|
|
export default ProfileImpersonationViewWrapper;
|