Skip to content

Commit

Permalink
Fix NRE in scripts page. Refactor logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed May 16, 2023
1 parent 0857b38 commit 9ea17ad
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Server/Components/Scripts/RunScript.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<span class="align-top">Show only mine</span>
</div>
</div>
<TreeView DataSource="ParentPage.FilteredScriptNodes"
<TreeView DataSource="ParentPage.TreeNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/SavedScripts.razor
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<span class="align-top">Show only mine</span>
</div>
</div>
<TreeView DataSource="ParentPage.FilteredScriptNodes"
<TreeView DataSource="ParentPage.TreeNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/ScriptSchedules.razor
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<input type="checkbox" @bind="ParentPage.ShowOnlyMyScripts" />
<span class="align-top">Show only mine</span>
</div>
<TreeView DataSource="ParentPage.FilteredScriptNodes"
<TreeView DataSource="ParentPage.TreeNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
1 change: 1 addition & 0 deletions Server/Components/Scripts/ScriptTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ScriptTreeNode
public string Id { get; } = Guid.NewGuid().ToString();
public TreeItemType ItemType { get; set; }
public string Name { get; init; }
public ScriptTreeNode? ParentNode { get; set; }
public List<ScriptTreeNode> ChildItems { get; } = new();
public SavedScript Script { get; init; }
}
Expand Down
105 changes: 58 additions & 47 deletions Server/Pages/ScriptsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@


@code {
private IEnumerable<ScriptTreeNode>? _filteredScriptNodes;
private readonly List<ScriptTreeNode> _treeNodes = new();
private IEnumerable<SavedScript> _allScripts = Enumerable.Empty<SavedScript>();

private bool _showOnlyMyScripts = true;

[Parameter]
Expand All @@ -43,34 +45,24 @@
get => _showOnlyMyScripts;
set
{
_filteredScriptNodes = null;
_showOnlyMyScripts = value;
_treeNodes.Clear();
}
}

public List<ScriptTreeNode> TreeNodes { get; } = new();

public IEnumerable<ScriptTreeNode> FilteredScriptNodes
public IEnumerable<ScriptTreeNode> TreeNodes
{
get
{
if (_filteredScriptNodes?.Any() == true)
if (_treeNodes?.Any() == true)
{
return _filteredScriptNodes;
return _treeNodes;
}

if (ShowOnlyMyScripts)
{
_filteredScriptNodes = TreeNodes.Where(x =>
x.Script.CreatorId == User.Id);
}
else
{
_filteredScriptNodes = TreeNodes.Where(x =>
x.Script.IsPublic || x.Script.CreatorId == User.Id);
}
RefreshTreeNodes();

return _filteredScriptNodes;
return _treeNodes;
}
}

Expand All @@ -85,31 +77,9 @@

public async Task RefreshScripts()
{
TreeNodes.Clear();
_filteredScriptNodes = null;

var allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID);

foreach (var script in allScripts)
{
var root = BuildFolderPath(script.FolderPath);
root.Add(new ScriptTreeNode()
{
Name = script.Name,
Script = script,
ItemType = TreeItemType.Item
});
}
_treeNodes.Clear();

TreeNodes.Sort((a, b) =>
{
if (a.ItemType != b.ItemType)
{
return Comparer.Default.Compare(a.ItemType, b.ItemType);
}

return Comparer.Default.Compare(a.Name, b.Name);
});
_allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID);
}

protected override async Task OnInitializedAsync()
Expand All @@ -119,13 +89,14 @@
}


private List<ScriptTreeNode> BuildFolderPath(string folderPath)
private void CreateTreeNode(SavedScript script)
{
var root = TreeNodes;
var root = _treeNodes;
ScriptTreeNode? targetParent = null;

if (!string.IsNullOrWhiteSpace(folderPath))
if (!string.IsNullOrWhiteSpace(script.FolderPath))
{
var paths = folderPath.Split("/", StringSplitOptions.RemoveEmptyEntries);
var paths = script.FolderPath.Split("/", StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < paths.Length; i++)
{
var existingParent = root.Find(x => x.Name == paths[i]);
Expand All @@ -135,18 +106,58 @@
var newItem = new ScriptTreeNode()
{
Name = paths[i],
ItemType = TreeItemType.Folder
ItemType = TreeItemType.Folder,
ParentNode = existingParent
};
root.Add(newItem);
root = newItem.ChildItems;
targetParent = newItem;
}
else
{
root = existingParent.ChildItems;
targetParent = existingParent;
}
}
}

return root;
var scriptNode = new ScriptTreeNode()
{
Name = script.Name,
Script = script,
ItemType = TreeItemType.Item,
ParentNode = targetParent
};

root.Add(scriptNode);
}

private void RefreshTreeNodes()
{
_treeNodes.Clear();

foreach (var script in _allScripts)
{
var showScript = ShowOnlyMyScripts ?
script.CreatorId == User.Id :
script.CreatorId == User.Id || script.IsPublic;

if (!showScript)
{
continue;
}

CreateTreeNode(script);
}

_treeNodes.Sort((a, b) =>
{
if (a.ItemType != b.ItemType)
{
return Comparer.Default.Compare(a.ItemType, b.ItemType);
}

return Comparer.Default.Compare(a.Name, b.Name);
});
}
}

0 comments on commit 9ea17ad

Please sign in to comment.