Skip to content
Clone
main
83 lines 2.7 KB
/** 代码行级评论领域:浏览页与提交页共用的行级评论读写。 */
import { unwrapApi } from '@app/lib'
import type { CreateCodeCommentCmd, EnvelopeCodeCommentDto } from '@app/services/api'
import Apis from '@app/services/api'
export type CodeComment = NonNullable<EnvelopeCodeCommentDto['data']>
/** 分页响应在生成器里是泛型 `Page`,行类型由此显式声明。 */
export interface CodeCommentPageData {
items: CodeComment[]
total: number
limit: number
offset: number
}
export const listCodeComments = (
space: string,
repo: string,
params: { path?: string; revision?: string; resolved?: boolean; limit?: number; offset?: number } = {},
) =>
Apis.codeComments
.list_code_comments({
pathParams: { space_name: space, repo_name: repo },
params: {
path: params.path,
revision: params.revision,
resolved: params.resolved,
limit: params.limit,
offset: params.offset,
},
})
.then((res) => res as unknown as { data?: CodeCommentPageData })
.then((res) => res.data ?? { items: [], total: 0, limit: 0, offset: 0 })
export const createCodeComment = (space: string, repo: string, cmd: CreateCodeCommentCmd) =>
unwrapApi<CodeComment>(
Apis.codeComments.create_code_comment({
pathParams: { space_name: space, repo_name: repo },
data: cmd,
}),
)
export const updateCodeComment = (space: string, repo: string, commentId: string, body: string) =>
unwrapApi<CodeComment>(
Apis.codeComments.update_code_comment({
pathParams: { space_name: space, repo_name: repo, comment_id: commentId },
data: { body },
}),
)
export const deleteCodeComment = (space: string, repo: string, commentId: string) =>
unwrapApi<unknown>(
Apis.codeComments.delete_code_comment({
pathParams: { space_name: space, repo_name: repo, comment_id: commentId },
}),
)
export const resolveCodeComment = (space: string, repo: string, commentId: string) =>
unwrapApi<CodeComment>(
Apis.codeComments.resolve_code_comment({
pathParams: { space_name: space, repo_name: repo, comment_id: commentId },
}),
)
export const unresolveCodeComment = (space: string, repo: string, commentId: string) =>
unwrapApi<CodeComment>(
Apis.codeComments.unresolve_code_comment({
pathParams: { space_name: space, repo_name: repo, comment_id: commentId },
}),
)
/** 把评论按 `path#line` 归组,供 diff / 代码视图按行渲染。 */
export function groupCommentsByLine(rows: CodeComment[]): Map<string, CodeComment[]> {
const map = new Map<string, CodeComment[]>()
for (const row of rows) {
const key = `${row.path}#${row.line}`
const list = map.get(key) ?? []
list.push(row)
map.set(key, list)
}
return map
}