Fix: Ensure only one workspace is marked as default

This commit ensures that only one workspace can be marked as default. Previously, multiple workspaces could be marked as default, leading to unexpected behavior.

The commit introduces a transaction to update all workspaces, setting `is_default` to `0` for all workspaces except the one being saved as default. This prevents multiple workspaces from being marked as default.
This commit is contained in:
Kristijan Ribarić 2024-10-04 17:41:48 +02:00
parent f1f547fe69
commit e366e624d1

View file

@ -39,8 +39,15 @@ var ZenWorkspacesStorage = {
async saveWorkspace(workspace) {
await PlacesUtils.withConnectionWrapper('ZenWorkspacesStorage.saveWorkspace', async (db) => {
const now = Date.now();
await db.executeCached(
`
await db.executeTransaction(async function() {
// If the workspace is set as default, unset is_default for all other workspaces
if (workspace.default) {
await db.execute(`UPDATE zen_workspaces SET is_default = 0 WHERE uuid != :uuid`, { uuid: workspace.uuid });
}
// Then insert or replace the workspace
await db.executeCached(`
INSERT OR REPLACE INTO zen_workspaces (
uuid, name, icon, is_default, container_id, created_at, updated_at
) VALUES (
@ -48,16 +55,15 @@ var ZenWorkspacesStorage = {
COALESCE((SELECT created_at FROM zen_workspaces WHERE uuid = :uuid), :now),
:now
)
`,
{
`, {
uuid: workspace.uuid,
name: workspace.name,
icon: workspace.icon || null,
is_default: workspace.default ? 1 : 0,
container_id: workspace.containerTabId || null,
now,
}
);
now
});
});
});
},