page.tsx 15 KB

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