Fixed a bug where the chats in the workspace component were not sorted (#79)

This commit is contained in:
Thorsten Sommer 2024-08-19 20:19:55 +02:00 committed by GitHub
parent f8e06faea5
commit a18cb22263
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 37 deletions

View File

@ -18,5 +18,7 @@ public class TreeItemData : ITreeItem
public bool Expandable { get; init; } = true;
public DateTimeOffset LastEditTime { get; init; }
public IReadOnlyCollection<TreeItemData<ITreeItem>> Children { get; init; } = [];
}

View File

@ -109,29 +109,26 @@ public partial class Workspaces : ComponentBase
private async Task<IReadOnlyCollection<TreeItemData<ITreeItem>>> LoadTemporaryChats()
{
var tempChildren = new List<TreeItemData<ITreeItem>>();
var tempChildren = new List<TreeItemData>();
//
// Search for workspace folders in the data directory:
//
// Get the workspace root directory:
// Get the temp root directory:
var temporaryDirectories = Path.Join(SettingsManager.DataDirectory, "tempChats");
// Ensure the directory exists:
Directory.CreateDirectory(temporaryDirectories);
// Enumerate the workspace directories:
// Enumerate the chat directories:
foreach (var tempChatDirPath in Directory.EnumerateDirectories(temporaryDirectories))
{
// Read the `name` file:
var chatNamePath = Path.Join(tempChatDirPath, "name");
var chatName = await File.ReadAllTextAsync(chatNamePath, Encoding.UTF8);
tempChildren.Add(new TreeItemData<ITreeItem>
{
Expandable = false,
Value = new TreeItemData
// Read the last change time of the chat:
var chatThreadPath = Path.Join(tempChatDirPath, "thread.json");
var lastEditTime = File.GetLastWriteTimeUtc(chatThreadPath);
tempChildren.Add(new TreeItemData
{
Type = TreeItemType.CHAT,
Depth = 1,
@ -140,11 +137,16 @@ public partial class Workspaces : ComponentBase
Icon = Icons.Material.Filled.Timer,
Expandable = false,
Path = tempChatDirPath,
},
LastEditTime = lastEditTime,
});
}
return tempChildren;
var result = new List<TreeItemData<ITreeItem>>(tempChildren.OrderByDescending(n => n.LastEditTime).Select(n => new TreeItemData<ITreeItem>
{
Expandable = false,
Value = n,
}));
return result;
}
public async Task<string> LoadWorkspaceName(Guid workspaceId)
@ -205,7 +207,7 @@ public partial class Workspaces : ComponentBase
private async Task<IReadOnlyCollection<TreeItemData<ITreeItem>>> LoadWorkspaceChats(string workspacePath)
{
var workspaceChats = new List<TreeItemData<ITreeItem>>();
var workspaceChats = new List<TreeItemData>();
// Enumerate the workspace directory:
foreach (var chatPath in Directory.EnumerateDirectories(workspacePath))
@ -214,10 +216,11 @@ public partial class Workspaces : ComponentBase
var chatNamePath = Path.Join(chatPath, "name");
var chatName = await File.ReadAllTextAsync(chatNamePath, Encoding.UTF8);
workspaceChats.Add(new TreeItemData<ITreeItem>
{
Expandable = false,
Value = new TreeItemData
// Read the last change time of the chat:
var chatThreadPath = Path.Join(chatPath, "thread.json");
var lastEditTime = File.GetLastWriteTimeUtc(chatThreadPath);
workspaceChats.Add(new TreeItemData
{
Type = TreeItemType.CHAT,
Depth = 2,
@ -226,17 +229,23 @@ public partial class Workspaces : ComponentBase
Icon = Icons.Material.Filled.Chat,
Expandable = false,
Path = chatPath,
},
LastEditTime = lastEditTime,
});
}
workspaceChats.Add(new TreeItemData<ITreeItem>
var result = new List<TreeItemData<ITreeItem>>(workspaceChats.OrderByDescending(n => n.LastEditTime).Select(n => new TreeItemData<ITreeItem>
{
Expandable = false,
Value = n,
}));
result.Add(new()
{
Expandable = false,
Value = new TreeButton(WorkspaceBranch.WORKSPACES, 2, "Add chat",Icons.Material.Filled.AddComment, () => this.AddChat(workspacePath)),
});
return workspaceChats;
return result;
}
public async Task StoreChat(ChatThread chat)

View File

@ -0,0 +1,2 @@
# v0.8.11, build 173
- Fixed a bug where the chats in the workspace component were not sorted by date.