ClassroomLayout.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. type="button"
  65. onClick={() => setShowCameraOverlay(!showCameraOverlay)}
  66. className={`absolute top-4 right-4 z-20 p-2 rounded-full ${
  67. showCameraOverlay ? 'bg-green-500' : 'bg-gray-500'
  68. } text-white`}
  69. title={showCameraOverlay ? '隐藏摄像头小窗' : '显示摄像头小窗'}
  70. >
  71. <CameraIcon className="w-4 h-4" />
  72. </button>
  73. </div>
  74. )}
  75. {/* 消息和控制面板列 */}
  76. <div className={`${showVideo ? 'w-full md:w-96 flex-1' : 'flex-1'} flex flex-col`}>
  77. {/* 消息区域 */}
  78. <div className="flex flex-col h-full">
  79. {/* 消息列表 - 填充剩余空间 */}
  80. <div className="flex-1 overflow-y-auto bg-white shadow-lg p-4">
  81. {messageList.map((msg, i) => (
  82. <div key={i} className="text-sm mb-1">{msg}</div>
  83. ))}
  84. </div>
  85. </div>
  86. {/* 底部固定区域 */}
  87. <div className="bg-white shadow-lg p-4">
  88. {/* 控制面板 */}
  89. <div className="p-2 flex flex-col gap-3 mb-1 border-b border-gray-200">
  90. <div className="flex flex-wrap gap-2">
  91. {role === Role.Teacher && (
  92. <button
  93. type="button"
  94. onClick={() => setShowVideo(!showVideo)}
  95. className={`p-2 rounded-full ${showVideo ? 'bg-gray-500' : 'bg-gray-300'} text-white`}
  96. title={showVideo ? '隐藏视频' : '显示视频'}
  97. >
  98. <VideoCameraIcon className="w-4 h-4" />
  99. </button>
  100. )}
  101. <button
  102. type="button"
  103. onClick={toggleCamera}
  104. className={`p-2 rounded-full ${isCameraOn ? 'bg-green-500' : 'bg-red-500'} text-white`}
  105. title={isCameraOn ? '关闭摄像头' : '开启摄像头'}
  106. >
  107. <CameraIcon className="w-4 h-4" />
  108. </button>
  109. <button
  110. type="button"
  111. onClick={toggleAudio}
  112. className={`p-2 rounded-full ${isAudioOn ? 'bg-green-500' : 'bg-red-500'} text-white`}
  113. title={isAudioOn ? '关闭麦克风' : '开启麦克风'}
  114. >
  115. <MicrophoneIcon className="w-4 h-4" />
  116. </button>
  117. {role === Role.Teacher && (
  118. <button
  119. type="button"
  120. onClick={toggleScreenShare}
  121. className={`p-2 rounded-full ${isScreenSharing ? 'bg-green-500' : 'bg-blue-500'} text-white`}
  122. title={isScreenSharing ? '停止共享' : '共享屏幕'}
  123. >
  124. <ShareIcon className="w-4 h-4" />
  125. </button>
  126. )}
  127. {role === Role.Teacher && shareLink && (
  128. <button
  129. type="button"
  130. onClick={() => setShowShareLink(!showShareLink)}
  131. className="p-2 rounded-full bg-blue-500 text-white"
  132. title="分享链接"
  133. >
  134. <ClipboardDocumentIcon className="w-4 h-4" />
  135. </button>
  136. )}
  137. </div>
  138. {showShareLink && shareLink && (
  139. <div className="bg-blue-50 p-2 rounded">
  140. <div className="flex items-center gap-1">
  141. <input
  142. type="text"
  143. value={shareLink}
  144. readOnly
  145. className="flex-1 text-xs border rounded px-2 py-1 truncate"
  146. />
  147. <button
  148. type="button"
  149. onClick={() => navigator.clipboard.writeText(shareLink)}
  150. className="p-2 bg-blue-500 text-white rounded"
  151. title="复制链接"
  152. >
  153. <ClipboardDocumentIcon className="w-4 h-4" />
  154. </button>
  155. </div>
  156. </div>
  157. )}
  158. {/* 角色特定内容 */}
  159. <div className="flex-1 overflow-y-auto">
  160. {children}
  161. </div>
  162. </div>
  163. {/* 消息输入框 */}
  164. <div className="relative mt-2">
  165. <textarea
  166. value={msgText}
  167. onChange={(e) => setMsgText(e.target.value)}
  168. onKeyDown={(e) => {
  169. if (e.key === 'Enter' && !e.shiftKey) {
  170. e.preventDefault();
  171. sendMessage();
  172. }
  173. }}
  174. className="w-full border rounded px-2 py-1 pr-10"
  175. placeholder="输入消息..."
  176. rows={3}
  177. />
  178. <button
  179. type="button"
  180. onClick={sendMessage}
  181. className="absolute right-2 bottom-2 p-1 bg-blue-500 text-white rounded-full"
  182. >
  183. <PaperAirplaneIcon className="w-4 h-4" />
  184. </button>
  185. </div>
  186. </div>
  187. </div>
  188. </div>
  189. );
  190. };