2023-03-05 12:59:07 +00:00
|
|
|
import { useState, Suspense } from "react";
|
2022-12-27 21:03:39 +00:00
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import LicenseRequired from "@calcom/features/ee/common/components/v2/LicenseRequired";
|
2023-01-31 19:10:21 +00:00
|
|
|
import useHasPaidPlan from "@calcom/lib/hooks/useHasPaidPlan";
|
2022-12-27 21:03:39 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-03-05 12:59:07 +00:00
|
|
|
import type { RecordingItemSchema } from "@calcom/prisma/zod-utils";
|
|
|
|
import type { RouterOutputs } from "@calcom/trpc/react";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-12-27 21:03:39 +00:00
|
|
|
import type { PartialReference } from "@calcom/types/EventManager";
|
2023-01-14 00:48:19 +00:00
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogClose,
|
|
|
|
DialogContent,
|
|
|
|
DialogFooter,
|
|
|
|
DialogHeader,
|
|
|
|
UpgradeTeamsBadge,
|
|
|
|
} from "@calcom/ui";
|
2023-03-05 12:59:07 +00:00
|
|
|
import { Button } from "@calcom/ui";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { FiDownload } from "@calcom/ui/components/icon";
|
2022-12-27 21:03:39 +00:00
|
|
|
|
|
|
|
import RecordingListSkeleton from "./components/RecordingListSkeleton";
|
|
|
|
|
|
|
|
type BookingItem = RouterOutputs["viewer"]["bookings"]["get"]["bookings"][number];
|
|
|
|
|
|
|
|
interface IViewRecordingsDialog {
|
|
|
|
booking?: BookingItem;
|
|
|
|
isOpenDialog: boolean;
|
|
|
|
setIsOpenDialog: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
timeFormat: number | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertSecondsToMs(seconds: number) {
|
|
|
|
// Bitwise Double Not is faster than Math.floor
|
|
|
|
const minutes = ~~(seconds / 60);
|
|
|
|
const extraSeconds = seconds % 60;
|
|
|
|
return `${minutes}min ${extraSeconds}sec`;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface GetTimeSpanProps {
|
|
|
|
startTime: string | undefined;
|
|
|
|
endTime: string | undefined;
|
|
|
|
locale: string;
|
2023-03-05 12:59:07 +00:00
|
|
|
hour12: boolean;
|
2022-12-27 21:03:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 12:59:07 +00:00
|
|
|
const getTimeSpan = ({ startTime, endTime, locale, hour12 }: GetTimeSpanProps) => {
|
2022-12-27 21:03:39 +00:00
|
|
|
if (!startTime || !endTime) return "";
|
|
|
|
|
|
|
|
const formattedStartTime = new Intl.DateTimeFormat(locale, {
|
|
|
|
hour: "numeric",
|
|
|
|
minute: "numeric",
|
2023-03-05 12:59:07 +00:00
|
|
|
hour12,
|
2022-12-27 21:03:39 +00:00
|
|
|
}).format(new Date(startTime));
|
|
|
|
|
|
|
|
const formattedEndTime = new Intl.DateTimeFormat(locale, {
|
|
|
|
hour: "numeric",
|
|
|
|
minute: "numeric",
|
2023-03-05 12:59:07 +00:00
|
|
|
hour12,
|
2022-12-27 21:03:39 +00:00
|
|
|
}).format(new Date(endTime));
|
|
|
|
|
|
|
|
return `${formattedStartTime} - ${formattedEndTime}`;
|
|
|
|
};
|
|
|
|
|
2023-03-05 12:59:07 +00:00
|
|
|
const useRecordingDownload = () => {
|
|
|
|
const [recordingId, setRecordingId] = useState("");
|
|
|
|
const { isFetching, data } = trpc.viewer.getDownloadLinkOfCalVideoRecordings.useQuery(
|
|
|
|
{
|
|
|
|
recordingId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enabled: !!recordingId,
|
|
|
|
cacheTime: 0,
|
|
|
|
refetchOnWindowFocus: false,
|
|
|
|
refetchOnReconnect: false,
|
|
|
|
retry: false,
|
|
|
|
onSuccess: (data) => {
|
|
|
|
if (data && data.download_link) {
|
|
|
|
window.location.href = data.download_link;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
setRecordingId: (newRecordingId: string) => {
|
|
|
|
// may be a way to do this by default, but this is easy enough.
|
|
|
|
if (recordingId === newRecordingId && data) {
|
|
|
|
window.location.href = data.download_link;
|
|
|
|
}
|
|
|
|
if (!isFetching) {
|
|
|
|
setRecordingId(newRecordingId);
|
|
|
|
}
|
|
|
|
// assume it is still fetching, do nothing.
|
|
|
|
},
|
|
|
|
isFetching,
|
2023-03-06 10:39:54 +00:00
|
|
|
recordingId,
|
2023-03-05 12:59:07 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const ViewRecordingsList = ({ roomName, hasPaidPlan }: { roomName: string; hasPaidPlan: boolean }) => {
|
|
|
|
const { t } = useLocale();
|
2023-03-06 10:39:54 +00:00
|
|
|
const { setRecordingId, isFetching, recordingId } = useRecordingDownload();
|
2023-03-05 12:59:07 +00:00
|
|
|
|
|
|
|
const { data: recordings } = trpc.viewer.getCalVideoRecordings.useQuery(
|
|
|
|
{ roomName },
|
|
|
|
{
|
|
|
|
suspense: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleDownloadClick = async (recordingId: string) => {
|
|
|
|
// this would enable the getDownloadLinkOfCalVideoRecordings
|
|
|
|
setRecordingId(recordingId);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{recordings && "data" in recordings && recordings?.data?.length > 0 ? (
|
|
|
|
<div className="flex flex-col gap-3">
|
|
|
|
{recordings.data.map((recording: RecordingItemSchema, index: number) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="flex w-full items-center justify-between rounded-md border px-4 py-2"
|
|
|
|
key={recording.id}>
|
|
|
|
<div className="flex flex-col">
|
|
|
|
<h1 className="text-sm font-semibold">
|
|
|
|
{t("recording")} {index + 1}
|
|
|
|
</h1>
|
|
|
|
<p className="text-sm font-normal text-gray-500">
|
|
|
|
{convertSecondsToMs(recording.duration)}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
{hasPaidPlan ? (
|
|
|
|
<Button
|
|
|
|
StartIcon={FiDownload}
|
|
|
|
className="ml-4 lg:ml-0"
|
2023-03-06 10:39:54 +00:00
|
|
|
loading={isFetching && recordingId === recording.id}
|
2023-03-05 12:59:07 +00:00
|
|
|
onClick={() => handleDownloadClick(recording.id)}>
|
|
|
|
{t("download")}
|
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<UpgradeTeamsBadge />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
(!recordings || (recordings && "total_count" in recordings && recordings?.total_count === 0)) && (
|
|
|
|
<p className="font-semibold">{t("no_recordings_found")}</p>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-27 21:03:39 +00:00
|
|
|
export const ViewRecordingsDialog = (props: IViewRecordingsDialog) => {
|
|
|
|
const { t, i18n } = useLocale();
|
|
|
|
const { isOpenDialog, setIsOpenDialog, booking, timeFormat } = props;
|
2023-01-23 09:58:41 +00:00
|
|
|
|
2023-01-31 19:10:21 +00:00
|
|
|
const { hasPaidPlan, isLoading: isTeamPlanStatusLoading } = useHasPaidPlan();
|
2023-01-23 09:58:41 +00:00
|
|
|
|
2022-12-27 21:03:39 +00:00
|
|
|
const roomName =
|
|
|
|
booking?.references?.find((reference: PartialReference) => reference.type === "daily_video")?.meetingId ??
|
|
|
|
undefined;
|
|
|
|
|
|
|
|
const subtitle = `${booking?.title} - ${dayjs(booking?.startTime).format("ddd")} ${dayjs(
|
|
|
|
booking?.startTime
|
|
|
|
).format("D")}, ${dayjs(booking?.startTime).format("MMM")} ${getTimeSpan({
|
|
|
|
startTime: booking?.startTime,
|
|
|
|
endTime: booking?.endTime,
|
|
|
|
locale: i18n.language,
|
2023-03-05 12:59:07 +00:00
|
|
|
hour12: timeFormat === 12,
|
2022-12-27 21:03:39 +00:00
|
|
|
})} `;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogHeader title={t("recordings_title")} subtitle={subtitle} />
|
2023-03-05 12:59:07 +00:00
|
|
|
{roomName ? (
|
|
|
|
<LicenseRequired>
|
|
|
|
{isTeamPlanStatusLoading ? (
|
|
|
|
<RecordingListSkeleton />
|
|
|
|
) : (
|
|
|
|
<Suspense fallback={<RecordingListSkeleton />}>
|
|
|
|
<ViewRecordingsList hasPaidPlan={!!hasPaidPlan} roomName={roomName} />
|
|
|
|
</Suspense>
|
2023-01-14 00:48:19 +00:00
|
|
|
)}
|
2023-03-05 12:59:07 +00:00
|
|
|
</LicenseRequired>
|
|
|
|
) : (
|
|
|
|
<p className="font-semibold">{t("no_recordings_found")}</p>
|
|
|
|
)}
|
2022-12-27 21:03:39 +00:00
|
|
|
<DialogFooter>
|
2023-01-14 00:48:19 +00:00
|
|
|
<DialogClose className="border" />
|
2022-12-27 21:03:39 +00:00
|
|
|
</DialogFooter>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ViewRecordingsDialog;
|