import React, { useState, useRef, useEffect, use } from "react"; import { featureEnabled } from "../domainContext"; import { useFiefUserinfo } from "@fief/fief/nextjs/react"; import SelectSearch from "react-select-search"; import "react-select-search/style.css"; import "../../styles/button.css"; import "../../styles/form.scss"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faSpinner } from "@fortawesome/free-solid-svg-icons"; import { UpdateTranscript } from "../../api"; import { ShareMode, toShareMode } from "../../lib/shareMode"; import useApi from "../../lib/useApi"; type ShareLinkProps = { transcriptId: string; userId: string | null; shareMode: ShareMode; }; const ShareLink = (props: ShareLinkProps) => { const [isCopied, setIsCopied] = useState(false); const inputRef = useRef(null); const [currentUrl, setCurrentUrl] = useState(""); const requireLogin = featureEnabled("requireLogin"); const [isOwner, setIsOwner] = useState(false); const [shareMode, setShareMode] = useState(props.shareMode); const [shareLoading, setShareLoading] = useState(false); const userinfo = useFiefUserinfo(); const api = useApi(); useEffect(() => { setCurrentUrl(window.location.href); }, []); useEffect(() => { setIsOwner(!!(requireLogin && userinfo?.sub === props.userId)); }, [userinfo, props.userId]); const handleCopyClick = () => { if (inputRef.current) { let text_to_copy = inputRef.current.value; text_to_copy && navigator.clipboard.writeText(text_to_copy).then(() => { setIsCopied(true); // Reset the copied state after 2 seconds setTimeout(() => setIsCopied(false), 2000); }); } }; const updateShareMode = async (selectedShareMode: string) => { if (!api) throw new Error("ShareLink's API should always be ready at this point"); setShareLoading(true); const requestBody: UpdateTranscript = { share_mode: toShareMode(selectedShareMode), }; const updatedTranscript = await api.v1TranscriptUpdate( props.transcriptId, requestBody, ); setShareMode(toShareMode(updatedTranscript.share_mode)); setShareLoading(false); }; const privacyEnabled = featureEnabled("privacy"); return (
{requireLogin && (
{shareMode === "private" && (

This transcript is private and can only be accessed by you.

)} {shareMode === "semi-private" && (

This transcript is secure. Only authenticated users can access it.

)} {shareMode === "public" && (

This transcript is public. Everyone can access it.

)} {isOwner && api && (
{shareLoading && (
)}
)}
)} {!requireLogin && ( <> {privacyEnabled ? (

Share this link to grant others access to this page. The link includes the full audio recording and is valid for the next 7 days.

) : (

Share this link to allow others to view this page and listen to the full audio recording.

)} )}
{}} className="border rounded-lg md:rounded-xl p-2 flex-grow flex-shrink overflow-auto mr-2 text-sm bg-slate-100 outline-slate-400" />
); }; export default ShareLink;