Spaces:
Running on Zero

khmersense / frontend /src /components /InputPanel.tsx
vechey's picture
Deploy KhmerSense
3a260a9 verified
Raw History Blame Contribute Delete
7.73 kB
import { useRef, useState } from "react";
import { SAMPLE_COMMENTS } from "../api";
import type { Health } from "../types";
type Tab = "youtube" | "paste" | "file" | "facebook";
export type Submit =
| { kind: "youtube"; url: string; limit: number; order: "relevance" | "time" }
| { kind: "paste"; texts: string[] }
| { kind: "file"; file: File };
const TABS: { id: Tab; label: string }[] = [
{ id: "youtube", label: "YouTube link" },
{ id: "paste", label: "Paste text" },
{ id: "file", label: "CSV / Excel" },
{ id: "facebook", label: "Facebook Page" },
];
export function InputPanel({ health, busy, onSubmit }: { health: Health | null; busy: boolean; onSubmit: (s: Submit) => void }) {
const [tab, setTab] = useState<Tab>("youtube");
const [url, setUrl] = useState("");
const [limit, setLimit] = useState(500);
const [order, setOrder] = useState<"relevance" | "time">("relevance");
const [text, setText] = useState("");
const [file, setFile] = useState<File | null>(null);
const [drag, setDrag] = useState(false);
const fileRef = useRef<HTMLInputElement>(null);
const lines = text.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
const ytReady = health?.youtube_enabled !== false;
const canSubmit = !busy && ((tab === "youtube" && !!url.trim() && ytReady) || (tab === "paste" && lines.length > 0) || (tab === "file" && !!file));
const submit = (e: React.FormEvent) => {
e.preventDefault();
if (!canSubmit) return;
if (tab === "youtube") onSubmit({ kind: "youtube", url: url.trim(), limit, order });
else if (tab === "paste") onSubmit({ kind: "paste", texts: lines });
else if (file) onSubmit({ kind: "file", file });
};
return (
<form onSubmit={submit}>
<div role="tablist" aria-label="Comment source" className="flex gap-6 overflow-x-auto border-b border-[var(--line)]">
{TABS.map(({ id, label }) => (
<button key={id} type="button" role="tab" aria-selected={tab === id} onClick={() => setTab(id)}
className={`-mb-px whitespace-nowrap border-b-2 pb-2.5 text-[14px] transition-colors ${tab === id ? "border-[var(--ink)] text-[var(--ink)]" : "border-transparent text-[var(--muted)] hover:text-[var(--ink)]"}`}>
{label}
</button>
))}
</div>
<div className="pt-6">
{tab === "youtube" && (
<div>
<label htmlFor="yt" className="mb-2 block text-[14px] text-[var(--ink-2)]">Video URL</label>
<div className="flex flex-col gap-2 sm:flex-row">
<input id="yt" value={url} onChange={(e) => setUrl(e.target.value)} inputMode="url" autoComplete="off"
placeholder="https://www.youtube.com/watch?v=…" className="field h-11 px-3" />
<button type="submit" className="btn" disabled={!canSubmit}>{busy ? "Analysing…" : "Analyse"}</button>
</div>
<div className="mt-3 flex flex-wrap gap-x-6 gap-y-2 text-[13px] text-[var(--muted)]">
<label className="flex items-center gap-2">
Comments
<select value={limit} onChange={(e) => setLimit(Number(e.target.value))} className="field h-8 w-auto px-2 text-[var(--ink)]">
{[100, 500, 1000, 2000, 5000].filter((n) => n <= (health?.max_comments ?? 5000)).map((n) => <option key={n} value={n}>{n === (health?.max_comments ?? 5000) ? `Up to ${n.toLocaleString()}` : n.toLocaleString()}</option>)}
</select>
</label>
<span className="self-center">≈ {Math.max(5, Math.round(limit / 100 + limit / 250))} s</span>
<label className="flex items-center gap-2">
Order
<select value={order} onChange={(e) => setOrder(e.target.value as "relevance" | "time")} className="field h-8 w-auto px-2 text-[var(--ink)]">
<option value="relevance">Top</option>
<option value="time">Newest</option>
</select>
</label>
</div>
{!ytReady && (
<p className="mt-5 border-l-2 border-[var(--line-strong)] pl-3 text-[13px] leading-relaxed text-[var(--ink-2)]">
YouTube is not configured on this server. Add <code className="font-mono text-[12px]">YOUTUBE_API_KEY</code> to <code className="font-mono text-[12px]">backend/.env</code> and restart.
You can still <button type="button" className="underline underline-offset-2" onClick={() => { setTab("paste"); setText(SAMPLE_COMMENTS.join("\n")); }}>try sample comments</button>.
</p>
)}
</div>
)}
{tab === "paste" && (
<div>
<div className="mb-2 flex items-baseline justify-between">
<label htmlFor="paste" className="text-[14px] text-[var(--ink-2)]">Comments, one per line</label>
<button type="button" onClick={() => setText(SAMPLE_COMMENTS.join("\n"))} className="text-[13px] text-[var(--muted)] underline underline-offset-2 hover:text-[var(--ink)]">Use sample</button>
</div>
<textarea id="paste" value={text} onChange={(e) => setText(e.target.value)} rows={8} lang="km"
className="field resize-y p-3 text-[15px]" />
<div className="mt-3 flex items-center justify-between">
<span className="font-mono text-[12px] text-[var(--muted)]">{lines.length} lines</span>
<button type="submit" className="btn" disabled={!canSubmit}>{busy ? "Analysing…" : "Analyse"}</button>
</div>
</div>
)}
{tab === "file" && (
<div>
<input ref={fileRef} type="file" accept=".csv,.xlsx,.xls,.txt" className="sr-only" id="file" onChange={(e) => setFile(e.target.files?.[0] ?? null)} />
<label htmlFor="file"
onDragOver={(e) => { e.preventDefault(); setDrag(true); }} onDragLeave={() => setDrag(false)}
onDrop={(e) => { e.preventDefault(); setDrag(false); setFile(e.dataTransfer.files?.[0] ?? null); }}
className={`block cursor-pointer rounded-[4px] border border-dashed px-5 py-8 text-[14px] transition-colors ${drag ? "border-[var(--ink)] bg-[var(--sunk)]" : "border-[var(--line-strong)] hover:bg-[var(--sunk)]"}`}>
{file ? <span className="text-[var(--ink)]">{file.name}</span> : <span className="text-[var(--ink-2)]">Drop a file here or <span className="underline underline-offset-2">choose one</span></span>}
<span className="mt-1 block text-[13px] text-[var(--muted)]">.csv or .xlsx, up to 5 MB. The column named “comment” or “text” is used.</span>
</label>
<div className="mt-3 flex items-center justify-between">
{file ? <button type="button" className="text-[13px] text-[var(--muted)] underline underline-offset-2" onClick={() => { setFile(null); if (fileRef.current) fileRef.current.value = ""; }}>Remove file</button> : <span />}
<button type="submit" className="btn" disabled={!canSubmit}>{busy ? "Analysing…" : "Analyse"}</button>
</div>
</div>
)}
{tab === "facebook" && (
<div className="max-w-xl text-[14px] leading-relaxed text-[var(--ink-2)]">
<p>Connect a Facebook Page you manage to analyse comments on its posts, through Meta's Graph API.</p>
<p className="mt-2 text-[var(--muted)]">Facebook only allows this for Pages you manage, not for other people's posts. Not available yet.</p>
</div>
)}
</div>
</form>
);
}