Skip to content

Commit

Permalink
Update Widget DeveloperId and Sign-in detection to handle login state…
Browse files Browse the repository at this point in the history
…s cleanly (#12)

* Move selectedDevId to base, update SignIn related logic

* Remove whitespace

* Remove comment
  • Loading branch information
dkbennett authored Nov 14, 2023
1 parent 1b1f88b commit d1e77cc
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 131 deletions.
2 changes: 1 addition & 1 deletion src/AzureExtension/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<comment>Shown in Widget, Tooltip text</comment>
</data>
<data name="Widget_Template.SignInRequired" xml:space="preserve">
<value>You must sign in to view this widget's content.</value>
<value>You must sign in to view this widget's content. Go to the Account Settings page to sign in.</value>
<comment>Shown in Widget when user is not logged in.</comment>
</data>
<data name="Widget_Template_Loading" xml:space="preserve">
Expand Down
51 changes: 21 additions & 30 deletions src/AzureExtension/Widgets/AzurePullRequestsWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ internal class AzurePullRequestsWidget : AzureWidget

// Widget Data
private string widgetTitle = string.Empty;
private string selectedDevId = string.Empty;
private string selectedRepositoryUrl = string.Empty;
private string selectedRepositoryName = string.Empty;
private string selectedView = DefaultSelectedView;
Expand Down Expand Up @@ -91,19 +90,19 @@ protected override void HandleSubmit(WidgetActionInvokedArgs args)
if (dataObject != null && dataObject["account"] != null && dataObject["query"] != null)
{
widgetTitle = dataObject["widgetTitle"]?.GetValue<string>() ?? string.Empty;
selectedDevId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
selectedRepositoryUrl = dataObject["query"]?.GetValue<string>() ?? string.Empty;
selectedView = dataObject["view"]?.GetValue<string>() ?? string.Empty;
SetDefaultDeveloperId();
if (selectedDevId != dataObject["account"]?.GetValue<string>())
SetDefaultDeveloperLoginId();
if (DeveloperLoginId != dataObject["account"]?.GetValue<string>())
{
dataObject["account"] = selectedDevId;
dataObject["account"] = DeveloperLoginId;
data = dataObject.ToJsonString();
}

ConfigurationData = data;

var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
message = Resources.GetResource(@"Widget_Template/DevIDError");
Expand Down Expand Up @@ -135,29 +134,21 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c
SetConfigure();
}

// This method will attempt to select a DeveloperId if one is not already selected.
// It uses the input url and tries to find the most likely DeveloperId that corresponds to the
// url among the set of available DeveloperIds. If there is no best match it chooses the first
// available developerId or none if there are no DeveloperIds.
private void SetDefaultDeveloperId()
// Increase precision of SetDefaultDeveloperLoginId by matching the selectedRepositoryUrl's org
// with the first matching DeveloperId that contains that org.
protected override void SetDefaultDeveloperLoginId()
{
if (!string.IsNullOrEmpty(selectedDevId))
{
return;
}

var devIds = DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds;
if (devIds is null)
{
return;
}

// Set as the first DevId found, unless we find a better match from the Url.
selectedDevId = devIds.FirstOrDefault()?.LoginId ?? string.Empty;
base.SetDefaultDeveloperLoginId();
var azureOrg = new AzureUri(selectedRepositoryUrl).Organization;
if (!string.IsNullOrEmpty(azureOrg))
{
selectedDevId = devIds.Where(i => i.LoginId.Contains(azureOrg, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.LoginId ?? selectedDevId;
var devIds = DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds;
if (devIds is null)
{
return;
}

DeveloperLoginId = devIds.Where(i => i.LoginId.Contains(azureOrg, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.LoginId ?? DeveloperLoginId;
}
}

Expand Down Expand Up @@ -189,7 +180,7 @@ public override void HandleDataManagerUpdate(object? source, DataManagerUpdateEv

public override void RequestContentData()
{
var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
// Should not happen
Expand Down Expand Up @@ -219,11 +210,11 @@ private void ResetDataFromState(string data)
}

widgetTitle = dataObject["widgetTitle"]?.GetValue<string>() ?? string.Empty;
selectedDevId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
selectedRepositoryUrl = dataObject["query"]?.GetValue<string>() ?? string.Empty;
selectedView = dataObject["view"]?.GetValue<string>() ?? string.Empty;

var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
return;
Expand Down Expand Up @@ -251,7 +242,7 @@ public override string GetConfiguration(string data)

configurationData.Add("accounts", developerIdsData);

configurationData.Add("selectedDevId", selectedDevId);
configurationData.Add("selectedDevId", DeveloperLoginId);
configurationData.Add("url", selectedRepositoryUrl);
configurationData.Add("selectedView", selectedView);
configurationData.Add("message", message);
Expand All @@ -268,7 +259,7 @@ public override void LoadContentData()
{
try
{
var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
// Should not happen
Expand Down
53 changes: 22 additions & 31 deletions src/AzureExtension/Widgets/AzureQueryListWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text.Json.Nodes;
using DevHomeAzureExtension.Client;
using DevHomeAzureExtension.DataManager;
using DevHomeAzureExtension.DataModel;
using DevHomeAzureExtension.DeveloperId;
using DevHomeAzureExtension.Helpers;
using Microsoft.Windows.Widgets.Providers;
Expand All @@ -20,7 +19,6 @@ internal class AzureQueryListWidget : AzureWidget

// Widget Data
private string widgetTitle = string.Empty;
private string selectedDevId = string.Empty;
private string selectedQueryUrl = string.Empty;
private string selectedQueryId = string.Empty;
private string? message;
Expand Down Expand Up @@ -90,17 +88,17 @@ protected override void HandleSubmit(WidgetActionInvokedArgs args)
CanPin = false;
widgetTitle = dataObject["widgetTitle"]?.GetValue<string>() ?? string.Empty;
selectedQueryUrl = dataObject["query"]?.GetValue<string>() ?? string.Empty;
selectedDevId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
SetDefaultDeveloperId();
if (selectedDevId != dataObject["account"]?.GetValue<string>())
DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
SetDefaultDeveloperLoginId();
if (DeveloperLoginId != dataObject["account"]?.GetValue<string>())
{
dataObject["account"] = selectedDevId;
dataObject["account"] = DeveloperLoginId;
data = dataObject.ToJsonString();
}

ConfigurationData = data;

var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
message = Resources.GetResource(@"Widget_Template/DevIDError");
Expand Down Expand Up @@ -137,29 +135,22 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c
SetConfigure();
}

// This method will attempt to select a DeveloperId if one is not already selected.
// It uses the input url and tries to find the most likely DeveloperId that corresponds to the
// url among the set of available DeveloperIds. If there is no best match it chooses the first
// available developerId or none if there are no DeveloperIds.
private void SetDefaultDeveloperId()
// Increase precision of SetDefaultDeveloperLoginId by matching the selectedQueryUrl's org
// with the first matching DeveloperId that contains that org.
protected override void SetDefaultDeveloperLoginId()
{
if (!string.IsNullOrEmpty(selectedDevId))
{
return;
}
base.SetDefaultDeveloperLoginId();

var devIds = DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds;
if (devIds is null)
{
return;
}

// Set as the first DevId found, unless we find a better match from the Url.
selectedDevId = devIds.FirstOrDefault()?.LoginId ?? string.Empty;
var azureOrg = new AzureUri(selectedQueryUrl).Organization;
if (!string.IsNullOrEmpty(azureOrg))
{
selectedDevId = devIds.Where(i => i.LoginId.Contains(azureOrg, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.LoginId ?? selectedDevId;
var devIds = DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds;
if (devIds is null)
{
return;
}

DeveloperLoginId = devIds.Where(i => i.LoginId.Contains(azureOrg, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.LoginId ?? DeveloperLoginId;
}
}

Expand Down Expand Up @@ -191,7 +182,7 @@ public override void HandleDataManagerUpdate(object? source, DataManagerUpdateEv

public override void RequestContentData()
{
var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
// Should not happen
Expand Down Expand Up @@ -222,10 +213,10 @@ private void ResetDataFromState(string data)
}

widgetTitle = dataObject["widgetTitle"]?.GetValue<string>() ?? string.Empty;
selectedDevId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
selectedQueryUrl = dataObject["query"]?.GetValue<string>() ?? string.Empty;

var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
return;
Expand Down Expand Up @@ -257,7 +248,7 @@ public override string GetConfiguration(string data)

configurationData.Add("accounts", developerIdsData);

configurationData.Add("selectedDevId", selectedDevId);
configurationData.Add("selectedDevId", DeveloperLoginId);
configurationData.Add("url", selectedQueryUrl);
configurationData.Add("message", message);
configurationData.Add("widgetTitle", widgetTitle);
Expand All @@ -273,7 +264,7 @@ public override void LoadContentData()
{
try
{
var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
// Should not happen, but may be possible in situations where the app is removed and
Expand All @@ -293,7 +284,7 @@ public override void LoadContentData()
}

// This can throw if DataStore is not connected.
Query? queryInfo = DataManager!.GetQuery(azureUri, developerId.LoginId);
var queryInfo = DataManager!.GetQuery(azureUri, developerId.LoginId);

var queryResults = queryInfo is null ? new Dictionary<string, object>() : JsonConvert.DeserializeObject<Dictionary<string, object>>(queryInfo.QueryResults);

Expand Down
50 changes: 20 additions & 30 deletions src/AzureExtension/Widgets/AzureQueryTilesWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ internal class AzureQueryTilesWidget : AzureWidget
protected static readonly new string Name = nameof(AzureQueryTilesWidget);
private readonly List<QueryTile> tiles = new();

private string selectedDevId = string.Empty;

// Creation and destruction methods
public AzureQueryTilesWidget()
: base()
Expand Down Expand Up @@ -185,7 +183,7 @@ public override void HandleDataManagerUpdate(object? source, DataManagerUpdateEv

public override void RequestContentData()
{
var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
// Should not happen
Expand Down Expand Up @@ -218,7 +216,7 @@ public override void LoadContentData()
var data = new JsonObject();
var linesArray = new JsonArray();

var developerId = GetDevId(selectedDevId);
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
// Should not happen
Expand Down Expand Up @@ -284,8 +282,8 @@ private void ValidateConfigurationData()
CanPin = false;
var pinIssueFound = false;

SetDefaultDeveloperId();
var developerId = GetDevId(selectedDevId);
SetDefaultDeveloperLoginId();
var developerId = GetDevId(DeveloperLoginId);
if (developerId == null)
{
return;
Expand Down Expand Up @@ -351,29 +349,21 @@ private void ResetNumberOfTilesFromData(string data)
}
}

// This method will attempt to select a DeveloperId if one is not already selected.
// It uses the url of the first valid tile and tries to find the most likely DeveloperId that
// among the set of available DeveloperIds. If there is no best match it chooses the first
// available developerId or none if there are no DeveloperIds.
private void SetDefaultDeveloperId()
// Increase precision of SetDefaultDeveloperLoginId by matching the first valid org in the
// tiles list with the first matching DeveloperId that contains that org.
protected override void SetDefaultDeveloperLoginId()
{
if (!string.IsNullOrEmpty(selectedDevId))
{
return;
}

var devIds = DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds;
if (devIds is null)
{
return;
}

// Set as the first DevId found, unless we find a better match to the first Org found.
selectedDevId = devIds.FirstOrDefault()?.LoginId ?? string.Empty;
base.SetDefaultDeveloperLoginId();
var azureOrg = tiles.Where(i => i.AzureUri.IsValid).FirstOrDefault().AzureUri?.Organization ?? string.Empty;
if (!string.IsNullOrEmpty(azureOrg))
{
selectedDevId = devIds.Where(i => i.LoginId.Contains(azureOrg, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.LoginId ?? selectedDevId;
var devIds = DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds;
if (devIds is null)
{
return;
}

DeveloperLoginId = devIds.Where(i => i.LoginId.Contains(azureOrg, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.LoginId ?? DeveloperLoginId;
}
}

Expand Down Expand Up @@ -407,11 +397,11 @@ private void UpdateAllTiles(string data)
}
}

selectedDevId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
SetDefaultDeveloperId();
if (selectedDevId != dataObject["account"]?.GetValue<string>())
DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
SetDefaultDeveloperLoginId();
if (DeveloperLoginId != dataObject["account"]?.GetValue<string>())
{
dataObject["account"] = selectedDevId;
dataObject["account"] = DeveloperLoginId;
data = dataObject.ToJsonString();
}

Expand Down Expand Up @@ -450,7 +440,7 @@ public override string GetConfiguration(string data)
}

configurationData.Add("tiles", tilesArray);
configurationData.Add("selectedDevId", selectedDevId);
configurationData.Add("selectedDevId", DeveloperLoginId);
configurationData.Add("configuring", !CanPin);
configurationData.Add("pinned", Pinned);
configurationData.Add("arrow", IconLoader.GetIconAsBase64("arrow.png"));
Expand Down
Loading

0 comments on commit d1e77cc

Please sign in to comment.