-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaudit_tab.py
More file actions
196 lines (157 loc) · 5.45 KB
/
audit_tab.py
File metadata and controls
196 lines (157 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""审核标签页 — 显示待审核的文件快照."""
from __future__ import annotations
from collections import defaultdict
from typing import ClassVar
from rich.markup import escape
from textual.containers import Horizontal, Vertical
from textual.widgets import Button, Collapsible, Label, Static
class AuditTab(Vertical):
"""审核标签页.
显示所有 PENDING_AUDIT 的文件快照,
每个文件作为一个可折叠块,含 diff 内容和批准/拒绝按钮.
"""
DEFAULT_CSS: ClassVar[str] = """
AuditTab {
height: 1fr;
width: 1fr;
padding: 0 1;
overflow-y: auto;
}
#audit-placeholder {
height: 100%;
content-align: center middle;
color: $text-muted;
}
#audit-empty {
height: 100%;
content-align: center middle;
color: $text-muted;
}
#audit-header {
height: auto;
padding: 1 0;
text-style: bold;
color: $text;
}
.audit-collapsible {
height: auto;
margin-bottom: 1;
}
.audit-diff {
height: auto;
max-height: 15;
overflow-y: auto;
padding: 1;
background: $surface;
border: solid $primary;
margin-bottom: 1;
}
.audit-buttons {
height: auto;
align: left middle;
margin-bottom: 1;
}
.audit-approve {
margin-right: 1;
}
.audit-reject {
margin-right: 1;
}
#audit-result-log {
height: auto;
max-height: 6;
overflow-y: auto;
border: solid $accent;
padding: 0 1;
margin-bottom: 1;
}
"""
def __init__(self) -> None:
super().__init__()
self._committer = None
def compose(self):
yield Label("正在加载审核列表...", id="audit-placeholder")
def set_committer(self, committer) -> None:
"""设置审核提交模块并刷新列表."""
self._committer = committer
self.set_timer(0.0, self._refresh)
def on_mount(self) -> None:
"""控件挂载后刷新."""
if self._committer is not None:
# 延迟一下让 UI 就绪
self.set_timer(0.1, self._refresh)
async def _refresh(self) -> None:
"""查询待审核列表并重建 UI."""
if self._committer is None:
return
await self.remove_children()
pending = self._committer.workspace.db.get_snapshots_by_audit_status("PENDING_AUDIT")
if not pending:
await self.mount(Label("没有待审核的更改.", id="audit-empty"))
return
# Group by file_path
grouped: defaultdict[str, list[tuple]] = defaultdict(list)
for snap in pending:
grouped[snap[1]].append(snap)
# Result area for showing commit results
result_log = Vertical(id="audit-result-log")
await self.mount(result_log)
header = Label(
f"待审核更改 ({sum(len(snaps) for snaps in grouped.values())} 项)",
id="audit-header",
)
await self.mount(header)
for file_path in sorted(grouped):
snaps = grouped[file_path]
# Collect all children for all snaps of this file
all_snap_widgets: list[Static | Horizontal] = []
for snap in snaps:
snap_id = snap[0]
diff_content = snap[4] or "(空 diff)"
diff_container = Vertical(Static(diff_content, markup=False), classes="audit-diff")
btn_row = Horizontal(
Button("批准", variant="primary", id=f"approve-{snap_id}", classes="audit-approve"),
Button("拒绝", variant="error", id=f"reject-{snap_id}", classes="audit-reject"),
classes="audit-buttons",
)
all_snap_widgets.append(diff_container)
all_snap_widgets.append(btn_row)
content_widgets = Vertical(*all_snap_widgets)
collapsible = Collapsible(
content_widgets,
title=f"{file_path} ({len(snaps)} 次更改)",
classes="audit-collapsible",
)
# Collapse excess items initially — keep first 3 expanded
if len(self.children) > 6: # header + result_log + 3 expanded
collapsible.collapsed = True
await self.mount(collapsible)
async def on_button_pressed(self, event: Button.Pressed) -> None:
"""处理批准/拒绝按钮点击."""
if self._committer is None:
return
button_id = event.button.id or ""
parts = button_id.split("-", 1)
if len(parts) != 2:
return
action, snap_id_str = parts
try:
snapshot_id = int(snap_id_str)
except ValueError:
return
if action == "approve":
result = self._committer.commit(snapshot_id, approved=True)
elif action == "reject":
result = self._committer.commit(snapshot_id, approved=False)
else:
return
# Show result in the result log
try:
log = self.query_one("#audit-result-log", Vertical)
color = "green" if "已批准" in result or "已拒绝" in result else "red"
escaped = escape(result)
await log.mount(Static(f"[{color}]{escaped}[/{color}]"))
except Exception:
pass
# Refresh the list
await self._refresh()