@page "/scripts" @inherits AuthComponentBase @using System.Collections @inject IDataService DataService Saved Scripts Run Script Script Schedules @code { public bool ShowOnlyMyScripts { get; set; } = true; public readonly List TreeNodes = new(); public string GetItemIconCss(ScriptTreeNode viewModel) { if (viewModel.ItemType == TreeItemType.Folder) { return "oi oi-folder text-warning"; } return "oi oi-script text-success"; } public async Task RefreshScripts() { TreeNodes.Clear(); var allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID); foreach (var script in allScripts) { if (ShowOnlyMyScripts && script.CreatorId != User.Id && !script.IsPublic) { continue; } var root = BuildFolderPath(script.FolderPath); root.Add(new ScriptTreeNode() { Name = script.Name, Script = script, ItemType = TreeItemType.Item }); } 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); }); } protected override async Task OnInitializedAsync() { await RefreshScripts(); await base.OnInitializedAsync(); } private List BuildFolderPath(string folderPath) { var root = TreeNodes; if (!string.IsNullOrWhiteSpace(folderPath)) { var paths = folderPath.Split("/", StringSplitOptions.RemoveEmptyEntries); for (var i = 0; i < paths.Length; i++) { var existingParent = root.Find(x => x.Name == paths[i]); if (existingParent is null) { var newItem = new ScriptTreeNode() { Name = paths[i], ItemType = TreeItemType.Folder }; root.Add(newItem); root = newItem.ChildItems; } else { root = existingParent.ChildItems; } } } return root; } }