lecture-notes-view.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use client';
  2. import { useEffect, useRef } from 'react';
  3. import { BookOpen, MessageSquare, Flashlight, MousePointer2, Play } from 'lucide-react';
  4. import { cn } from '@/lib/utils';
  5. import { useI18n } from '@/lib/hooks/use-i18n';
  6. import type { LectureNoteEntry } from '@/lib/types/chat';
  7. const ACTION_ICON_ONLY: Record<string, { Icon: typeof Flashlight; style: string }> = {
  8. spotlight: {
  9. Icon: Flashlight,
  10. style:
  11. 'bg-yellow-50 dark:bg-yellow-500/15 border-yellow-300/40 dark:border-yellow-500/30 text-yellow-700 dark:text-yellow-300',
  12. },
  13. laser: {
  14. Icon: MousePointer2,
  15. style:
  16. 'bg-red-50 dark:bg-red-500/15 border-red-300/40 dark:border-red-500/30 text-red-600 dark:text-red-300',
  17. },
  18. play_video: {
  19. Icon: Play,
  20. style:
  21. 'bg-yellow-50 dark:bg-yellow-500/15 border-yellow-300/40 dark:border-yellow-500/30 text-yellow-700 dark:text-yellow-300',
  22. },
  23. };
  24. interface LectureNotesViewProps {
  25. notes: LectureNoteEntry[];
  26. currentSceneId?: string | null;
  27. }
  28. export function LectureNotesView({ notes, currentSceneId }: LectureNotesViewProps) {
  29. const { t } = useI18n();
  30. const containerRef = useRef<HTMLDivElement>(null);
  31. // Auto-scroll to the current scene note
  32. useEffect(() => {
  33. if (!currentSceneId || !containerRef.current) return;
  34. const el = containerRef.current.querySelector(`[data-scene-id="${currentSceneId}"]`);
  35. if (el) {
  36. el.scrollIntoView({ behavior: 'smooth', block: 'center' });
  37. }
  38. }, [currentSceneId]);
  39. // Empty state
  40. if (notes.length === 0) {
  41. return (
  42. <div className="h-full flex flex-col items-center justify-center text-center p-6">
  43. <div className="w-12 h-12 bg-purple-50 dark:bg-purple-900/20 rounded-2xl flex items-center justify-center mb-3 text-purple-300 dark:text-purple-600 ring-1 ring-purple-100 dark:ring-purple-800/30">
  44. <BookOpen className="w-6 h-6" />
  45. </div>
  46. <p className="text-xs font-medium text-gray-500 dark:text-gray-400">
  47. {t('chat.lectureNotes.empty')}
  48. </p>
  49. <p className="text-[10px] text-gray-400 dark:text-gray-500 mt-1">
  50. {t('chat.lectureNotes.emptyHint')}
  51. </p>
  52. </div>
  53. );
  54. }
  55. return (
  56. <div
  57. ref={containerRef}
  58. className="flex-1 overflow-y-auto overflow-x-hidden px-3 py-2 scrollbar-hide"
  59. >
  60. {notes.map((note, index) => {
  61. const isCurrent = note.sceneId === currentSceneId;
  62. const pageNum = index + 1;
  63. const pageLabel = t('chat.lectureNotes.pageLabel', { n: pageNum });
  64. return (
  65. <div
  66. key={note.sceneId}
  67. data-scene-id={note.sceneId}
  68. className={cn(
  69. 'relative mb-3 last:mb-0 rounded-lg px-3 py-2.5 transition-colors duration-200',
  70. isCurrent
  71. ? 'bg-purple-50/80 dark:bg-purple-950/25 ring-1 ring-purple-200/60 dark:ring-purple-700/30'
  72. : 'bg-gray-50/50 dark:bg-gray-800/30',
  73. )}
  74. >
  75. {/* Page label row */}
  76. <div className="flex items-center gap-2 mb-1.5">
  77. {/* Timeline dot */}
  78. <div
  79. className={cn(
  80. 'w-2 h-2 rounded-full shrink-0',
  81. isCurrent
  82. ? 'bg-purple-500 dark:bg-purple-400 shadow-sm shadow-purple-400/40'
  83. : 'bg-gray-300 dark:bg-gray-600',
  84. )}
  85. />
  86. <span
  87. className={cn(
  88. 'text-[10px] font-semibold tracking-wide',
  89. isCurrent
  90. ? 'text-purple-600 dark:text-purple-400'
  91. : 'text-gray-400 dark:text-gray-500',
  92. )}
  93. >
  94. {pageLabel}
  95. </span>
  96. {isCurrent && (
  97. <span className="text-[9px] font-bold px-1.5 py-px rounded-full bg-purple-100 dark:bg-purple-900/40 text-purple-600 dark:text-purple-300">
  98. {t('chat.lectureNotes.currentPage')}
  99. </span>
  100. )}
  101. </div>
  102. {/* Scene title */}
  103. <h4 className="text-[13px] font-bold text-gray-800 dark:text-gray-100 mb-1.5 leading-snug pl-4">
  104. {note.sceneTitle}
  105. </h4>
  106. {/* Ordered items: spotlight/laser inline at sentence start, discussion as card */}
  107. <div className="pl-4 space-y-1">
  108. {(() => {
  109. // Build render rows: group inline actions (spotlight/laser) with next speech,
  110. // but render discussion as its own block
  111. type Row =
  112. | { kind: 'speech'; inlineActions: string[]; text: string }
  113. | { kind: 'discussion'; label?: string }
  114. | { kind: 'trailing'; inlineActions: string[] };
  115. const rows: Row[] = [];
  116. let pendingInline: string[] = [];
  117. for (const item of note.items) {
  118. if (item.kind === 'action' && item.type === 'discussion') {
  119. // Flush pending inline actions as trailing if any
  120. if (pendingInline.length > 0) {
  121. rows.push({
  122. kind: 'trailing',
  123. inlineActions: pendingInline,
  124. });
  125. pendingInline = [];
  126. }
  127. rows.push({ kind: 'discussion', label: item.label });
  128. } else if (item.kind === 'action') {
  129. pendingInline.push(item.type);
  130. } else {
  131. rows.push({
  132. kind: 'speech',
  133. inlineActions: pendingInline,
  134. text: item.text,
  135. });
  136. pendingInline = [];
  137. }
  138. }
  139. if (pendingInline.length > 0) {
  140. rows.push({ kind: 'trailing', inlineActions: pendingInline });
  141. }
  142. return rows.map((row, i) => {
  143. if (row.kind === 'discussion') {
  144. return (
  145. <div
  146. key={i}
  147. className="my-1.5 flex items-start gap-1.5 rounded-md border border-amber-200/60 dark:border-amber-700/30 bg-amber-50/60 dark:bg-amber-900/10 px-2 py-1.5"
  148. >
  149. <MessageSquare className="w-3 h-3 text-amber-500 dark:text-amber-400 shrink-0 mt-0.5" />
  150. <span className="text-[11px] leading-snug text-amber-800 dark:text-amber-300">
  151. {row.label}
  152. </span>
  153. </div>
  154. );
  155. }
  156. const actions = row.kind === 'trailing' ? row.inlineActions : row.inlineActions;
  157. return (
  158. <p
  159. key={i}
  160. className="text-[12px] leading-[1.8] text-gray-700 dark:text-gray-300"
  161. >
  162. {actions.map((a, j) => {
  163. const cfg = ACTION_ICON_ONLY[a];
  164. if (!cfg) return null;
  165. const { Icon, style } = cfg;
  166. return (
  167. <span
  168. key={j}
  169. className={cn(
  170. 'inline-flex items-center justify-center w-4 h-4 rounded-full border align-middle mr-0.5',
  171. style,
  172. )}
  173. >
  174. <Icon className="w-2.5 h-2.5" />
  175. </span>
  176. );
  177. })}
  178. {row.kind === 'speech' ? row.text : null}
  179. </p>
  180. );
  181. });
  182. })()}
  183. </div>
  184. </div>
  185. );
  186. })}
  187. </div>
  188. );
  189. }