sentence-pr.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { get, post, put, delete_ } from "../request";
  2. import type { ISentence } from "./sentence";
  3. import type { IChannel } from "./channel";
  4. import type { IUser } from "./Auth";
  5. // ─── 原有类型定义,保持不动 ─────────────────────────────────────────────────────
  6. export interface ISentencePrRequest {
  7. book?: number;
  8. para?: number;
  9. begin?: number;
  10. end?: number;
  11. channel?: string;
  12. text: string;
  13. }
  14. export interface ISentencePrResponseData {
  15. book_id: number;
  16. paragraph: number;
  17. word_start: number;
  18. word_end: number;
  19. channel_uid: string;
  20. }
  21. export interface ISentencePrResponse {
  22. ok: boolean;
  23. message: string;
  24. data: {
  25. new: ISentencePrResponseData;
  26. count: number;
  27. webhook: { message: string; ok: boolean };
  28. };
  29. }
  30. // ─── 新增:PR 列表查询的响应类型 ────────────────────────────────────────────────
  31. export interface ISentencePrData {
  32. id: string;
  33. uid: string;
  34. book: number;
  35. paragraph: number;
  36. word_start: number;
  37. word_end: number;
  38. content: string;
  39. html: string;
  40. editor: IUser;
  41. channel: IChannel;
  42. updated_at: string;
  43. }
  44. export interface ISentencePrListResponse {
  45. ok: boolean;
  46. message: string;
  47. data: { rows: ISentencePrData[]; count: number };
  48. }
  49. export interface IDeleteResponse {
  50. ok: boolean;
  51. message: string;
  52. }
  53. // ─── 新增:纯 HTTP 函数,供 hooks 层调用 ────────────────────────────────────────
  54. /**
  55. * 查询某句下的 PR 列表
  56. */
  57. export async function fetchSentencePrList(
  58. book: number,
  59. para: number,
  60. wordStart: number,
  61. wordEnd: number,
  62. channelId: string
  63. ): Promise<ISentencePrData[]> {
  64. const url = `/v2/sentpr?view=sent-info&book=${book}&para=${para}&start=${wordStart}&end=${wordEnd}&channel=${channelId}`;
  65. const json = await get<ISentencePrListResponse>(url);
  66. if (!json.ok) {
  67. throw new Error(json.message ?? "PR 列表加载失败");
  68. }
  69. return json.data.rows;
  70. }
  71. /**
  72. * 创建 PR(提交修改建议)
  73. */
  74. export async function createSentencePr(
  75. sentence: ISentence,
  76. content: string
  77. ): Promise<void> {
  78. const json = await post<ISentencePrRequest, ISentencePrResponse>(
  79. `/v2/sentpr`,
  80. {
  81. book: sentence.book,
  82. para: sentence.para,
  83. begin: sentence.wordStart,
  84. end: sentence.wordEnd,
  85. channel: sentence.channel.id,
  86. text: content,
  87. }
  88. );
  89. if (!json.ok) {
  90. throw new Error(json.message ?? "提交建议失败");
  91. }
  92. }
  93. /**
  94. * 更新 PR 内容
  95. */
  96. export async function updateSentencePr(
  97. prId: string,
  98. content: string
  99. ): Promise<void> {
  100. const json = await put<Pick<ISentencePrRequest, "text">, ISentencePrResponse>(
  101. `/v2/sentpr/${prId}`,
  102. { text: content }
  103. );
  104. if (!json.ok) {
  105. throw new Error(json.message ?? "更新建议失败");
  106. }
  107. }
  108. /**
  109. * 删除 PR
  110. */
  111. export async function deleteSentencePr(prId: string): Promise<void> {
  112. const json = await delete_<IDeleteResponse>(`/v2/sentpr/${prId}`);
  113. if (!json.ok) {
  114. throw new Error(json.message ?? "删除失败");
  115. }
  116. }