page.tsx 15 KB

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