feat(cis): add Supabase integration, canvas event logger, and sessions tab (Phase 2)
- Connect transcriptionStore to Supabase (start/stop session, save segments) - Add CanvasEventLogger for silent TLDraw activity tracking - Add Sessions tab to CCTranscriptionPanel with past sessions list - Auto-detect timetable context on panel mount - Flush canvas events to API every 5 seconds during recording
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import { Mic as MicIcon, Stop as StopIcon } from "@mui/icons-material";
|
||||
import { useTranscriptionStore, TranscriptionSegment } from "../../../../../stores/transcriptionStore";
|
||||
import { Mic as MicIcon, Stop as StopIcon, History as HistoryIcon, Add as AddIcon } from "@mui/icons-material";
|
||||
import { useTranscriptionStore, TranscriptionSegment, TranscriptionSession, TimetablePeriod } from "../../../../../stores/transcriptionStore";
|
||||
import { TranscriptionService } from "../../../cc-base/cc-transcription/transcriptionService";
|
||||
import { CanvasEventLogger } from "../../../cc-base/canvas-event-logger/CanvasEventLogger";
|
||||
import "./panel.css";
|
||||
|
||||
const formatTime = (seconds: number): string => {
|
||||
@@ -10,6 +11,13 @@ const formatTime = (seconds: number): string => {
|
||||
return m.toString().padStart(2, "0") + ":" + s.toString().padStart(2, "0");
|
||||
};
|
||||
|
||||
const formatDateTime = (isoString: string): string => {
|
||||
const date = new Date(isoString);
|
||||
return date.toLocaleDateString() + " " + date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
};
|
||||
|
||||
type TabType = "live" | "sessions";
|
||||
|
||||
export const CCTranscriptionPanel: React.FC = () => {
|
||||
const {
|
||||
isRecording,
|
||||
@@ -17,17 +25,48 @@ export const CCTranscriptionPanel: React.FC = () => {
|
||||
currentSegment,
|
||||
wordCount,
|
||||
elapsedSeconds,
|
||||
activeSession,
|
||||
timetableContext,
|
||||
startSession,
|
||||
stopSession,
|
||||
saveSegment,
|
||||
resetSession,
|
||||
tickElapsed,
|
||||
addCanvasEvent,
|
||||
flushCanvasEvents,
|
||||
loadSessions,
|
||||
setTimetableContext,
|
||||
} = useTranscriptionStore();
|
||||
|
||||
const [sessionName] = useState("Untitled Session");
|
||||
const [activeTab, setActiveTab] = useState<TabType>("live");
|
||||
const [sessions, setSessions] = useState<TranscriptionSession[]>([]);
|
||||
const [sessionName, setSessionName] = useState("Untitled Session");
|
||||
const serviceRef = useRef<TranscriptionService | null>(null);
|
||||
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const canvasLoggerRef = useRef<CanvasEventLogger | null>(null);
|
||||
|
||||
// Load sessions on mount
|
||||
useEffect(() => {
|
||||
loadSessions().then(setSessions);
|
||||
}, []);
|
||||
|
||||
// Auto-detect timetable context on mount
|
||||
useEffect(() => {
|
||||
const detectTimetable = async () => {
|
||||
try {
|
||||
const response = await fetch('http://192.168.0.64:8000/database/timetables/current-period');
|
||||
const data = await response.json();
|
||||
if (data.period_id) {
|
||||
setTimetableContext(data as TimetablePeriod);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to detect timetable context:', error);
|
||||
}
|
||||
};
|
||||
detectTimetable();
|
||||
}, []);
|
||||
|
||||
// Timer for elapsed seconds
|
||||
useEffect(() => {
|
||||
if (isRecording) {
|
||||
timerRef.current = setInterval(tickElapsed, 1000);
|
||||
@@ -40,27 +79,53 @@ export const CCTranscriptionPanel: React.FC = () => {
|
||||
};
|
||||
}, [isRecording, tickElapsed]);
|
||||
|
||||
// Canvas event flush interval (every 5s)
|
||||
useEffect(() => {
|
||||
if (!isRecording) return;
|
||||
|
||||
const flushInterval = setInterval(async () => {
|
||||
await flushCanvasEvents();
|
||||
}, 5000);
|
||||
|
||||
return () => clearInterval(flushInterval);
|
||||
}, [isRecording, flushCanvasEvents]);
|
||||
|
||||
const handleStart = async () => {
|
||||
try {
|
||||
startSession();
|
||||
await startSession(timetableContext || undefined);
|
||||
const service = new TranscriptionService();
|
||||
service.setTranscriptionCallback((text, isFinal, metadata) => {
|
||||
saveSegment(text, isFinal, metadata);
|
||||
});
|
||||
await service.startTranscription();
|
||||
serviceRef.current = service;
|
||||
|
||||
// Initialize canvas event logger if session was created
|
||||
const state = useTranscriptionStore.getState();
|
||||
if (state.activeSession) {
|
||||
// TODO: Get editor instance from TLDraw context
|
||||
// For now, just log that canvas logging would start
|
||||
console.log('[CCTranscriptionPanel] Canvas event logging would start for session', state.activeSession.id);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to start transcription:", error);
|
||||
stopSession();
|
||||
}
|
||||
};
|
||||
|
||||
const handleStop = () => {
|
||||
const handleStop = async () => {
|
||||
if (serviceRef.current) {
|
||||
serviceRef.current.stopTranscription();
|
||||
serviceRef.current = null;
|
||||
}
|
||||
stopSession();
|
||||
|
||||
// Detach canvas event logger
|
||||
if (canvasLoggerRef.current) {
|
||||
canvasLoggerRef.current.detach();
|
||||
canvasLoggerRef.current = null;
|
||||
}
|
||||
|
||||
await stopSession();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@@ -72,92 +137,228 @@ export const CCTranscriptionPanel: React.FC = () => {
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleRefreshSessions = async () => {
|
||||
const loaded = await loadSessions();
|
||||
setSessions(loaded);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="panel-container">
|
||||
<div className="panel-section">
|
||||
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
|
||||
<span style={{ fontSize: "14px", fontWeight: 500, color: "var(--color-text)" }}>
|
||||
{sessionName}
|
||||
</span>
|
||||
<div style={{ display: "flex", gap: "12px", fontSize: "12px", color: "var(--color-text-2)" }}>
|
||||
<span>{wordCount} words</span>
|
||||
<span>{formatTime(elapsedSeconds)}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="panel-divider" />
|
||||
|
||||
<div className="panel-section">
|
||||
{/* Tab bar */}
|
||||
<div style={{ display: "flex", borderBottom: "1px solid var(--color-divider)", marginBottom: "8px" }}>
|
||||
<button
|
||||
onClick={isRecording ? handleStop : handleStart}
|
||||
onClick={() => setActiveTab("live")}
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
gap: "8px",
|
||||
padding: "16px",
|
||||
width: "100%",
|
||||
borderRadius: "8px",
|
||||
flex: 1,
|
||||
padding: "8px",
|
||||
border: "none",
|
||||
backgroundColor: activeTab === "live" ? "var(--color-hover)" : "transparent",
|
||||
color: "var(--color-text)",
|
||||
cursor: "pointer",
|
||||
fontSize: "14px",
|
||||
fontWeight: 600,
|
||||
color: "#fff",
|
||||
backgroundColor: isRecording ? "#ef4444" : "var(--color-text)",
|
||||
transition: "background-color 200ms ease",
|
||||
fontSize: "13px",
|
||||
fontWeight: activeTab === "live" ? 600 : 400,
|
||||
borderBottom: activeTab === "live" ? "2px solid var(--color-text)" : "none",
|
||||
}}
|
||||
>
|
||||
{isRecording ? <StopIcon /> : <MicIcon />}
|
||||
{isRecording ? "Stop Recording" : "Start Recording"}
|
||||
<HistoryIcon style={{ fontSize: "16px", verticalAlign: "middle", marginRight: "4px" }} />
|
||||
Live
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setActiveTab("sessions")}
|
||||
style={{
|
||||
flex: 1,
|
||||
padding: "8px",
|
||||
border: "none",
|
||||
backgroundColor: activeTab === "sessions" ? "var(--color-hover)" : "transparent",
|
||||
color: "var(--color-text)",
|
||||
cursor: "pointer",
|
||||
fontSize: "13px",
|
||||
fontWeight: activeTab === "sessions" ? 600 : 400,
|
||||
borderBottom: activeTab === "sessions" ? "2px solid var(--color-text)" : "none",
|
||||
}}
|
||||
>
|
||||
<HistoryIcon style={{ fontSize: "16px", verticalAlign: "middle", marginRight: "4px" }} />
|
||||
Sessions
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="panel-divider" />
|
||||
{/* Live Tab */}
|
||||
{activeTab === "live" && (
|
||||
<>
|
||||
{/* Session header */}
|
||||
<div className="panel-section">
|
||||
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
|
||||
<span style={{ fontSize: "14px", fontWeight: 500, color: "var(--color-text)" }}>
|
||||
{sessionName}
|
||||
</span>
|
||||
<div style={{ display: "flex", gap: "12px", fontSize: "12px", color: "var(--color-text-2)" }}>
|
||||
<span>{wordCount} words</span>
|
||||
<span>{formatTime(elapsedSeconds)}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="panel-section" style={{ gap: "6px" }}>
|
||||
<div className="panel-section-title">Live Feed</div>
|
||||
|
||||
{completedSegments.map((seg, i) => (
|
||||
<div
|
||||
key={"completed-" + i}
|
||||
style={{
|
||||
padding: "8px 10px",
|
||||
backgroundColor: "#fff",
|
||||
borderRadius: "4px",
|
||||
border: "1px solid var(--color-divider)",
|
||||
fontSize: "13px",
|
||||
color: "var(--color-text)",
|
||||
lineHeight: 1.4,
|
||||
}}
|
||||
>
|
||||
{seg.text}
|
||||
{/* Timetable badge */}
|
||||
{timetableContext && timetableContext.event_label && (
|
||||
<div style={{
|
||||
marginTop: "8px",
|
||||
padding: "4px 8px",
|
||||
backgroundColor: "var(--color-hover)",
|
||||
borderRadius: "4px",
|
||||
fontSize: "12px",
|
||||
color: "var(--color-text)",
|
||||
}}>
|
||||
📅 {timetableContext.event_label}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
|
||||
{currentSegment && (
|
||||
<div
|
||||
style={{
|
||||
padding: "8px 10px",
|
||||
backgroundColor: "var(--color-panel)",
|
||||
borderRadius: "4px",
|
||||
border: "1px dashed var(--color-divider)",
|
||||
fontSize: "13px",
|
||||
color: "var(--color-text-2)",
|
||||
fontStyle: "italic",
|
||||
lineHeight: 1.4,
|
||||
}}
|
||||
>
|
||||
{currentSegment.text || "Listening..."}
|
||||
</div>
|
||||
)}
|
||||
<div className="panel-divider" />
|
||||
|
||||
{!isRecording && completedSegments.length === 0 && !currentSegment && (
|
||||
<div style={{ textAlign: "center", color: "var(--color-text-2)", padding: "16px", fontSize: "13px" }}>
|
||||
Press Start Recording to begin transcription
|
||||
{/* Record button */}
|
||||
<div className="panel-section">
|
||||
<button
|
||||
onClick={isRecording ? handleStop : handleStart}
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
gap: "8px",
|
||||
padding: "16px",
|
||||
width: "100%",
|
||||
borderRadius: "8px",
|
||||
border: "none",
|
||||
cursor: "pointer",
|
||||
fontSize: "14px",
|
||||
fontWeight: 600,
|
||||
color: "#fff",
|
||||
backgroundColor: isRecording ? "#ef4444" : "var(--color-text)",
|
||||
transition: "background-color 200ms ease",
|
||||
}}
|
||||
>
|
||||
{isRecording ? <StopIcon /> : <MicIcon />}
|
||||
{isRecording ? "Stop Recording" : "Start Recording"}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="panel-divider" />
|
||||
|
||||
{/* Live feed */}
|
||||
<div className="panel-section" style={{ gap: "6px" }}>
|
||||
<div className="panel-section-title">Live Feed</div>
|
||||
|
||||
{completedSegments.map((seg, i) => (
|
||||
<div
|
||||
key={"completed-" + i}
|
||||
style={{
|
||||
padding: "8px 10px",
|
||||
backgroundColor: "#fff",
|
||||
borderRadius: "4px",
|
||||
border: "1px solid var(--color-divider)",
|
||||
fontSize: "13px",
|
||||
color: "var(--color-text)",
|
||||
lineHeight: 1.4,
|
||||
}}
|
||||
>
|
||||
{seg.text}
|
||||
</div>
|
||||
))}
|
||||
|
||||
{currentSegment && (
|
||||
<div
|
||||
style={{
|
||||
padding: "8px 10px",
|
||||
backgroundColor: "var(--color-panel)",
|
||||
borderRadius: "4px",
|
||||
border: "1px dashed var(--color-divider)",
|
||||
fontSize: "13px",
|
||||
color: "var(--color-text-2)",
|
||||
fontStyle: "italic",
|
||||
lineHeight: 1.4,
|
||||
}}
|
||||
>
|
||||
{currentSegment.text || "Listening..."}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isRecording && completedSegments.length === 0 && !currentSegment && (
|
||||
<div style={{ textAlign: "center", color: "var(--color-text-2)", padding: "16px", fontSize: "13px" }}>
|
||||
Press Start Recording to begin transcription
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Sessions Tab */}
|
||||
{activeTab === "sessions" && (
|
||||
<>
|
||||
<div className="panel-section">
|
||||
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
|
||||
<span style={{ fontSize: "14px", fontWeight: 500, color: "var(--color-text)" }}>
|
||||
Past Sessions
|
||||
</span>
|
||||
<button
|
||||
onClick={handleRefreshSessions}
|
||||
style={{
|
||||
padding: "4px 8px",
|
||||
border: "1px solid var(--color-divider)",
|
||||
backgroundColor: "transparent",
|
||||
color: "var(--color-text)",
|
||||
borderRadius: "4px",
|
||||
cursor: "pointer",
|
||||
fontSize: "12px",
|
||||
}}
|
||||
>
|
||||
Refresh
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="panel-divider" />
|
||||
|
||||
<div className="panel-section" style={{ gap: "8px", maxHeight: "400px", overflowY: "auto" }}>
|
||||
{sessions.length === 0 ? (
|
||||
<div style={{ textAlign: "center", color: "var(--color-text-2)", padding: "16px", fontSize: "13px" }}>
|
||||
No sessions yet
|
||||
</div>
|
||||
) : (
|
||||
sessions.map((session) => (
|
||||
<div
|
||||
key={session.id}
|
||||
style={{
|
||||
padding: "10px",
|
||||
backgroundColor: "#fff",
|
||||
borderRadius: "4px",
|
||||
border: "1px solid var(--color-divider)",
|
||||
}}
|
||||
>
|
||||
<div style={{ fontSize: "13px", fontWeight: 500, color: "var(--color-text)" }}>
|
||||
{session.title || "Untitled Session"}
|
||||
</div>
|
||||
<div style={{ fontSize: "12px", color: "var(--color-text-2)", marginTop: "4px" }}>
|
||||
{formatDateTime(session.started_at)}
|
||||
{session.duration_seconds && ` · ${Math.floor(session.duration_seconds / 60)}m`}
|
||||
{session.segment_count && ` · ${session.segment_count} segments`}
|
||||
</div>
|
||||
{session.timetable_event_label && (
|
||||
<div style={{
|
||||
marginTop: "4px",
|
||||
padding: "2px 6px",
|
||||
backgroundColor: "var(--color-hover)",
|
||||
borderRadius: "3px",
|
||||
fontSize: "11px",
|
||||
color: "var(--color-text)",
|
||||
display: "inline-block",
|
||||
}}>
|
||||
📅 {session.timetable_event_label}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user