2022-11-17 18:56:47 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
2022-11-11 20:38:21 +00:00
|
|
|
import { MembershipRole, Prisma } from "@prisma/client";
|
2023-01-25 01:08:10 +00:00
|
|
|
import MarkdownIt from "markdown-it";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
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-12-14 17:43:00 +00:00
|
|
|
import { CAL_URL } from "@calcom/lib/constants";
|
2022-11-11 20:38:21 +00:00
|
|
|
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { getPlaceholderAvatar } from "@calcom/lib/getPlaceholderAvatar";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
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-01-23 23:08:01 +00:00
|
|
|
import { FiExternalLink, FiLink, FiTrash2, FiLogOut } 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";
|
|
|
|
|
2023-01-25 01:08:10 +00:00
|
|
|
const md = new MarkdownIt("default", { html: true, breaks: true });
|
|
|
|
|
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();
|
|
|
|
|
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();
|
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-01-04 07:38:45 +00:00
|
|
|
<div className="ltr:ml-4 rtl:mr-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>
|
|
|
|
|
|
|
|
<hr className="my-8 border-gray-200" />
|
|
|
|
|
|
|
|
<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"]}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-09-12 22:04:33 +00:00
|
|
|
<p className="mt-2 text-sm text-gray-600">{t("team_description")}</p>
|
|
|
|
<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>
|
|
|
|
<Label className="text-black">{t("team_name")}</Label>
|
|
|
|
<p className="text-sm text-gray-800">{team?.name}</p>
|
|
|
|
</div>
|
2023-01-25 01:08:10 +00:00
|
|
|
{team && !isBioEmpty && (
|
2022-09-12 22:04:33 +00:00
|
|
|
<>
|
|
|
|
<Label className="mt-5 text-black">{t("about")}</Label>
|
2023-01-25 01:08:10 +00:00
|
|
|
<div
|
2023-01-27 16:03:04 +00:00
|
|
|
className="dark:text-darkgray-600 text-sm text-gray-500 [&_a]:text-blue-500 [&_a]:underline [&_a]:hover:text-blue-600"
|
2023-01-25 01:08:10 +00:00
|
|
|
dangerouslySetInnerHTML={{ __html: md.render(team.bio || "") }}
|
|
|
|
/>
|
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-01-23 23:08:01 +00:00
|
|
|
<LinkIconButton Icon={FiExternalLink}>{t("preview")}</LinkIconButton>
|
2022-09-12 22:04:33 +00:00
|
|
|
</Link>
|
|
|
|
<LinkIconButton
|
2023-01-23 23:08:01 +00:00
|
|
|
Icon={FiLink}
|
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-01-20 22:04:58 +00:00
|
|
|
<hr className="my-8 border border-gray-200" />
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
<div className="mb-3 text-base font-semibold">{t("danger_zone")}</div>
|
|
|
|
{team?.membership.role === "OWNER" ? (
|
|
|
|
<Dialog>
|
|
|
|
<DialogTrigger asChild>
|
2023-01-23 23:08:01 +00:00
|
|
|
<Button color="destructive" className="border" StartIcon={FiTrash2}>
|
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-01-23 23:08:01 +00:00
|
|
|
<Button color="destructive" className="border" StartIcon={FiLogOut}>
|
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;
|