ClassroomLayout.tsx 5.6 KB

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