diff --git a/src/store/index.ts b/src/store/index.ts index 54d6944..dea4297 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1058,8 +1058,8 @@ export const initStore = async () => { // await syncSystemTagsAndRules(); }; - // Fire off background sync after a short delay - setTimeout(backgroundSync, 100); + // Fire off background sync + await backgroundSync(); // Separate Templates load (less critical) try { @@ -1548,7 +1548,7 @@ export const upsertTagDefinition = async (name: string, value: number, color?: s value, color: color || "#6366f1", // Default indigo theme: finalTheme - }); + }, { requestKey: null }); const newDef: TagDefinition = { id: record.id, @@ -1636,7 +1636,7 @@ export const syncSystemTagsAndRules = async () => { color: bucket.color || "#64748b", theme: "dark", isBucket: true - }); + }, { requestKey: null }); setStore("tagDefinitions", prev => [...prev, { id: record.id, name: record.name, @@ -1650,7 +1650,7 @@ export const syncSystemTagsAndRules = async () => { } } else if (!tagExistsExact.isBucket) { try { - await pb.collection(TAGS_COLLECTION).update(tagExistsExact.id, { isBucket: true }); + await pb.collection(TAGS_COLLECTION).update(tagExistsExact.id, { isBucket: true }, { requestKey: null }); setStore("tagDefinitions", d => d.id === tagExistsExact.id, { isBucket: true }); } catch (e) { console.error(`Failed to flag tag ${bucketTagName} as bucket`, e); @@ -1673,7 +1673,7 @@ export const syncSystemTagsAndRules = async () => { if (bucketRuleLegacy && !bucketRuleExact) { // Rename legacy rule try { - await pb.collection(SHARE_RULES_COLLECTION).update(bucketRuleLegacy.id, { tagName: bucketTagName }); + await pb.collection(SHARE_RULES_COLLECTION).update(bucketRuleLegacy.id, { tagName: bucketTagName }, { requestKey: null }); setStore("shareRules", r => r.id === bucketRuleLegacy.id, { tagName: bucketTagName }); console.log(`Renamed legacy bucket rule: ${bucket.name} -> ${bucketTagName}`); } catch (e) { @@ -1687,7 +1687,7 @@ export const syncSystemTagsAndRules = async () => { type: 'tag', tagName: bucketTagName, share_mode: 'SEND_TASK' - }); + }, { requestKey: null }); } catch (e) { console.error(`Failed to ensure system rule for bucket ${bucketTagName}`, e); } @@ -1710,7 +1710,7 @@ export const syncSystemTagsAndRules = async () => { color: "#64748b", theme: "dark", isUser: true - }); + }, { requestKey: null }); setStore("tagDefinitions", prev => [...prev, { id: record.id, name: record.name, @@ -1724,7 +1724,7 @@ export const syncSystemTagsAndRules = async () => { } } else if (!tagExistsExact.isUser) { try { - await pb.collection(TAGS_COLLECTION).update(tagExistsExact.id, { isUser: true }); + await pb.collection(TAGS_COLLECTION).update(tagExistsExact.id, { isUser: true }, { requestKey: null }); setStore("tagDefinitions", d => d.id === tagExistsExact.id, { isUser: true }); } catch (e) { console.error(`Failed to flag tag ${userName} as user`, e); @@ -1749,7 +1749,7 @@ export const syncSystemTagsAndRules = async () => { if (selfRuleLegacy && !selfRuleExact) { // Rename legacy self-rule try { - await pb.collection(SHARE_RULES_COLLECTION).update(selfRuleLegacy.id, { tagName: userName }); + await pb.collection(SHARE_RULES_COLLECTION).update(selfRuleLegacy.id, { tagName: userName }, { requestKey: null }); setStore("shareRules", r => r.id === selfRuleLegacy.id, { tagName: userName }); console.log(`Renamed legacy self-rule: ${originalName} -> ${userName}`); } catch (e) { @@ -1763,7 +1763,7 @@ export const syncSystemTagsAndRules = async () => { type: 'tag', tagName: userName, share_mode: 'ADD_USER' - }); + }, { requestKey: null }); const newRule: ShareRule = { id: record.id, @@ -1806,7 +1806,7 @@ export const renameTagDefinition = async (oldName: string, newName: string) => { } try { - await pb.collection(TAGS_COLLECTION).update(def.id, { name: finalName }); + await pb.collection(TAGS_COLLECTION).update(def.id, { name: finalName }, { requestKey: null }); setStore("tagDefinitions", (d) => d.id === def.id, { name: finalName }); const tasksWithTag = store.tasks.filter(t => t.tags?.includes(oldName)); @@ -1820,7 +1820,7 @@ export const renameTagDefinition = async (oldName: string, newName: string) => { const rulesWithTag = store.shareRules.filter(r => r.tagName === oldName); for (const rule of rulesWithTag) { try { - await pb.collection(SHARE_RULES_COLLECTION).update(rule.id, { tagName: finalName }); + await pb.collection(SHARE_RULES_COLLECTION).update(rule.id, { tagName: finalName }, { requestKey: null }); setStore("shareRules", (r) => r.id === rule.id, { tagName: finalName }); console.log(`Synced ShareRule ${rule.id}: ${oldName} -> ${finalName}`); } catch (ruleErr) { @@ -1989,7 +1989,7 @@ export const addTemplate = async (template: Omit) => { completed: false, startDate: new Date().toISOString(), dueDate: new Date().toISOString() - }); + }, { requestKey: null }); const newTemplate = { ...template, id: record.id }; // Check if already added by realtime subscription @@ -2194,7 +2194,7 @@ export const addShareRule = async (type: 'tag' | 'all', targetUserId: string, ta type, tagName: type === 'tag' ? tagName : null, share_mode: share_mode || 'ADD_USER' - }); + }, { requestKey: null }); // Realtime subscription will handle adding to store toast.success(`Share rule created`); } catch (err) { @@ -2207,7 +2207,7 @@ export const updateShareRule = async (ruleId: string, updates: Partial rules.map(r => r.id === ruleId ? { ...r, ...updates } : r) );