Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on SSH wallet widget #3895

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,24 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c
SetConfigure();
}

// This function assumes arg.Data will be a json string object with the following structure:
// { "data": "hostname" }
private void HandleConnect(WidgetActionInvokedArgs args)
{
var jsonObject = JsonDocument.Parse(args.Data).RootElement;
var host = jsonObject.GetProperty("data").GetString();

if (string.IsNullOrEmpty(host))
{
Log.Error("Invalid data received for HandleConnect.");
return;
}

var cmd = new Process();
cmd.StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/k \"ssh {args.Data}\"",
Arguments = $"/k \"ssh {host}\"",
UseShellExecute = true,
};

Expand Down
44 changes: 20 additions & 24 deletions tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.Widgets;
using Microsoft.Windows.Widgets.Hosts;
using Newtonsoft.Json.Linq;
using Serilog;

namespace DevHome.Dashboard.ViewModels;
Expand Down Expand Up @@ -311,20 +310,29 @@ private Grid GetErrorCard(string error, string subError = null)
return grid;
}

private string MergeJsonData(string jsonStringA, string jsonStringB)
private Newtonsoft.Json.Linq.JObject WrapJsonString(string jsonString)
{
if (string.IsNullOrEmpty(jsonStringA))
return new Newtonsoft.Json.Linq.JObject { ["data"] = jsonString };
}

private string MergeJsonData(Windows.Data.Json.JsonValue actionValue, Windows.Data.Json.JsonObject inputsObject)
{
Newtonsoft.Json.Linq.JObject objA = [];
Newtonsoft.Json.Linq.JObject objB = [];

if (actionValue?.ValueType == Windows.Data.Json.JsonValueType.Object)
{
return jsonStringB;
objA = Newtonsoft.Json.Linq.JObject.Parse(actionValue.Stringify());
}

if (string.IsNullOrEmpty(jsonStringB))
else if (actionValue?.ValueType == Windows.Data.Json.JsonValueType.String)
{
return jsonStringA;
objA = WrapJsonString(actionValue.Stringify());
}

var objA = JObject.Parse(jsonStringA);
var objB = JObject.Parse(jsonStringB);
if (inputsObject?.ValueType != Windows.Data.Json.JsonValueType.Null)
{
objB = Newtonsoft.Json.Linq.JObject.Parse(inputsObject.Stringify());
}

objA.Merge(objB);

Expand All @@ -341,22 +349,10 @@ private async void HandleAdaptiveAction(RenderedAdaptiveCard sender, AdaptiveAct
}
else if (args.Action is AdaptiveExecuteAction executeAction)
{
var actionData = string.Empty;
var inputsData = string.Empty;

var dataType = executeAction.DataJson.ValueType;
if (dataType != Windows.Data.Json.JsonValueType.Null)
{
actionData = executeAction.DataJson.Stringify();
}

var inputType = args.Inputs.AsJson().ValueType;
if (inputType != Windows.Data.Json.JsonValueType.Null)
{
inputsData = args.Inputs.AsJson().Stringify();
}
Windows.Data.Json.JsonValue actionValue = executeAction.DataJson;
Windows.Data.Json.JsonObject inputsObject = args.Inputs.AsJson();

var dataToSend = MergeJsonData(actionData, inputsData);
var dataToSend = MergeJsonData(actionValue, inputsObject);

_log.Information($"Verb = {executeAction.Verb}, Data = {dataToSend}");
await Widget.NotifyActionInvokedAsync(executeAction.Verb, dataToSend);
Expand Down
Loading