import { Dialog, Transition } from "@headlessui/react"; import { Fragment, useState } from "react"; import { ModernSwitch } from "~/components/ModernSwitch"; import { api, type RouterOutputs } from "~/utils/api"; import ExpireDateSelectMenu from "~/components/ExpireDateSelectMenu"; import { usePostHog } from "posthog-js/react"; interface Props { video: RouterOutputs["video"]["get"]; } export function ShareModal({ video }: Props) { const utils = api.useContext(); const [open, setOpen] = useState(false); const posthog = usePostHog(); const openModal = () => { setOpen(true); posthog?.capture("open video share modal", { videoSharing: video.sharing, videoId: video.id, }); }; const closeModal = () => { setOpen(false); posthog?.capture("close video share modal", { videoSharing: video.sharing, videoId: video.id, }); }; const setSharingMutation = api.video.setSharing.useMutation({ onMutate: async ({ videoId, sharing }) => { await utils.video.get.cancel(); const previousValue = utils.video.get.getData({ videoId }); if (previousValue) { utils.video.get.setData({ videoId }, { ...previousValue, sharing }); } return { previousValue }; }, onError: (err, { videoId }, context) => { if (context?.previousValue) { utils.video.get.setData({ videoId }, context.previousValue); } console.error(err.message); }, }); const setDeleteAfterLinkExpiresMutation = api.video.setDeleteAfterLinkExpires.useMutation({ onMutate: async ({ videoId, delete_after_link_expires }) => { await utils.video.get.cancel(); const previousValue = utils.video.get.getData({ videoId }); if (previousValue) { utils.video.get.setData( { videoId }, { ...previousValue, delete_after_link_expires } ); } return { previousValue }; }, onError: (err, { videoId }, context) => { if (context?.previousValue) { utils.video.get.setData({ videoId }, context.previousValue); } console.error(err.message); }, }); const [linkCopied, setLinkCopied] = useState(false); const handleCopy = () => { void navigator.clipboard.writeText(window.location.href); setLinkCopied(true); setTimeout(() => { setLinkCopied(false); }, 5000); posthog?.capture("public video link copied", { videoSharing: video.sharing, videoId: video.id, }); }; return ( <> Share
Share this recording
Share link with anyone setSharingMutation.mutate({ videoId: video.id, sharing: !video.sharing, }) } />
{video.sharing ? ( <>
Expire link
Delete video after link expires setDeleteAfterLinkExpiresMutation.mutate({ videoId: video.id, delete_after_link_expires: !video.delete_after_link_expires, }) } />
) : null}
); }