ClassroomLayout.tsx 5.8 KB

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