page.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. "use client";
  2. import { useCallback, useEffect, useState } from "react";
  3. import { useRouter } from "next/navigation";
  4. import { useAuth } from "@/features/agent/hooks/useAuth";
  5. import { ResponsiveLayout } from "@/components/layout";
  6. import {
  7. fetchFAQs,
  8. createFAQ,
  9. updateFAQ,
  10. deleteFAQ,
  11. type FAQSummary,
  12. type CreateFAQRequest,
  13. type UpdateFAQRequest,
  14. } from "@/features/agent/services/faqApi";
  15. import { Button } from "@/components/ui/button";
  16. import { Input } from "@/components/ui/input";
  17. import {
  18. Dialog,
  19. DialogContent,
  20. DialogHeader,
  21. DialogTitle,
  22. DialogDescription,
  23. } from "@/components/ui/dialog";
  24. import { Card } from "@/components/ui/card";
  25. import { Label } from "@/components/ui/label";
  26. import {
  27. Plus,
  28. Edit,
  29. Trash2,
  30. Search,
  31. FileText,
  32. Save,
  33. X,
  34. } from "lucide-react";
  35. import { Textarea } from "@/components/ui/textarea";
  36. export default function FAQsPage({ embedded = false }: { embedded?: boolean } = {}) {
  37. const router = useRouter();
  38. const { agent } = useAuth();
  39. const [faqs, setFaqs] = useState<FAQSummary[]>([]);
  40. const [loading, setLoading] = useState(true);
  41. const [searchQuery, setSearchQuery] = useState("");
  42. const [createDialogOpen, setCreateDialogOpen] = useState(false);
  43. const [editDialogOpen, setEditDialogOpen] = useState(false);
  44. const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
  45. const [selectedFAQ, setSelectedFAQ] = useState<FAQSummary | null>(null);
  46. const [submitting, setSubmitting] = useState(false);
  47. // 创建 FAQ 表单
  48. const [createForm, setCreateForm] = useState<CreateFAQRequest>({
  49. question: "",
  50. answer: "",
  51. keywords: "",
  52. });
  53. // 编辑 FAQ 表单
  54. const [editForm, setEditForm] = useState<UpdateFAQRequest>({
  55. question: "",
  56. answer: "",
  57. keywords: "",
  58. });
  59. // 加载 FAQ 列表
  60. const loadFAQs = useCallback(async () => {
  61. setLoading(true);
  62. try {
  63. // 如果搜索框有内容,使用关键词搜索;否则加载全部
  64. const query = searchQuery.trim() || undefined;
  65. const data = await fetchFAQs(query);
  66. setFaqs(data);
  67. } catch (error) {
  68. console.error("加载 FAQ 列表失败:", error);
  69. alert((error as Error).message || "加载 FAQ 列表失败");
  70. } finally {
  71. setLoading(false);
  72. }
  73. }, [searchQuery]);
  74. // 初始加载和搜索
  75. useEffect(() => {
  76. // 延迟搜索,避免频繁请求
  77. const timer = setTimeout(() => {
  78. loadFAQs();
  79. }, 500);
  80. return () => clearTimeout(timer);
  81. }, [loadFAQs]);
  82. // 打开创建对话框
  83. const handleOpenCreate = () => {
  84. setCreateForm({
  85. question: "",
  86. answer: "",
  87. keywords: "",
  88. });
  89. setCreateDialogOpen(true);
  90. };
  91. // 创建 FAQ
  92. const handleCreate = async () => {
  93. if (!createForm.question.trim() || !createForm.answer.trim()) {
  94. alert("问题和答案不能为空");
  95. return;
  96. }
  97. setSubmitting(true);
  98. try {
  99. await createFAQ(createForm);
  100. setCreateDialogOpen(false);
  101. setCreateForm({ question: "", answer: "", keywords: "" });
  102. await loadFAQs();
  103. alert("创建成功");
  104. } catch (error) {
  105. alert((error as Error).message || "创建 FAQ 失败");
  106. } finally {
  107. setSubmitting(false);
  108. }
  109. };
  110. // 打开编辑对话框
  111. const handleOpenEdit = (faq: FAQSummary) => {
  112. setSelectedFAQ(faq);
  113. setEditForm({
  114. question: faq.question,
  115. answer: faq.answer,
  116. keywords: faq.keywords || "",
  117. });
  118. setEditDialogOpen(true);
  119. };
  120. // 更新 FAQ
  121. const handleUpdate = async () => {
  122. if (!selectedFAQ) {
  123. return;
  124. }
  125. if (!editForm.question?.trim() || !editForm.answer?.trim()) {
  126. alert("问题和答案不能为空");
  127. return;
  128. }
  129. setSubmitting(true);
  130. try {
  131. await updateFAQ(selectedFAQ.id, editForm);
  132. setEditDialogOpen(false);
  133. setSelectedFAQ(null);
  134. await loadFAQs();
  135. alert("更新成功");
  136. } catch (error) {
  137. alert((error as Error).message || "更新 FAQ 失败");
  138. } finally {
  139. setSubmitting(false);
  140. }
  141. };
  142. // 打开删除对话框
  143. const handleOpenDelete = (faq: FAQSummary) => {
  144. setSelectedFAQ(faq);
  145. setDeleteDialogOpen(true);
  146. };
  147. // 删除 FAQ
  148. const handleDelete = async () => {
  149. if (!selectedFAQ) {
  150. return;
  151. }
  152. setSubmitting(true);
  153. try {
  154. await deleteFAQ(selectedFAQ.id);
  155. setDeleteDialogOpen(false);
  156. setSelectedFAQ(null);
  157. await loadFAQs();
  158. alert("删除成功");
  159. } catch (error) {
  160. alert((error as Error).message || "删除 FAQ 失败");
  161. } finally {
  162. setSubmitting(false);
  163. }
  164. };
  165. // 格式化时间
  166. const formatTime = (dateStr: string) => {
  167. const date = new Date(dateStr);
  168. return date.toLocaleString("zh-CN", {
  169. year: "numeric",
  170. month: "2-digit",
  171. day: "2-digit",
  172. hour: "2-digit",
  173. minute: "2-digit",
  174. });
  175. };
  176. // 构建头部内容
  177. const headerContent = (
  178. <div className="bg-card border-b p-4 shadow-sm">
  179. <div className="flex items-center justify-between mb-4">
  180. <h1 className="text-xl font-bold text-foreground">事件管理(FAQ)</h1>
  181. {!embedded && (
  182. <Button
  183. variant="ghost"
  184. size="sm"
  185. onClick={() => router.push("/agent/dashboard")}
  186. >
  187. 返回
  188. </Button>
  189. )}
  190. </div>
  191. {/* 搜索和操作栏 */}
  192. <div className="flex flex-col sm:flex-row items-stretch sm:items-center gap-2">
  193. <div className="flex-1 relative">
  194. <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 w-4 h-4 text-muted-foreground" />
  195. <Input
  196. type="text"
  197. placeholder="关键词搜索(用 % 分隔,例如:openai%api%调用)..."
  198. value={searchQuery}
  199. onChange={(e) => setSearchQuery(e.target.value)}
  200. className="pl-10"
  201. />
  202. </div>
  203. <Button
  204. onClick={handleOpenCreate}
  205. className="w-full sm:w-auto"
  206. >
  207. <Plus className="w-4 h-4 mr-2" />
  208. 创建事件
  209. </Button>
  210. </div>
  211. </div>
  212. );
  213. // 构建主内容区
  214. const mainContent = (
  215. <div className="flex-1 overflow-y-auto p-4 scrollbar-auto">
  216. {loading ? (
  217. <div className="flex items-center justify-center h-full">
  218. <span className="text-muted-foreground">加载中...</span>
  219. </div>
  220. ) : faqs.length === 0 ? (
  221. <div className="flex items-center justify-center h-full">
  222. <span className="text-muted-foreground">
  223. {searchQuery ? "没有找到匹配的事件" : "暂无事件"}
  224. </span>
  225. </div>
  226. ) : (
  227. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
  228. {faqs.map((faq) => (
  229. <Card key={faq.id} className="p-4 flex flex-col">
  230. <div className="flex-1 mb-3">
  231. <div className="flex items-start justify-between mb-2">
  232. <FileText className="w-5 h-5 text-blue-600 mt-0.5 mr-2 flex-shrink-0" />
  233. <h3 className="font-medium text-foreground flex-1 line-clamp-2">
  234. {faq.question}
  235. </h3>
  236. </div>
  237. <div className="text-sm text-muted-foreground mb-2 line-clamp-3">
  238. {faq.answer}
  239. </div>
  240. {faq.keywords && (
  241. <div className="text-xs text-muted-foreground mb-2">
  242. 关键词: {faq.keywords}
  243. </div>
  244. )}
  245. <div className="text-xs text-muted-foreground">
  246. 创建时间: {formatTime(faq.created_at)}
  247. </div>
  248. </div>
  249. <div className="flex items-center gap-2 mt-3 pt-3 border-t border-border">
  250. <Button
  251. variant="outline"
  252. size="sm"
  253. onClick={() => handleOpenEdit(faq)}
  254. className="flex-1"
  255. >
  256. <Edit className="w-4 h-4 mr-1" />
  257. 编辑
  258. </Button>
  259. <Button
  260. variant="destructive"
  261. size="sm"
  262. onClick={() => handleOpenDelete(faq)}
  263. >
  264. <Trash2 className="w-4 h-4" />
  265. </Button>
  266. </div>
  267. </Card>
  268. ))}
  269. </div>
  270. )}
  271. </div>
  272. );
  273. // 如果是嵌入模式,只返回内容,不包含 ResponsiveLayout
  274. if (embedded) {
  275. return (
  276. <>
  277. <div className="flex-1 flex flex-col min-h-0 overflow-hidden">
  278. {headerContent}
  279. {mainContent}
  280. </div>
  281. {/* 对话框 */}
  282. {/* 创建 FAQ 对话框 */}
  283. <Dialog open={createDialogOpen} onOpenChange={setCreateDialogOpen}>
  284. <DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">
  285. <DialogHeader>
  286. <DialogTitle>创建新事件</DialogTitle>
  287. <DialogDescription>
  288. 填写问题和答案,可以添加关键词以便搜索
  289. </DialogDescription>
  290. </DialogHeader>
  291. <div className="space-y-4">
  292. <div>
  293. <Label htmlFor="create-question">问题 *</Label>
  294. <Textarea
  295. id="create-question"
  296. value={createForm.question}
  297. onChange={(e) =>
  298. setCreateForm({ ...createForm, question: e.target.value })
  299. }
  300. placeholder="请输入问题"
  301. rows={2}
  302. className="resize-none"
  303. />
  304. </div>
  305. <div>
  306. <Label htmlFor="create-answer">答案 *</Label>
  307. <Textarea
  308. id="create-answer"
  309. value={createForm.answer}
  310. onChange={(e) =>
  311. setCreateForm({ ...createForm, answer: e.target.value })
  312. }
  313. placeholder="请输入答案"
  314. rows={6}
  315. className="resize-none"
  316. />
  317. </div>
  318. <div>
  319. <Label htmlFor="create-keywords">关键词(可选)</Label>
  320. <Input
  321. id="create-keywords"
  322. value={createForm.keywords}
  323. onChange={(e) =>
  324. setCreateForm({ ...createForm, keywords: e.target.value })
  325. }
  326. placeholder="例如:API、错误、配置(用逗号或空格分隔)"
  327. />
  328. <p className="text-xs text-muted-foreground mt-1">
  329. 提示:即使不填写关键词,系统也会自动搜索问题和答案中的内容。关键词字段用于添加额外的搜索索引,帮助用户更快找到相关内容。
  330. </p>
  331. </div>
  332. <div className="flex justify-end gap-2">
  333. <Button
  334. variant="outline"
  335. onClick={() => setCreateDialogOpen(false)}
  336. disabled={submitting}
  337. >
  338. 取消
  339. </Button>
  340. <Button onClick={handleCreate} disabled={submitting}>
  341. {submitting ? "创建中..." : "创建"}
  342. </Button>
  343. </div>
  344. </div>
  345. </DialogContent>
  346. </Dialog>
  347. {/* 编辑 FAQ 对话框 */}
  348. <Dialog open={editDialogOpen} onOpenChange={setEditDialogOpen}>
  349. <DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">
  350. <DialogHeader>
  351. <DialogTitle>编辑事件</DialogTitle>
  352. <DialogDescription>
  353. 修改问题和答案,可以更新关键词以便搜索
  354. </DialogDescription>
  355. </DialogHeader>
  356. {selectedFAQ && (
  357. <div className="space-y-4">
  358. <div>
  359. <Label htmlFor="edit-question">问题 *</Label>
  360. <Textarea
  361. id="edit-question"
  362. value={editForm.question || ""}
  363. onChange={(e) =>
  364. setEditForm({ ...editForm, question: e.target.value })
  365. }
  366. placeholder="请输入问题"
  367. rows={2}
  368. className="resize-none"
  369. />
  370. </div>
  371. <div>
  372. <Label htmlFor="edit-answer">答案 *</Label>
  373. <Textarea
  374. id="edit-answer"
  375. value={editForm.answer || ""}
  376. onChange={(e) =>
  377. setEditForm({ ...editForm, answer: e.target.value })
  378. }
  379. placeholder="请输入答案"
  380. rows={6}
  381. className="resize-none"
  382. />
  383. </div>
  384. <div>
  385. <Label htmlFor="edit-keywords">关键词(可选)</Label>
  386. <Input
  387. id="edit-keywords"
  388. value={editForm.keywords || ""}
  389. onChange={(e) =>
  390. setEditForm({ ...editForm, keywords: e.target.value })
  391. }
  392. placeholder="例如:API、错误、配置(用逗号或空格分隔)"
  393. />
  394. <p className="text-xs text-muted-foreground mt-1">
  395. 提示:即使不填写关键词,系统也会自动搜索问题和答案中的内容。关键词字段用于添加额外的搜索索引,帮助用户更快找到相关内容。
  396. </p>
  397. </div>
  398. <div className="flex justify-end gap-2">
  399. <Button
  400. variant="outline"
  401. onClick={() => setEditDialogOpen(false)}
  402. disabled={submitting}
  403. >
  404. 取消
  405. </Button>
  406. <Button onClick={handleUpdate} disabled={submitting}>
  407. {submitting ? "更新中..." : "更新"}
  408. </Button>
  409. </div>
  410. </div>
  411. )}
  412. </DialogContent>
  413. </Dialog>
  414. {/* 删除确认对话框 */}
  415. <Dialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
  416. <DialogContent>
  417. <DialogHeader>
  418. <DialogTitle>删除事件</DialogTitle>
  419. </DialogHeader>
  420. {selectedFAQ && (
  421. <div className="space-y-4">
  422. <p className="text-foreground">
  423. 确定要删除事件 <strong>&quot;{selectedFAQ.question}&quot;</strong> 吗?
  424. </p>
  425. <p className="text-sm text-muted-foreground">
  426. 此操作不可恢复,请谨慎操作。
  427. </p>
  428. <div className="flex justify-end gap-2">
  429. <Button
  430. variant="outline"
  431. onClick={() => setDeleteDialogOpen(false)}
  432. disabled={submitting}
  433. >
  434. 取消
  435. </Button>
  436. <Button
  437. variant="destructive"
  438. onClick={handleDelete}
  439. disabled={submitting}
  440. >
  441. {submitting ? "删除中..." : "删除"}
  442. </Button>
  443. </div>
  444. </div>
  445. )}
  446. </DialogContent>
  447. </Dialog>
  448. </>
  449. );
  450. }
  451. return (
  452. <ResponsiveLayout
  453. main={mainContent}
  454. header={headerContent}
  455. />
  456. );
  457. }