Skip to content

Commit fe859dc

Browse files
authored
fix!: generate stable IDs to avoid orphaned aliases in Command Palette (#50)
1 parent 7d7d63d commit fe859dc

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

CmdPalWebSearchShortcut/WebSearchShortcut/Pages/AddShortcutPage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public AddShortcutPage(WebSearchShortcutDataEntry? shortcut)
1616
var isAdd = string.IsNullOrEmpty(name) && string.IsNullOrEmpty(url);
1717

1818
_addShortcutForm = new AddShortcutForm(shortcut);
19+
20+
Id = "WebSearchShortcut.AddShortcut";
1921
Icon = IconHelpers.FromRelativePath("Assets\\SearchAdd.png");
2022
Title = isAdd ? Resources.AddShortcut_AddTitle : Resources.SearchShortcut_EditTitle;
2123
Name = isAdd ? Resources.AddShortcut_AddName : Resources.SearchShortcut_EditName;

CmdPalWebSearchShortcut/WebSearchShortcut/Pages/SearchWebPage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public SearchWebPage(WebSearchShortcutDataEntry shortcut)
2727
{
2828
_shortcut = shortcut;
2929

30+
Id = shortcut.Id;
3031
Name = shortcut.Name;
3132
Icon = IconService.GetIconInfo(shortcut);
3233
_openHomePageItem = new ListItem(new OpenHomePageCommand(shortcut))

CmdPalWebSearchShortcut/WebSearchShortcut/Storage.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.IO;
34
using System.Text.Json;
@@ -47,6 +48,12 @@ public static Storage ReadFromFile(string path)
4748
if (!string.IsNullOrEmpty(jsonStringReading))
4849
{
4950
data = JsonSerializer.Deserialize(jsonStringReading, AppJsonSerializerContext.Default.Storage) ?? new Storage();
51+
52+
bool modified = EnsureIds(data.Data);
53+
if (modified)
54+
{
55+
WriteToFile(path, data);
56+
}
5057
}
5158
}
5259

@@ -55,8 +62,38 @@ public static Storage ReadFromFile(string path)
5562

5663
public static void WriteToFile(string path, Storage data)
5764
{
65+
EnsureIds(data.Data);
66+
5867
var jsonString = JsonPrettyFormatter.ToPrettyJson(data, AppJsonSerializerContext.Default.Storage);
5968

6069
File.WriteAllText(path, jsonString);
6170
}
71+
72+
private static bool EnsureIds(List<WebSearchShortcutDataEntry> shortcuts)
73+
{
74+
bool modified = false;
75+
HashSet<string> existingIds = [];
76+
77+
foreach (var shortcut in shortcuts)
78+
{
79+
while (string.IsNullOrWhiteSpace(shortcut.Id) || existingIds.Contains(shortcut.Id))
80+
{
81+
modified = true;
82+
83+
shortcut.Id = GenerateId();
84+
}
85+
existingIds.Add(shortcut.Id);
86+
}
87+
88+
return modified;
89+
}
90+
91+
private static string GenerateId()
92+
{
93+
byte[] buffer = new byte[8];
94+
Random.Shared.NextBytes(buffer);
95+
ulong randomNumber = BitConverter.ToUInt64(buffer, 0);
96+
97+
return $"WebSearchShortcut{randomNumber}";
98+
}
6299
}

CmdPalWebSearchShortcut/WebSearchShortcut/WebSearchShortcutDataEntry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace WebSearchShortcut;
66

77
internal sealed class WebSearchShortcutDataEntry
88
{
9+
public string Id { get; set; } = string.Empty;
910
public string Name { get; set; } = string.Empty;
1011
public string? Keyword { get; set; }
1112
public string Url { get; set; } = string.Empty;

0 commit comments

Comments
 (0)