diff --git a/src/lib/appwrite/user-prefs-actions.ts b/src/lib/appwrite/user-prefs-actions.ts index d10f4c8..517a5ac 100644 --- a/src/lib/appwrite/user-prefs-actions.ts +++ b/src/lib/appwrite/user-prefs-actions.ts @@ -27,7 +27,26 @@ export async function getUserPrefs(): Promise { queries: [Query.equal("userId", user.$id), Query.limit(1)], }); - if (result.rows.length === 0) return {}; + if (result.rows.length === 0) { + // Pre-create an empty row so saveUserPrefsAction always calls updateRow + // (createRow inside a Server Action causes router cache invalidation → remount loop) + try { + await tablesDB.createRow( + DATABASE_ID, + TABLES.userPreferences, + ID.unique(), + { userId: user.$id }, + [ + Permission.read(Role.user(user.$id)), + Permission.update(Role.user(user.$id)), + Permission.delete(Role.user(user.$id)), + ], + ); + } catch { + // race condition or already exists — fine + } + return {}; + } const row = result.rows[0] as Record; const str = (v: unknown) => (v && typeof v === "string" ? v : undefined);