page.tsx 15 KB

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