Fix transcripts: use API route for server-side fs
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import fs from "fs";
|
||||
|
||||
const AVAILABLE_CATEGORIES = [
|
||||
"SiteMente",
|
||||
@@ -15,17 +14,6 @@ const AVAILABLE_CATEGORIES = [
|
||||
"Personal Growth"
|
||||
];
|
||||
|
||||
const STORAGE_FILE = "/root/.openclaw/workspace/SiteMente/data/transcripts.json";
|
||||
|
||||
function getTranscripts() {
|
||||
try {
|
||||
if (fs.existsSync(STORAGE_FILE)) {
|
||||
return JSON.parse(fs.readFileSync(STORAGE_FILE, "utf-8"));
|
||||
}
|
||||
} catch (e) {}
|
||||
return [];
|
||||
}
|
||||
|
||||
export default function TranscriptsPage() {
|
||||
const [transcripts, setTranscripts] = useState<any[]>([]);
|
||||
const [videoUrl, setVideoUrl] = useState("");
|
||||
@@ -36,9 +24,15 @@ export default function TranscriptsPage() {
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setTranscripts(getTranscripts());
|
||||
fetchTranscripts();
|
||||
}, []);
|
||||
|
||||
const fetchTranscripts = async () => {
|
||||
const res = await fetch("/api/transcripts");
|
||||
const data = await res.json();
|
||||
setTranscripts(data);
|
||||
};
|
||||
|
||||
const toggleCategory = (cat: string) => {
|
||||
setSelectedCategories(prev =>
|
||||
prev.includes(cat)
|
||||
@@ -51,35 +45,42 @@ export default function TranscriptsPage() {
|
||||
if (!videoUrl || !transcriptText) return;
|
||||
setSaving(true);
|
||||
|
||||
// Extract video ID
|
||||
const match = videoUrl.match(/(?:v=|\/)([0-9A-Za-z_-]{11})/);
|
||||
const videoId = match ? match[1] : "";
|
||||
const res = await fetch("/api/transcripts", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
videoUrl,
|
||||
title: title || videoUrl,
|
||||
transcript: transcriptText,
|
||||
categories: selectedCategories
|
||||
})
|
||||
});
|
||||
|
||||
const data = {
|
||||
videoId,
|
||||
videoUrl,
|
||||
title: title || `Video ${videoId}`,
|
||||
transcript: transcriptText,
|
||||
categories: selectedCategories,
|
||||
savedAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
// Save directly
|
||||
const current = getTranscripts();
|
||||
const existing = current.findIndex(t => t.videoId === videoId);
|
||||
if (existing >= 0) {
|
||||
current[existing] = data;
|
||||
} else {
|
||||
current.unshift(data);
|
||||
}
|
||||
|
||||
fs.writeFileSync(STORAGE_FILE, JSON.stringify(current, null, 2));
|
||||
setTranscripts(current);
|
||||
const data = await res.json();
|
||||
setSaving(false);
|
||||
setVideoUrl("");
|
||||
setTranscriptText("");
|
||||
setTitle("");
|
||||
setSelectedCategories([]);
|
||||
|
||||
if (data.transcripts) {
|
||||
setTranscripts(data.transcripts);
|
||||
setVideoUrl("");
|
||||
setTranscriptText("");
|
||||
setTitle("");
|
||||
setSelectedCategories([]);
|
||||
}
|
||||
};
|
||||
|
||||
const deleteTranscript = async (videoId: string) => {
|
||||
if (!confirm("Delete this transcript?")) return;
|
||||
|
||||
const res = await fetch("/api/transcripts", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ action: "delete", videoId })
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
if (data.transcripts) {
|
||||
setTranscripts(data.transcripts);
|
||||
}
|
||||
};
|
||||
|
||||
const filteredTranscripts = filterCategory === "all"
|
||||
@@ -191,13 +192,7 @@ export default function TranscriptsPage() {
|
||||
{new Date(t.savedAt).toLocaleDateString()}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => {
|
||||
if (confirm("Delete this transcript?")) {
|
||||
const updated = transcripts.filter(x => x.videoId !== t.videoId);
|
||||
fs.writeFileSync(STORAGE_FILE, JSON.stringify(updated, null, 2));
|
||||
setTranscripts(updated);
|
||||
}
|
||||
}}
|
||||
onClick={() => deleteTranscript(t.videoId)}
|
||||
className="text-red-400 hover:text-red-300"
|
||||
>
|
||||
✕
|
||||
|
||||
Reference in New Issue
Block a user