-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathReviewResultsSection.swift
More file actions
183 lines (154 loc) · 5.12 KB
/
ReviewResultsSection.swift
File metadata and controls
183 lines (154 loc) · 5.12 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
import SwiftUI
import ComposableArchitecture
import ConversationServiceProvider
import SharedUIComponents
// MARK: - Review Results Section
struct ReviewResultsSection: View {
let store: StoreOf<Chat>
let round: CodeReviewRound
@State private var isExpanded = false
@AppStorage(\.chatFontSize) private var chatFontSize
private static let defaultVisibleReviewCount = 5
private var fileComments: [CodeReviewResponse.FileComment] {
round.response?.fileComments ?? []
}
private var visibleReviewCount: Int {
isExpanded ? fileComments.count : min(fileComments.count, Self.defaultVisibleReviewCount)
}
private var hasMoreReviews: Bool {
fileComments.count > Self.defaultVisibleReviewCount
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
ReviewResultsHeader(
reviewStatus: round.status,
chatFontSize: chatFontSize
)
.padding(8)
.background(CodeReviewHeaderBackground())
if !fileComments.isEmpty {
VStack(alignment: .leading, spacing: 4) {
ReviewResultsList(
store: store,
fileComments: Array(fileComments.prefix(visibleReviewCount))
)
}
.padding(.horizontal, 8)
.padding(.bottom, !hasMoreReviews || isExpanded ? 8 : 0)
}
if hasMoreReviews && !isExpanded {
ExpandReviewsButton(isExpanded: $isExpanded)
}
}
.background(CodeReviewCardBackground())
}
}
private struct ReviewResultsHeader: View {
let reviewStatus: CodeReviewRound.Status
let chatFontSize: CGFloat
var body: some View {
HStack(spacing: 4) {
Text("Reviewed Changes")
.scaledFont(size: chatFontSize)
Spacer()
}
}
}
private struct ExpandReviewsButton: View {
@Binding var isExpanded: Bool
var body: some View {
HStack {
Spacer()
Button {
isExpanded = true
} label: {
Image("chevron.down")
.resizable()
.scaledToFit()
.scaledFrame(width: 16, height: 16)
}
.buttonStyle(PlainButtonStyle())
Spacer()
}
.padding(.vertical, 2)
.background(CodeReviewHeaderBackground())
}
}
private struct ReviewResultsList: View {
let store: StoreOf<Chat>
let fileComments: [CodeReviewResponse.FileComment]
var body: some View {
ForEach(fileComments, id: \.self) { fileComment in
if let fileURL = fileComment.url {
ReviewResultRow(
store: store,
fileURL: fileURL,
comments: fileComment.comments
)
}
}
}
}
private struct ReviewResultRow: View {
let store: StoreOf<Chat>
let fileURL: URL
let comments: [ReviewComment]
@State private var isExpanded = false
private var commentCountText: String {
comments.count == 1 ? "1 comment" : "\(comments.count) comments"
}
private var hasComments: Bool {
!comments.isEmpty
}
var body: some View {
VStack(alignment: .leading) {
ReviewResultRowContent(
store: store,
fileURL: fileURL,
comments: comments,
commentCountText: commentCountText,
hasComments: hasComments
)
}
}
}
private struct ReviewResultRowContent: View {
let store: StoreOf<Chat>
let fileURL: URL
let comments: [ReviewComment]
let commentCountText: String
let hasComments: Bool
@State private var isHovered: Bool = false
@AppStorage(\.chatFontSize) private var chatFontSize
var body: some View {
HStack(alignment: .center, spacing: 4) {
drawFileIcon(fileURL)
.scaledToFit()
.scaledFrame(width: 16, height: 16)
Button(action: {
if hasComments {
store.send(.codeReview(.onFileClicked(fileURL, comments[0].range.end.line)))
}
}) {
Text(fileURL.lastPathComponent)
.scaledFont(.system(size: chatFontSize))
.foregroundColor(isHovered ? Color("ItemSelectedColor") : .primary)
}
.buttonStyle(PlainButtonStyle())
.disabled(!hasComments)
.onHover { hovering in
isHovered = hovering
if hovering {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
Text(commentCountText)
.scaledFont(size: chatFontSize - 1)
.lineSpacing(20)
.foregroundColor(.secondary)
Spacer()
}
}
}