ClassroomLayout.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. remoteVideoContainer,
  21. isCameraOn,
  22. isAudioOn,
  23. isScreenSharing,
  24. toggleCamera,
  25. toggleAudio,
  26. toggleScreenShare,
  27. messageList,
  28. msgText,
  29. setMsgText,
  30. sendMessage,
  31. handUpList,
  32. questions,
  33. classStatus,
  34. shareLink
  35. } = useClassroomContext();
  36. return (
  37. <div className="flex flex-col md:flex-row h-screen bg-gray-100">
  38. {/* 视频区域 */}
  39. {showVideo && (
  40. <div className="p-4 h-[300px] md:flex-1 md:h-auto">
  41. <div className="bg-white rounded-lg shadow p-4 h-full">
  42. <div
  43. id="remoteVideoContainer"
  44. ref={remoteVideoContainer}
  45. className="grid grid-cols-1 md:grid-cols-2 gap-4"
  46. >
  47. {/* 远程视频将在这里动态添加 */}
  48. </div>
  49. </div>
  50. </div>
  51. )}
  52. {/* 消息和控制面板列 */}
  53. <div className={`${showVideo ? 'w-full md:w-96 flex-1' : 'flex-1'} flex flex-col`}>
  54. {/* 消息区域 */}
  55. <div className="flex flex-col h-full">
  56. {/* 消息列表 - 填充剩余空间 */}
  57. <div className="flex-1 overflow-y-auto bg-white shadow-lg p-4">
  58. {messageList.map((msg, i) => (
  59. <div key={i} className="text-sm mb-1">{msg}</div>
  60. ))}
  61. </div>
  62. </div>
  63. {/* 底部固定区域 */}
  64. <div className="bg-white shadow-lg p-4">
  65. {/* 控制面板 */}
  66. <div className="p-2 flex flex-col gap-3 mb-1 border-b border-gray-200">
  67. <div className="flex flex-wrap gap-2">
  68. {role === Role.Teacher && (
  69. <button
  70. onClick={() => setShowVideo(!showVideo)}
  71. className={`p-2 rounded-full ${showVideo ? 'bg-gray-500' : 'bg-gray-300'} text-white`}
  72. title={showVideo ? '隐藏视频' : '显示视频'}
  73. >
  74. <VideoCameraIcon className="w-4 h-4" />
  75. </button>
  76. )}
  77. <button
  78. onClick={toggleCamera}
  79. className={`p-2 rounded-full ${isCameraOn ? 'bg-green-500' : 'bg-red-500'} text-white`}
  80. title={isCameraOn ? '关闭摄像头' : '开启摄像头'}
  81. >
  82. <CameraIcon className="w-4 h-4" />
  83. </button>
  84. <button
  85. onClick={toggleAudio}
  86. className={`p-2 rounded-full ${isAudioOn ? 'bg-green-500' : 'bg-red-500'} text-white`}
  87. title={isAudioOn ? '关闭麦克风' : '开启麦克风'}
  88. >
  89. <MicrophoneIcon className="w-4 h-4" />
  90. </button>
  91. {role === Role.Teacher && (
  92. <button
  93. onClick={toggleScreenShare}
  94. className={`p-2 rounded-full ${isScreenSharing ? 'bg-green-500' : 'bg-blue-500'} text-white`}
  95. title={isScreenSharing ? '停止共享' : '共享屏幕'}
  96. >
  97. <ShareIcon className="w-4 h-4" />
  98. </button>
  99. )}
  100. {role === Role.Teacher && shareLink && (
  101. <button
  102. onClick={() => setShowShareLink(!showShareLink)}
  103. className="p-2 rounded-full bg-blue-500 text-white"
  104. title="分享链接"
  105. >
  106. <ClipboardDocumentIcon className="w-4 h-4" />
  107. </button>
  108. )}
  109. </div>
  110. {showShareLink && shareLink && (
  111. <div className="bg-blue-50 p-2 rounded">
  112. <div className="flex items-center gap-1">
  113. <input
  114. type="text"
  115. value={shareLink}
  116. readOnly
  117. className="flex-1 text-xs border rounded px-2 py-1 truncate"
  118. />
  119. <button
  120. onClick={() => navigator.clipboard.writeText(shareLink)}
  121. className="p-2 bg-blue-500 text-white rounded"
  122. title="复制链接"
  123. >
  124. <ClipboardDocumentIcon className="w-4 h-4" />
  125. </button>
  126. </div>
  127. </div>
  128. )}
  129. {/* 角色特定内容 */}
  130. <div className="flex-1 overflow-y-auto">
  131. {children}
  132. </div>
  133. </div>
  134. {/* 消息输入框 */}
  135. <div className="relative mt-2">
  136. <textarea
  137. value={msgText}
  138. onChange={(e) => setMsgText(e.target.value)}
  139. className="w-full border rounded px-2 py-1 pr-10"
  140. placeholder="输入消息..."
  141. rows={3}
  142. />
  143. <button
  144. onClick={sendMessage}
  145. className="absolute right-2 bottom-2 p-1 bg-blue-500 text-white rounded-full"
  146. >
  147. <PaperAirplaneIcon className="w-4 h-4" />
  148. </button>
  149. </div>
  150. </div>
  151. </div>
  152. </div>
  153. );
  154. };