ClassroomLayout.tsx 5.3 KB

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