mirror of
https://github.com/devonthesofa/obsidian-note-status.git
synced 2026-07-22 05:45:04 +00:00
fix: implement template deduplication and installation safety checks
This commit is contained in:
parent
a51b1ad4c3
commit
7833676b8b
2 changed files with 33 additions and 0 deletions
|
|
@ -63,6 +63,12 @@ export const TemplateSettings: React.FC<TemplateSettingsProps> = ({
|
|||
|
||||
const handleInstallTemplate = useCallback(
|
||||
(template: StatusTemplate) => {
|
||||
if (!isTemplateNameUnique(template.name, undefined)) {
|
||||
alert(
|
||||
`A template with the name "${template.name}" is already installed.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
const newId = generateTemplateId(template.name, settings.templates);
|
||||
const installedTemplate: StatusTemplate = {
|
||||
...template,
|
||||
|
|
|
|||
|
|
@ -103,9 +103,36 @@ class SettingsService {
|
|||
private async loadSettings() {
|
||||
const loadedData = await this.plugin.loadData();
|
||||
this.settings = this.mergeSettings(DEFAULT_PLUGIN_SETTINGS, loadedData);
|
||||
this.deduplicateTemplates();
|
||||
return this.settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes duplicate templates with the same name, keeping the first occurrence.
|
||||
*/
|
||||
private deduplicateTemplates() {
|
||||
if (!this.settings.templates) return;
|
||||
|
||||
const seenNames = new Set<string>();
|
||||
const uniqueTemplates = [];
|
||||
let hasDuplicates = false;
|
||||
|
||||
for (const template of this.settings.templates) {
|
||||
const lowerName = template.name.toLowerCase().trim();
|
||||
if (!seenNames.has(lowerName)) {
|
||||
seenNames.add(lowerName);
|
||||
uniqueTemplates.push(template);
|
||||
} else {
|
||||
hasDuplicates = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasDuplicates) {
|
||||
this.settings.templates = uniqueTemplates;
|
||||
this.saveSettings().catch(console.error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep merges loaded settings with defaults to prevent data loss for nested objects like statusColors.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue