2022-11-17 18:56:47 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
2023-02-27 07:24:43 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
|
|
|
import { MembershipRole } from "@prisma/client";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
2023-04-19 11:40:18 +00:00
|
|
|
import { useState } from "react";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-11-17 18:56:47 +00:00
|
|
|
import { z } from "zod";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-11 20:38:21 +00:00
|
|
|
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
|
2023-03-10 22:10:56 +00:00
|
|
|
import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-03-03 23:20:13 +00:00
|
|
|
import { md } from "@calcom/lib/markdownIt";
|
2022-09-12 22:04:33 +00:00
|
|
|
import objectKeys from "@calcom/lib/objectKeys";
|
2023-01-27 23:14:58 +00:00
|
|
|
import turndown from "@calcom/lib/turndownService";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Avatar,
|
|
|
|
Button,
|
|
|
|
ConfirmationDialogContent,
|
|
|
|
Dialog,
|
|
|
|
DialogTrigger,
|
|
|
|
Form,
|
|
|
|
ImageUploader,
|
|
|
|
Label,
|
|
|
|
LinkIconButton,
|
|
|
|
Meta,
|
|
|
|
showToast,
|
|
|
|
TextField,
|
2023-01-25 01:08:10 +00:00
|
|
|
Editor,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { ExternalLink, Link as LinkIcon, Trash2, LogOut } from "@calcom/ui/components/icon";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-01-05 17:00:16 +00:00
|
|
|
import { getLayout } from "../../../settings/layouts/SettingsLayout";
|
|
|
|
|
2022-11-17 18:56:47 +00:00
|
|
|
const regex = new RegExp("^[a-zA-Z0-9-]*$");
|
|
|
|
|
|
|
|
const teamProfileFormSchema = z.object({
|
|
|
|
name: z.string(),
|
|
|
|
slug: z
|
|
|
|
.string()
|
|
|
|
.regex(regex, {
|
|
|
|
message: "Url can only have alphanumeric characters(a-z, 0-9) and hyphen(-) symbol.",
|
|
|
|
})
|
|
|
|
.min(1, { message: "Url cannot be left empty" }),
|
|
|
|
logo: z.string(),
|
|
|
|
bio: z.string(),
|
|
|
|
});
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
const ProfileView = () => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const session = useSession();
|
2023-04-19 11:40:18 +00:00
|
|
|
const [firstRender, setFirstRender] = useState(true);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const mutation = trpc.viewer.teams.update.useMutation({
|
2022-09-12 22:04:33 +00:00
|
|
|
onError: (err) => {
|
|
|
|
showToast(err.message, "error");
|
|
|
|
},
|
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.teams.get.invalidate();
|
2022-09-12 22:04:33 +00:00
|
|
|
showToast(t("your_team_updated_successfully"), "success");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-17 18:56:47 +00:00
|
|
|
const form = useForm({
|
|
|
|
resolver: zodResolver(teamProfileFormSchema),
|
|
|
|
});
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: team, isLoading } = trpc.viewer.teams.get.useQuery(
|
|
|
|
{ teamId: Number(router.query.id) },
|
|
|
|
{
|
|
|
|
onError: () => {
|
|
|
|
router.push("/settings");
|
|
|
|
},
|
|
|
|
onSuccess: (team) => {
|
|
|
|
if (team) {
|
|
|
|
form.setValue("name", team.name || "");
|
2022-11-17 18:56:47 +00:00
|
|
|
form.setValue("slug", team.slug || "");
|
2022-11-10 23:40:01 +00:00
|
|
|
form.setValue("logo", team.logo || "");
|
|
|
|
form.setValue("bio", team.bio || "");
|
2022-11-11 20:38:21 +00:00
|
|
|
if (team.slug === null && (team?.metadata as Prisma.JsonObject)?.requestedSlug) {
|
2022-11-17 18:56:47 +00:00
|
|
|
form.setValue("slug", ((team?.metadata as Prisma.JsonObject)?.requestedSlug as string) || "");
|
2022-11-11 20:38:21 +00:00
|
|
|
}
|
2022-11-10 23:40:01 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
const isAdmin =
|
|
|
|
team && (team.membership.role === MembershipRole.OWNER || team.membership.role === MembershipRole.ADMIN);
|
|
|
|
|
2023-02-23 22:03:33 +00:00
|
|
|
const permalink = `${WEBAPP_URL}/team/${team?.slug}`;
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-01-25 01:08:10 +00:00
|
|
|
const isBioEmpty = !team || !team.bio || !team.bio.replace("<p><br></p>", "").length;
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const deleteTeamMutation = trpc.viewer.teams.delete.useMutation({
|
2022-09-12 22:04:33 +00:00
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.teams.list.invalidate();
|
2022-11-10 20:23:56 +00:00
|
|
|
showToast(t("your_team_disbanded_successfully"), "success");
|
|
|
|
router.push(`${WEBAPP_URL}/teams`);
|
2022-09-12 22:04:33 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const removeMemberMutation = trpc.viewer.teams.removeMember.useMutation({
|
2022-09-12 22:04:33 +00:00
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.teams.get.invalidate();
|
|
|
|
await utils.viewer.teams.list.invalidate();
|
2023-04-13 02:10:23 +00:00
|
|
|
await utils.viewer.eventTypes.invalidate();
|
2022-09-12 22:04:33 +00:00
|
|
|
showToast(t("success"), "success");
|
|
|
|
},
|
|
|
|
async onError(err) {
|
|
|
|
showToast(err.message, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-11 20:38:21 +00:00
|
|
|
const publishMutation = trpc.viewer.teams.publish.useMutation({
|
|
|
|
async onSuccess(data: { url?: string }) {
|
|
|
|
if (data.url) {
|
|
|
|
router.push(data.url);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async onError(err) {
|
|
|
|
showToast(err.message, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-09-12 22:04:33 +00:00
|
|
|
function deleteTeam() {
|
|
|
|
if (team?.id) deleteTeamMutation.mutate({ teamId: team.id });
|
|
|
|
}
|
|
|
|
|
|
|
|
function leaveTeam() {
|
|
|
|
if (team?.id && session.data)
|
|
|
|
removeMemberMutation.mutate({
|
|
|
|
teamId: team.id,
|
|
|
|
memberId: session.data.user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-12-07 20:53:44 +00:00
|
|
|
<Meta title={t("profile")} description={t("profile_team_description")} />
|
2022-09-12 22:04:33 +00:00
|
|
|
{!isLoading && (
|
|
|
|
<>
|
|
|
|
{isAdmin ? (
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
if (team) {
|
|
|
|
const variables = {
|
|
|
|
logo: values.logo,
|
|
|
|
name: values.name,
|
2022-11-17 18:56:47 +00:00
|
|
|
slug: values.slug,
|
2022-09-12 22:04:33 +00:00
|
|
|
bio: values.bio,
|
|
|
|
};
|
|
|
|
objectKeys(variables).forEach((key) => {
|
|
|
|
if (variables[key as keyof typeof variables] === team?.[key]) delete variables[key];
|
|
|
|
});
|
|
|
|
mutation.mutate({ id: team.id, ...variables });
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
<div className="flex items-center">
|
|
|
|
<Controller
|
|
|
|
control={form.control}
|
|
|
|
name="logo"
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<>
|
|
|
|
<Avatar alt="" imageSrc={getPlaceholderAvatar(value, team?.name as string)} size="lg" />
|
2023-04-09 09:13:57 +00:00
|
|
|
<div className="ms-4">
|
2022-09-12 22:04:33 +00:00
|
|
|
<ImageUploader
|
|
|
|
target="avatar"
|
|
|
|
id="avatar-upload"
|
|
|
|
buttonMsg={t("update")}
|
|
|
|
handleAvatarChange={(newLogo) => {
|
|
|
|
form.setValue("logo", newLogo);
|
|
|
|
}}
|
|
|
|
imageSrc={value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2023-04-05 18:14:46 +00:00
|
|
|
<hr className="border-subtle my-8" />
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
<Controller
|
|
|
|
control={form.control}
|
|
|
|
name="name"
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<div className="mt-8">
|
|
|
|
<TextField
|
|
|
|
name="name"
|
|
|
|
label={t("team_name")}
|
|
|
|
value={value}
|
|
|
|
onChange={(e) => {
|
|
|
|
form.setValue("name", e?.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Controller
|
|
|
|
control={form.control}
|
2022-11-17 18:56:47 +00:00
|
|
|
name="slug"
|
2022-09-12 22:04:33 +00:00
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<div className="mt-8">
|
|
|
|
<TextField
|
2022-11-17 18:56:47 +00:00
|
|
|
name="slug"
|
2022-09-12 22:04:33 +00:00
|
|
|
label={t("team_url")}
|
|
|
|
value={value}
|
2022-10-17 06:10:06 +00:00
|
|
|
addOnLeading={`${WEBAPP_URL}/team/`}
|
2022-09-12 22:04:33 +00:00
|
|
|
onChange={(e) => {
|
2022-11-17 18:56:47 +00:00
|
|
|
form.clearErrors("slug");
|
|
|
|
form.setValue("slug", e?.target.value);
|
2022-09-12 22:04:33 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
2023-01-25 01:08:10 +00:00
|
|
|
<div className="mt-8">
|
|
|
|
<Label>{t("about")}</Label>
|
|
|
|
<Editor
|
|
|
|
getText={() => md.render(form.getValues("bio") || "")}
|
2023-01-27 23:14:58 +00:00
|
|
|
setText={(value: string) => form.setValue("bio", turndown(value))}
|
2023-01-25 01:08:10 +00:00
|
|
|
excludedToolbarItems={["blockType"]}
|
2023-03-03 23:20:13 +00:00
|
|
|
disableLists
|
2023-04-19 11:40:18 +00:00
|
|
|
firstRender={firstRender}
|
|
|
|
setFirstRender={setFirstRender}
|
2023-01-25 01:08:10 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-default mt-2 text-sm">{t("team_description")}</p>
|
2022-09-12 22:04:33 +00:00
|
|
|
<Button color="primary" className="mt-8" type="submit" loading={mutation.isLoading}>
|
|
|
|
{t("update")}
|
|
|
|
</Button>
|
2022-11-11 20:38:21 +00:00
|
|
|
{IS_TEAM_BILLING_ENABLED &&
|
|
|
|
team.slug === null &&
|
|
|
|
(team.metadata as Prisma.JsonObject)?.requestedSlug && (
|
|
|
|
<Button
|
|
|
|
color="secondary"
|
2023-01-25 01:08:10 +00:00
|
|
|
className="mt-8 ml-2"
|
2022-11-11 20:38:21 +00:00
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
publishMutation.mutate({ teamId: team.id });
|
|
|
|
}}>
|
|
|
|
Publish
|
|
|
|
</Button>
|
|
|
|
)}
|
2022-09-12 22:04:33 +00:00
|
|
|
</Form>
|
|
|
|
) : (
|
|
|
|
<div className="flex">
|
|
|
|
<div className="flex-grow">
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<Label className="text-emphasis">{t("team_name")}</Label>
|
|
|
|
<p className="text-default text-sm">{team?.name}</p>
|
2022-09-12 22:04:33 +00:00
|
|
|
</div>
|
2023-01-25 01:08:10 +00:00
|
|
|
{team && !isBioEmpty && (
|
2022-09-12 22:04:33 +00:00
|
|
|
<>
|
2023-04-05 18:14:46 +00:00
|
|
|
<Label className="text-emphasis mt-5">{t("about")}</Label>
|
2023-01-25 01:08:10 +00:00
|
|
|
<div
|
2023-04-05 18:14:46 +00:00
|
|
|
className=" text-subtle text-sm [&_a]:text-blue-500 [&_a]:underline [&_a]:hover:text-blue-600"
|
|
|
|
dangerouslySetInnerHTML={{ __html: md.render(team.bio || "") }}
|
2023-01-25 01:08:10 +00:00
|
|
|
/>
|
2022-09-12 22:04:33 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="">
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link href={permalink} passHref={true} target="_blank">
|
2023-04-12 15:26:31 +00:00
|
|
|
<LinkIconButton Icon={ExternalLink}>{t("preview")}</LinkIconButton>
|
2022-09-12 22:04:33 +00:00
|
|
|
</Link>
|
|
|
|
<LinkIconButton
|
2023-04-12 15:26:31 +00:00
|
|
|
Icon={LinkIcon}
|
2022-09-12 22:04:33 +00:00
|
|
|
onClick={() => {
|
|
|
|
navigator.clipboard.writeText(permalink);
|
|
|
|
showToast("Copied to clipboard", "success");
|
|
|
|
}}>
|
|
|
|
{t("copy_link_team")}
|
|
|
|
</LinkIconButton>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-04-05 18:14:46 +00:00
|
|
|
<hr className="border-subtle my-8 border" />
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-default mb-3 text-base font-semibold">{t("danger_zone")}</div>
|
2022-09-12 22:04:33 +00:00
|
|
|
{team?.membership.role === "OWNER" ? (
|
|
|
|
<Dialog>
|
|
|
|
<DialogTrigger asChild>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Button color="destructive" className="border" StartIcon={Trash2}>
|
2022-11-22 19:18:47 +00:00
|
|
|
{t("disband_team")}
|
2022-09-12 22:04:33 +00:00
|
|
|
</Button>
|
|
|
|
</DialogTrigger>
|
|
|
|
<ConfirmationDialogContent
|
|
|
|
variety="danger"
|
|
|
|
title={t("disband_team")}
|
|
|
|
confirmBtnText={t("confirm_disband_team")}
|
|
|
|
onConfirm={deleteTeam}>
|
|
|
|
{t("disband_team_confirmation_message")}
|
|
|
|
</ConfirmationDialogContent>
|
|
|
|
</Dialog>
|
|
|
|
) : (
|
|
|
|
<Dialog>
|
|
|
|
<DialogTrigger asChild>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Button color="destructive" className="border" StartIcon={LogOut}>
|
2022-09-12 22:04:33 +00:00
|
|
|
{t("leave_team")}
|
|
|
|
</Button>
|
|
|
|
</DialogTrigger>
|
|
|
|
<ConfirmationDialogContent
|
|
|
|
variety="danger"
|
|
|
|
title={t("leave_team")}
|
|
|
|
confirmBtnText={t("confirm_leave_team")}
|
|
|
|
onConfirm={leaveTeam}>
|
|
|
|
{t("leave_team_confirmation_message")}
|
|
|
|
</ConfirmationDialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ProfileView.getLayout = getLayout;
|
|
|
|
|
|
|
|
export default ProfileView;
|