2
0

ClassroomLayout.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import React, { ReactNode } from 'react';
  2. import { Role } from './useClassroom.ts';
  3. import { useClassroomContext } from './ClassroomProvider.tsx';
  4. import {
  5. VideoCameraIcon,
  6. CameraIcon,
  7. MicrophoneIcon,
  8. ShareIcon,
  9. ClipboardDocumentIcon,
  10. PaperAirplaneIcon
  11. } from '@heroicons/react/24/outline';
  12. interface ClassroomLayoutProps {
  13. children: ReactNode;
  14. role: Role;
  15. }
  16. export const ClassroomLayout = ({ children, role }: ClassroomLayoutProps) => {
  17. const [showVideo, setShowVideo] = React.useState(role !== Role.Teacher);
  18. const [showShareLink, setShowShareLink] = React.useState(false);
  19. const {
  20. remoteScreenContainer,
  21. remoteCameraContainer,
  22. isCameraOn,
  23. isAudioOn,
  24. isScreenSharing,
  25. toggleCamera,
  26. toggleAudio,
  27. toggleScreenShare,
  28. messageList,
  29. msgText,
  30. setMsgText,
  31. sendMessage,
  32. handUpList,
  33. questions,
  34. classStatus,
  35. shareLink,
  36. showCameraOverlay,
  37. setShowCameraOverlay
  38. } = useClassroomContext();
  39. return (
  40. <div className="flex flex-col md:flex-row h-screen bg-gray-100">
  41. {/* 视频区域 */}
  42. {showVideo && (
  43. <div className="relative h-[300px] md:flex-1 md:h-auto bg-black">
  44. {/* 主屏幕共享容器 */}
  45. <div
  46. id="remoteScreenContainer"
  47. ref={remoteScreenContainer}
  48. className="w-full h-full"
  49. >
  50. {/* 屏幕共享视频将在这里动态添加 */}
  51. </div>
  52. {/* 摄像头小窗容器 - 固定在右上角 */}
  53. <div
  54. id="remoteCameraContainer"
  55. ref={remoteCameraContainer}
  56. className={`absolute top-4 right-4 z-10 w-1/4 aspect-video ${
  57. showCameraOverlay ? 'block' : 'hidden'
  58. }`}
  59. >
  60. {/* 摄像头视频将在这里动态添加 */}
  61. </div>
  62. {/* 摄像头小窗开关按钮 */}
  63. <button
  64. onClick={() => setShowCameraOverlay(!showCameraOverlay)}
  65. className={`absolute top-4 right-4 z-20 p-2 rounded-full ${
  66. showCameraOverlay ? 'bg-green-500' : 'bg-gray-500'
  67. } text-white`}
  68. title={showCameraOverlay ? '隐藏摄像头小窗' : '显示摄像头小窗'}
  69. >
  70. <CameraIcon className="w-4 h-4" />
  71. </button>
  72. </div>
  73. )}
  74. {/* 消息和控制面板列 */}
  75. <div className={`${showVideo ? 'w-full md:w-96 flex-1' : 'flex-1'} flex flex-col`}>
  76. {/* 消息区域 */}
  77. <div className="flex flex-col h-full">
  78. {/* 消息列表 - 填充剩余空间 */}
  79. <div className="flex-1 overflow-y-auto bg-white shadow-lg p-4">
  80. {messageList.map((msg, i) => (
  81. <div key={i} className="text-sm mb-1">{msg}</div>
  82. ))}
  83. </div>
  84. </div>
  85. {/* 底部固定区域 */}
  86. <div className="bg-white shadow-lg p-4">
  87. {/* 控制面板 */}
  88. <div className="p-2 flex flex-col gap-3 mb-1 border-b border-gray-200">
  89. <div className="flex flex-wrap gap-2">
  90. {role === Role.Teacher && (
  91. <button
  92. onClick={() => setShowVideo(!showVideo)}
  93. className={`p-2 rounded-full ${showVideo ? 'bg-gray-500' : 'bg-gray-300'} text-white`}
  94. title={showVideo ? '隐藏视频' : '显示视频'}
  95. >
  96. <VideoCameraIcon className="w-4 h-4" />
  97. </button>
  98. )}
  99. <button
  100. onClick={toggleCamera}
  101. className={`p-2 rounded-full ${isCameraOn ? 'bg-green-500' : 'bg-red-500'} text-white`}
  102. title={isCameraOn ? '关闭摄像头' : '开启摄像头'}
  103. >
  104. <CameraIcon className="w-4 h-4" />
  105. </button>
  106. <button
  107. onClick={toggleAudio}
  108. className={`p-2 rounded-full ${isAudioOn ? 'bg-green-500' : 'bg-red-500'} text-white`}
  109. title={isAudioOn ? '关闭麦克风' : '开启麦克风'}
  110. >
  111. <MicrophoneIcon className="w-4 h-4" />
  112. </button>
  113. {role === Role.Teacher && (
  114. <button
  115. onClick={toggleScreenShare}
  116. className={`p-2 rounded-full ${isScreenSharing ? 'bg-green-500' : 'bg-blue-500'} text-white`}
  117. title={isScreenSharing ? '停止共享' : '共享屏幕'}
  118. >
  119. <ShareIcon className="w-4 h-4" />
  120. </button>
  121. )}
  122. {role === Role.Teacher && shareLink && (
  123. <button
  124. onClick={() => setShowShareLink(!showShareLink)}
  125. className="p-2 rounded-full bg-blue-500 text-white"
  126. title="分享链接"
  127. >
  128. <ClipboardDocumentIcon className="w-4 h-4" />
  129. </button>
  130. )}
  131. </div>
  132. {showShareLink && shareLink && (
  133. <div className="bg-blue-50 p-2 rounded">
  134. <div className="flex items-center gap-1">
  135. <input
  136. type="text"
  137. value={shareLink}
  138. readOnly
  139. className="flex-1 text-xs border rounded px-2 py-1 truncate"
  140. />
  141. <button
  142. onClick={() => navigator.clipboard.writeText(shareLink)}
  143. className="p-2 bg-blue-500 text-white rounded"
  144. title="复制链接"
  145. >
  146. <ClipboardDocumentIcon className="w-4 h-4" />
  147. </button>
  148. </div>
  149. </div>
  150. )}
  151. {/* 角色特定内容 */}
  152. <div className="flex-1 overflow-y-auto">
  153. {children}
  154. </div>
  155. </div>
  156. {/* 消息输入框 */}
  157. <div className="relative mt-2">
  158. <textarea
  159. value={msgText}
  160. onChange={(e) => setMsgText(e.target.value)}
  161. onKeyDown={(e) => {
  162. if (e.key === 'Enter' && !e.shiftKey) {
  163. e.preventDefault();
  164. sendMessage();
  165. }
  166. }}
  167. className="w-full border rounded px-2 py-1 pr-10"
  168. placeholder="输入消息..."
  169. rows={3}
  170. />
  171. <button
  172. onClick={sendMessage}
  173. className="absolute right-2 bottom-2 p-1 bg-blue-500 text-white rounded-full"
  174. >
  175. <PaperAirplaneIcon className="w-4 h-4" />
  176. </button>
  177. </div>
  178. </div>
  179. </div>
  180. </div>
  181. );
  182. };