|
| 1 | +import VideoContainer from "@components/session/VideoContainer.tsx"; |
| 2 | +import { useCallback, useEffect, useState } from "react"; |
| 3 | +import useToast from "@hooks/useToast.ts"; |
| 4 | + |
| 5 | +interface MediaPreviewModalProps { |
| 6 | + modal: UseModalReturn; |
| 7 | + nickname?: string; |
| 8 | + setNickname: (nickname: string) => void; |
| 9 | + isVideoOn: boolean; |
| 10 | + setIsVideoOn: (value: boolean) => void; |
| 11 | + onConfirm: () => void; |
| 12 | + onReject: () => void; |
| 13 | + setReady: (ready: boolean) => void; |
| 14 | + getMediaStream: (type: "video" | "audio") => Promise<MediaStream | undefined>; |
| 15 | +} |
| 16 | + |
| 17 | +interface UseModalReturn { |
| 18 | + dialogRef: React.RefObject<HTMLDialogElement>; |
| 19 | + isOpen: boolean; |
| 20 | + openModal: () => void; |
| 21 | + closeModal: () => void; |
| 22 | +} |
| 23 | + |
| 24 | +const MediaPreviewModal = ({ |
| 25 | + modal, |
| 26 | + nickname, |
| 27 | + setNickname, |
| 28 | + onConfirm, |
| 29 | + onReject, |
| 30 | + getMediaStream, |
| 31 | + isVideoOn, |
| 32 | + setIsVideoOn, |
| 33 | + setReady, |
| 34 | +}: MediaPreviewModalProps) => { |
| 35 | + const [preview, setPreview] = useState<MediaStream | undefined>(); |
| 36 | + const toast = useToast(); |
| 37 | + |
| 38 | + const getMediaPreview = useCallback(async () => { |
| 39 | + const mediaStream = await getMediaStream("video"); |
| 40 | + if (!mediaStream) { |
| 41 | + toast.error( |
| 42 | + "비디오 장치를 찾을 수 없습니다. 비디오 장치 없이 세션에 참가합니다." |
| 43 | + ); |
| 44 | + } |
| 45 | + setPreview(mediaStream); |
| 46 | + }, []); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + getMediaPreview(); |
| 50 | + |
| 51 | + return () => { |
| 52 | + preview?.getTracks().forEach((track) => track.stop()); |
| 53 | + }; |
| 54 | + }, []); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (modal.dialogRef.current) { |
| 58 | + const dialog = modal.dialogRef.current; |
| 59 | + const handleEscape = (event: globalThis.KeyboardEvent) => { |
| 60 | + if (event.key === "Escape") { |
| 61 | + event.preventDefault(); |
| 62 | + onReject(); |
| 63 | + setReady(false); |
| 64 | + } |
| 65 | + }; |
| 66 | + dialog.addEventListener("keydown", (event) => handleEscape(event)); |
| 67 | + } |
| 68 | + }, [modal.dialogRef.current]); |
| 69 | + |
| 70 | + return ( |
| 71 | + modal.isOpen && ( |
| 72 | + <dialog |
| 73 | + ref={modal.dialogRef} |
| 74 | + className={ |
| 75 | + "flex flex-col items-center rounded-custom-l px-10 py-6 w-[640px] shadow-lg" |
| 76 | + } |
| 77 | + > |
| 78 | + <h3 className={"text-bold-m"}>비디오 미리보기</h3> |
| 79 | + <div className={"w-[400px] p-4"}> |
| 80 | + <VideoContainer |
| 81 | + nickname={nickname || ""} |
| 82 | + isMicOn={true} |
| 83 | + isVideoOn={isVideoOn} |
| 84 | + isLocal={true} |
| 85 | + isSpeaking={false} |
| 86 | + reaction={""} |
| 87 | + stream={isVideoOn ? preview : undefined} |
| 88 | + videoCount={1} |
| 89 | + /> |
| 90 | + </div> |
| 91 | + <div className={"text-medium-r mt-4 flex w-full justify-center gap-4"}> |
| 92 | + <label |
| 93 | + className={ |
| 94 | + "inline-flex gap-2 items-center rounded-custom-m px-4 py-2 bg-transparent" |
| 95 | + } |
| 96 | + id={"checkbox"} |
| 97 | + > |
| 98 | + <input |
| 99 | + defaultChecked={!isVideoOn || false} |
| 100 | + className={"w-6 h-6"} |
| 101 | + type={"checkbox"} |
| 102 | + title={"dd"} |
| 103 | + onClick={() => setIsVideoOn(!isVideoOn)} |
| 104 | + /> |
| 105 | + <span>내 비디오 끄고 참가하기</span> |
| 106 | + </label> |
| 107 | + <input |
| 108 | + className={ |
| 109 | + "rounded-custom-m px-4 py-2 bg-gray-50 hover:bg-gray-100" |
| 110 | + } |
| 111 | + type={"text"} |
| 112 | + defaultValue={nickname} |
| 113 | + placeholder={"닉네임 변경"} |
| 114 | + onChange={(e) => setNickname(e.target.value)} |
| 115 | + /> |
| 116 | + </div> |
| 117 | + <div |
| 118 | + className={"text-semibold-r mt-4 flex w-full justify-center gap-4"} |
| 119 | + > |
| 120 | + <button |
| 121 | + onClick={() => { |
| 122 | + onReject(); |
| 123 | + setReady(false); |
| 124 | + preview?.getTracks().forEach((track) => track.stop()); |
| 125 | + }} |
| 126 | + className={ |
| 127 | + "rounded-custom-m px-16 py-4 bg-gray-50 hover:bg-gray-100" |
| 128 | + } |
| 129 | + > |
| 130 | + 세션 나가기 |
| 131 | + </button> |
| 132 | + <button |
| 133 | + onClick={() => { |
| 134 | + onConfirm(); |
| 135 | + setReady(true); |
| 136 | + modal.closeModal(); |
| 137 | + }} |
| 138 | + className={ |
| 139 | + "rounded-custom-m px-16 py-4 bg-green-500 text-white hover:bg-green-600" |
| 140 | + } |
| 141 | + > |
| 142 | + 세션 참가 |
| 143 | + </button> |
| 144 | + </div> |
| 145 | + </dialog> |
| 146 | + ) |
| 147 | + ); |
| 148 | +}; |
| 149 | + |
| 150 | +export default MediaPreviewModal; |
0 commit comments