feat: create trpc route to fetch download link (#7495)
* feat: create trpc route to fetch download link Signed-off-by: Udit Takkar <[email protected]> * fix: removing file, adding isdownloadable logix Signed-off-by: Udit Takkar <[email protected]> * chore Signed-off-by: Udit Takkar <[email protected]> * fix: type error Signed-off-by: Udit Takkar <[email protected]> * Some performance enhancements and better error logging * Add error message here too --------- Signed-off-by: Udit Takkar <[email protected]> Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
1 parent
cc1d606ba8
commit
2dddd4ce77
8 files changed
+204
-122
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import DOMPurify from "dompurify";
|
||||
import { useSession } from "next-auth/react";
|
||||
import React, { AriaRole, ComponentType, Fragment } from "react";
|
||||
import type { AriaRole, ComponentType } from "react";
|
||||
import React, { Fragment } from "react";
|
||||
|
||||
import { APP_NAME, CONSOLE_URL, SUPPORT_MAIL_ADDRESS, WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { useState } from "react";
|
||||
import { useState, Suspense } from "react";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import LicenseRequired from "@calcom/features/ee/common/components/v2/LicenseRequired";
|
||||
import useHasPaidPlan from "@calcom/lib/hooks/useHasPaidPlan";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { RecordingItemSchema } from "@calcom/prisma/zod-utils";
|
||||
import { RouterOutputs, trpc } from "@calcom/trpc/react";
|
||||
import type { RecordingItemSchema } from "@calcom/prisma/zod-utils";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { PartialReference } from "@calcom/types/EventManager";
|
||||
import {
|
||||
Dialog,
|
||||
@@ -15,7 +16,7 @@ import {
|
||||
DialogHeader,
|
||||
UpgradeTeamsBadge,
|
||||
} from "@calcom/ui";
|
||||
import { Button, showToast } from "@calcom/ui";
|
||||
import { Button } from "@calcom/ui";
|
||||
import { FiDownload } from "@calcom/ui/components/icon";
|
||||
|
||||
import RecordingListSkeleton from "./components/RecordingListSkeleton";
|
||||
@@ -40,31 +41,122 @@ interface GetTimeSpanProps {
|
||||
startTime: string | undefined;
|
||||
endTime: string | undefined;
|
||||
locale: string;
|
||||
isTimeFormatAMPM: boolean;
|
||||
hour12: boolean;
|
||||
}
|
||||
|
||||
const getTimeSpan = ({ startTime, endTime, locale, isTimeFormatAMPM }: GetTimeSpanProps) => {
|
||||
const getTimeSpan = ({ startTime, endTime, locale, hour12 }: GetTimeSpanProps) => {
|
||||
if (!startTime || !endTime) return "";
|
||||
|
||||
const formattedStartTime = new Intl.DateTimeFormat(locale, {
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
hour12: isTimeFormatAMPM,
|
||||
hour12,
|
||||
}).format(new Date(startTime));
|
||||
|
||||
const formattedEndTime = new Intl.DateTimeFormat(locale, {
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
hour12: isTimeFormatAMPM,
|
||||
hour12,
|
||||
}).format(new Date(endTime));
|
||||
|
||||
return `${formattedStartTime} - ${formattedEndTime}`;
|
||||
};
|
||||
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
||||
const ViewRecordingsList = ({ roomName, hasPaidPlan }: { roomName: string; hasPaidPlan: boolean }) => {
|
||||
const { t } = useLocale();
|
||||
const { setRecordingId, isFetching } = useRecordingDownload();
|
||||
|
||||
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"
|
||||
loading={isFetching}
|
||||
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>
|
||||
)
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export const ViewRecordingsDialog = (props: IViewRecordingsDialog) => {
|
||||
const { t, i18n } = useLocale();
|
||||
const { isOpenDialog, setIsOpenDialog, booking, timeFormat } = props;
|
||||
const [downloadingRecordingId, setRecordingId] = useState<string | null>(null);
|
||||
|
||||
const { hasPaidPlan, isLoading: isTeamPlanStatusLoading } = useHasPaidPlan();
|
||||
|
||||
@@ -72,84 +164,32 @@ export const ViewRecordingsDialog = (props: IViewRecordingsDialog) => {
|
||||
booking?.references?.find((reference: PartialReference) => reference.type === "daily_video")?.meetingId ??
|
||||
undefined;
|
||||
|
||||
const { data: recordings, isLoading } = trpc.viewer.getCalVideoRecordings.useQuery(
|
||||
{ roomName: roomName ?? "" },
|
||||
{ enabled: !!roomName && isOpenDialog }
|
||||
);
|
||||
const handleDownloadClick = async (recordingId: string) => {
|
||||
try {
|
||||
setRecordingId(recordingId);
|
||||
const res = await fetch(`/api/download-cal-video-recording?recordingId=${recordingId}`, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
const respBody = await res.json();
|
||||
|
||||
if (respBody?.download_link) {
|
||||
window.location.href = respBody.download_link;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
showToast(t("something_went_wrong"), "error");
|
||||
}
|
||||
setRecordingId(null);
|
||||
};
|
||||
|
||||
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,
|
||||
isTimeFormatAMPM: timeFormat === 12,
|
||||
hour12: timeFormat === 12,
|
||||
})} `;
|
||||
|
||||
return (
|
||||
<Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}>
|
||||
<DialogContent>
|
||||
<DialogHeader title={t("recordings_title")} subtitle={subtitle} />
|
||||
<LicenseRequired>
|
||||
<>
|
||||
{(isLoading || isTeamPlanStatusLoading) && <RecordingListSkeleton />}
|
||||
{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"
|
||||
loading={downloadingRecordingId === recording.id}
|
||||
onClick={() => handleDownloadClick(recording.id)}>
|
||||
{t("download")}
|
||||
</Button>
|
||||
) : (
|
||||
<UpgradeTeamsBadge />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
{roomName ? (
|
||||
<LicenseRequired>
|
||||
{isTeamPlanStatusLoading ? (
|
||||
<RecordingListSkeleton />
|
||||
) : (
|
||||
<Suspense fallback={<RecordingListSkeleton />}>
|
||||
<ViewRecordingsList hasPaidPlan={!!hasPaidPlan} roomName={roomName} />
|
||||
</Suspense>
|
||||
)}
|
||||
{!isLoading &&
|
||||
(!recordings ||
|
||||
(recordings && "total_count" in recordings && recordings?.total_count === 0)) && (
|
||||
<h1 className="font-semibold">{t("no_recordings_found")}</h1>
|
||||
)}
|
||||
</>
|
||||
</LicenseRequired>
|
||||
</LicenseRequired>
|
||||
) : (
|
||||
<p className="font-semibold">{t("no_recordings_found")}</p>
|
||||
)}
|
||||
<DialogFooter>
|
||||
<DialogClose className="border" />
|
||||
</DialogFooter>
|
||||
|
||||
Reference in new issue
Block a user