86 lines
4.9 KiB
React
86 lines
4.9 KiB
React
import { useEffect, useRef, useState } from "react";
|
|
import { Button, Card, Code, Group, ScrollArea, Stack, Table, Text, TextInput, Title, useComputedColorScheme } from "@mantine/core";
|
|
import { IconSearch } from "@tabler/icons-react";
|
|
import { call, Page } from "../common.jsx";
|
|
|
|
const when = t => (t ? t.slice(0, 16).replace("T", " ") : "");
|
|
|
|
// The LibreDesk conversations, pumped read-only by ~/aimsight-libredesk into labeling/auto/conversations/: a pumping (a range
|
|
// of updates, or without dates what changed since the last one), its log read every 2 s; the pumped conversations, searched
|
|
// on subject, contact and reference; a click shows the transcript (images inline, the client's text never run)
|
|
export default function Conversations() {
|
|
const [range, setRange] = useState({ start: "", end: "" }), [state, setState] = useState(null);
|
|
const [list, setList] = useState([]), [search, setSearch] = useState(""), [open, setOpen] = useState(null);
|
|
const dark = useComputedColorScheme("light") === "dark", wasRunning = useRef(false);
|
|
const read = () => call("/api/conversations").then(l => l && setList(l));
|
|
useEffect(() => { read(); }, []);
|
|
useEffect(() => {
|
|
const poll = () => fetch("/api/conversations/pump").then(r => r.ok && r.json()).then(p => {
|
|
if (!p) return;
|
|
if (wasRunning.current && !p.running) read(); // a pumping ended: the list again
|
|
wasRunning.current = p.running;
|
|
setState(p);
|
|
}).catch(() => {}); // silently: every 2 s
|
|
poll();
|
|
const timer = setInterval(poll, 2000);
|
|
return () => clearInterval(timer);
|
|
}, []);
|
|
const pump = () => call("/api/conversations/pump", "POST", range).then(p => { if (p) { wasRunning.current = p.running; setState(p); } });
|
|
const q = search.trim().toLowerCase();
|
|
const shown = list.filter(c => !q || [c.subject, c.contact, c.reference].some(x => x.toLowerCase().includes(q)));
|
|
return (
|
|
<Stack gap="lg" maw={1200}>
|
|
<div>
|
|
<Title order={2}>Conversations</Title>
|
|
<Text c="dimmed">The LibreDesk conversations, pumped read-only into labeling/auto/conversations/: the thread, its images and
|
|
attachments, for the support and as training data.</Text>
|
|
</div>
|
|
<Card shadow="sm" withBorder>
|
|
<Stack gap="md">
|
|
<Group align="flex-end" gap="md">
|
|
<TextInput label="Updated from (UTC)" type="date" value={range.start} onChange={e => setRange({ ...range, start: e.currentTarget.value })} />
|
|
<TextInput label="To (excluded)" type="date" value={range.end} onChange={e => setRange({ ...range, end: e.currentTarget.value })} />
|
|
<Button onClick={pump} loading={!!state?.running}>Pump</Button>
|
|
</Group>
|
|
<Text size="xs" c="dimmed">Without dates: what changed since the last pumping; the first time, the whole history (a quiet
|
|
hour). The key stays in ~/aimsight-libredesk/.env; nothing is ever written to LibreDesk.</Text>
|
|
{state?.log?.length > 0 && (
|
|
<ScrollArea h={200} viewportRef={v => v && (v.scrollTop = v.scrollHeight)}>
|
|
<Code block>{state.log.join("\n")}</Code>
|
|
</ScrollArea>
|
|
)}
|
|
</Stack>
|
|
</Card>
|
|
<Card shadow="sm" withBorder>
|
|
<Stack gap="md">
|
|
<TextInput placeholder="Search: subject, contact, reference" leftSection={<IconSearch size={16} />} value={search}
|
|
onChange={e => setSearch(e.currentTarget.value)} maw={400} />
|
|
{list.length ? (
|
|
<ScrollArea.Autosize mah={400}>
|
|
<Table highlightOnHover verticalSpacing={4} stickyHeader>
|
|
<Table.Thead><Table.Tr><Table.Th>Ref.</Table.Th><Table.Th>Subject</Table.Th><Table.Th>Contact</Table.Th>
|
|
<Table.Th>Status</Table.Th><Table.Th>Last message (UTC)</Table.Th><Table.Th>Messages</Table.Th><Table.Th>Attachments</Table.Th></Table.Tr></Table.Thead>
|
|
<Table.Tbody>
|
|
{shown.map(c => (
|
|
<Table.Tr key={c.reference} onClick={() => setOpen(c.reference)} style={{ cursor: "pointer" }}
|
|
bg={open === c.reference ? "var(--mantine-primary-color-light)" : undefined}>
|
|
<Table.Td ff="monospace">{c.reference}</Table.Td><Table.Td>{c.subject}</Table.Td><Table.Td>{c.contact}</Table.Td>
|
|
<Table.Td>{c.status}</Table.Td><Table.Td ff="monospace">{when(c.last)}</Table.Td>
|
|
<Table.Td>{c.messages}</Table.Td><Table.Td>{c.attachments}</Table.Td>
|
|
</Table.Tr>
|
|
))}
|
|
</Table.Tbody>
|
|
</Table>
|
|
</ScrollArea.Autosize>
|
|
) : <Text c="dimmed" size="sm">No conversation pumped yet.</Text>}
|
|
</Stack>
|
|
</Card>
|
|
{open && (
|
|
<Card shadow="sm" withBorder>
|
|
<Page key={`${open}-${dark}`} title="Transcript" src={`/api/conversations/${encodeURIComponent(open)}/report.html?dark=${dark}`} />
|
|
</Card>
|
|
)}
|
|
</Stack>
|
|
);
|
|
}
|