Skip to content

Commit 50f7792

Browse files
committed
fix(Avalonia): initialize telemetry when user reports error to dev
1 parent 1197329 commit 50f7792

1 file changed

Lines changed: 51 additions & 10 deletions

File tree

SnapX.Avalonia/App.axaml.cs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public override void Initialize()
7373

7474
private void ShowErrorDialog(string? title, Exception ex)
7575
{
76-
// Create the dialog content with a button
7776
var stackPanel = new StackPanel
7877
{
7978
Orientation = Orientation.Vertical,
@@ -139,7 +138,7 @@ private void ShowErrorDialog(string? title, Exception ex)
139138
FontWeight = FontWeight.Bold,
140139
CornerRadius = new CornerRadius(5)
141140
};
142-
reportButton.Click += (sender, e) => OnReportErrorClicked();
141+
reportButton.Click += (sender, e) => OnReportErrorClicked(reportButton, ex);
143142

144143
var githubButton = new Button
145144
{
@@ -173,8 +172,7 @@ private void ShowErrorDialog(string? title, Exception ex)
173172
CornerRadius = new CornerRadius(5)
174173
};
175174

176-
// Copy error to clipboard when clicked
177-
copyButton.Click += (sender, e) => CopyErrorToClipboard(ex.ToString());
175+
copyButton.Click += (sender, e) => CopyErrorToClipboard(copyButton, ex.ToString());
178176

179177

180178
buttonPanel.Children.Add(reportButton);
@@ -208,18 +206,61 @@ private void OnGitHubButtonClicked(Exception ex)
208206
URLHelpers.OpenURL(newIssueURL);
209207
}
210208

211-
private void CopyErrorToClipboard(string? errorMessage)
209+
private void CopyErrorToClipboard(Control Sender, string? errorMessage)
212210
{
213-
Clipboard.CopyText(errorMessage);
211+
var topLevel = TopLevel.GetTopLevel(Sender);
212+
if (topLevel is null)
213+
{
214+
DebugHelper.WriteLine("TopLevel is null");
215+
return;
216+
}
217+
topLevel.Clipboard?.SetTextAsync(errorMessage);
214218
}
215219

216-
private void OnReportErrorClicked()
220+
private async void OnReportErrorClicked(Button button, Exception ex)
217221
{
218-
// For now, do nothing when the button is clicked
219-
// This is where Sentry comes in
220-
Console.WriteLine("Report Error button clicked. No action yet. If you have telemetry enabled, (it is by default) it will have already been sent to Sentry.");
222+
var originalButtonContent = CreateContentCopy(button.Content!);
223+
224+
try
225+
{
226+
if (!FeatureFlags.DisableTelemetry && Core.SnapX.TelemetryHandler is null)
227+
{
228+
Core.SnapX.InitTelemetryServices();
229+
SentrySdk.CaptureException(ex);
230+
231+
DebugHelper.WriteLine("Error reported to Sentry successfully.");
232+
}
233+
else
234+
{
235+
DebugHelper.WriteLine("Error has likely already been sent to Sentry as telemetry is not disabled! :heart:");
236+
}
237+
238+
button.Content = "✓ Reported";
239+
button.IsEnabled = false;
240+
241+
await Task.Delay(TimeSpan.FromSeconds(3));
242+
}
243+
catch (Exception taskEx)
244+
{
245+
DebugHelper.WriteLine($"Error during exception reporting: {taskEx.Message}");
246+
}
247+
finally
248+
{
249+
button.Content = originalButtonContent;
250+
}
221251
}
252+
private object CreateContentCopy(object content)
253+
{
254+
if (content == null)
255+
return null;
222256

257+
return content switch
258+
{
259+
string str => string.Copy(str),
260+
ICloneable cloneable => cloneable.Clone(),
261+
_ => content // For other types, we have to hope they're immutable
262+
};
263+
}
223264
private void Shutdown()
224265
{
225266
try

0 commit comments

Comments
 (0)