forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedbackView.swift
More file actions
225 lines (189 loc) · 7.22 KB
/
Copy pathFeedbackView.swift
File metadata and controls
225 lines (189 loc) · 7.22 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import OmiTheme
import Sentry
import SwiftUI
import UniformTypeIdentifiers
/// The Sentry event title used when a user submits feedback. Shared by the real
/// `submitFeedback()` path and the non-prod dry-run bridge action so the dry-run
/// can never drift from the title that actually ships to Sentry (SET-02).
func feedbackReportTitle(for _: String) -> String {
"User Report"
}
/// Filename of the JSON diagnostics attachment on the feedback Sentry event.
/// Shared so the dry-run reports the same attachment name the real submit uses.
let feedbackDiagnosticsAttachmentFilename = "desktop_diagnostics.json"
/// Window controller for the feedback dialog
@MainActor
class FeedbackWindow {
private static var window: NSWindow?
static func show(userEmail: String?) {
// Close existing window if any
window?.close()
// Track feedback opened
AnalyticsManager.shared.feedbackOpened()
let feedbackView = FeedbackView(userEmail: userEmail) {
window?.close()
window = nil
}
let hostingController = NSHostingController(rootView: feedbackView.withFontScaling())
let newWindow = NSWindow(contentViewController: hostingController)
newWindow.title = "Report Issue"
newWindow.styleMask = [.titled, .closable]
newWindow.setContentSize(NSSize(width: 400, height: 300))
newWindow.center()
newWindow.makeKeyAndOrderFront(nil)
newWindow.level = .floating
window = newWindow
NSApp.activate()
}
}
/// SwiftUI view for collecting user feedback and sending logs
struct FeedbackView: View {
let userEmail: String?
let onDismiss: () -> Void
@State private var feedbackText: String = ""
@State private var name: String = ""
@State private var email: String = ""
@State private var isSubmitting: Bool = false
@State private var showSuccess: Bool = false
init(userEmail: String?, onDismiss: @escaping () -> Void) {
self.userEmail = userEmail
self.onDismiss = onDismiss
// Pre-fill email from auth
_email = State(initialValue: userEmail ?? "")
// Pre-fill name from AuthService
_name = State(initialValue: AuthService.shared.displayName)
}
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.lg) {
if showSuccess {
// Success state
VStack(spacing: OmiSpacing.md) {
Image(systemName: "checkmark.circle.fill")
.scaledFont(size: 48)
.foregroundColor(.green)
Text("Report sent")
.font(.headline)
Text("We'll look into this issue.")
.foregroundColor(.secondary)
Button("Close") {
onDismiss()
}
.keyboardShortcut(.defaultAction)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
// Form state
Text("Report an Issue")
.font(.headline)
Text(
"Redacted diagnostics will be included automatically. Notes, name, and email stay on this device for privacy; save a diagnostics file to share them manually."
)
.font(.caption)
.foregroundColor(.secondary)
TextEditor(text: $feedbackText)
.font(.body)
.frame(minHeight: 100)
.border(Color.gray.opacity(0.3), width: 1)
HStack {
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text("Name (optional)")
.font(.caption)
.foregroundColor(.secondary)
TextField("Your name", text: $name)
.textFieldStyle(.roundedBorder)
}
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text("Email")
.font(.caption)
.foregroundColor(.secondary)
TextField("your@email.com", text: $email)
.textFieldStyle(.roundedBorder)
}
}
HStack {
Button("Cancel") {
onDismiss()
}
.keyboardShortcut(.cancelAction)
Button("Save Diagnostics…") {
saveDiagnosticsLocally()
}
.help("Save a redacted diagnostics report locally — works offline, nothing is uploaded.")
Spacer()
Button("Send Report") {
submitFeedback()
}
.keyboardShortcut(.defaultAction)
.disabled(isSubmitting)
}
}
}
.padding(OmiSpacing.xl)
.frame(width: 400, height: 300)
}
private func submitFeedback() {
isSubmitting = true
let message = feedbackText.trimmingCharacters(in: .whitespacesAndNewlines)
// Track feedback submitted
AnalyticsManager.shared.feedbackSubmitted(feedbackLength: message.count)
// Submit to Sentry with log file attachment (dev + prod — user explicitly chose to report)
let sentryMessage = feedbackReportTitle(for: message)
// Attach bounded, redacted diagnostics rather than the raw local log or
// user-entered feedback text.
let diagnosticsURL = DesktopDiagnosticsManager.shared.writeIncidentDiagnosticsAttachment(
area: "other",
failureClass: "user_report",
phase: "other")
SentrySDK.capture(message: sentryMessage) { scope in
if let diagnosticsURL {
let attachment = Attachment(
path: diagnosticsURL.path,
filename: feedbackDiagnosticsAttachmentFilename,
contentType: "application/json")
scope.addAttachment(attachment)
}
}
if let diagnosticsURL {
try? FileManager.default.removeItem(at: diagnosticsURL)
}
// The user-entered text, name, and email are intentionally not sent to Sentry.
// The report's diagnostic attachment is the privacy-safe cloud evidence path.
log("User report diagnostics submitted to Sentry")
// Show success
OmiMotion.withGated {
showSuccess = true
isSubmitting = false
}
}
/// Save a redacted diagnostics bundle to a user-chosen location and reveal it
/// in Finder. Fully offline — no Sentry, no network — so users on named/dev
/// bundles or without connectivity can still capture a report (BL-023 / SET-03).
private func saveDiagnosticsLocally() {
let panel = NSSavePanel()
panel.title = "Save Diagnostics"
panel.message = "Save a redacted diagnostics report you can share manually."
panel.nameFieldStringValue = "omi-diagnostics-\(Self.exportTimestamp()).txt"
panel.allowedContentTypes = [.plainText]
panel.canCreateDirectories = true
guard panel.runModal() == .OK, let url = panel.url else { return }
// Building the bundle reads the log, serializes snapshots, and writes the
// file — keep it off the main thread so a large log can't hang the UI. The
// panel already returned; reveal in Finder back on main.
DispatchQueue.global(qos: .userInitiated).async {
let saved = DesktopDiagnosticsManager.shared.writeLocalDiagnosticsBundle(to: url)
DispatchQueue.main.async {
if saved {
NSWorkspace.shared.activateFileViewerSelecting([url])
log("Saved local diagnostics bundle to a user-chosen location")
} else {
log("Failed to save local diagnostics bundle")
}
}
}
}
private static func exportTimestamp() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd-HHmmss"
return formatter.string(from: Date())
}
}