Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
// MobileShell own their subsequent data loading.
appInitialized = true;
if (!setupCompleted) {
moduleSettings.load();
moduleSettings.ensureLoaded();
} else if ($authStore.isAuthenticated) {
// An OAuth callback just landed here: send the user back to the
// page that started the flow (replaces the callback redirect entry).
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lib/api/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export const workspaces = {
),
getItemTypes: (id) => fetchV2Data(`/workspaces/${id}/item-types`),
getWorkflows: (id) => fetchV2Data(`/workspaces/${id}/workflows`),
getPriorities: (id) => fetchV2Data(`/workspaces/${id}/priorities`),
getPriorities: (id, requestOptions = {}) =>
fetchV2Data(`/workspaces/${id}/priorities`, requestOptions),
// Resolved effective configuration: assigned config set (else the global
// default), its condition/approval sets, effective screens per mode with
// hydrated fields, and config-set priorities.
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/features/milestones/Milestones.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
onMount(async () => {
await Promise.all([
loadData(),
moduleSettings.load()
moduleSettings.ensureLoaded()
]);

// Load test statistics if test management is enabled
Expand Down
67 changes: 32 additions & 35 deletions frontend/src/lib/pickers/PriorityPicker.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<script>
import { onMount } from 'svelte';
import { priorities as prioritiesApi } from '../api/configuration.js';
import AlertCircle from '@lucide/svelte/icons/alert-circle';
import { priorityIconMap } from '../utils/icons.js';
import ItemPicker from './ItemPicker.svelte';
import { t } from '../stores/i18n.svelte.js';
import { workspaceDataStore } from '../stores/workspaceDataStore.svelte.js';
import { workspaces } from '../api/workspaces.js';
import { objectDisplayName } from '../utils/systemLabels.js';

// Props
Expand Down Expand Up @@ -43,40 +41,45 @@
getLabel: (item) => objectDisplayName(item, 'priority')
};

// Reactive: Use provided items or fetch when workspaceId changes
// Reactive: use provided items, otherwise load the target workspace's
// effective priority catalog from the workspace-priorities API. The API owns
// configuration-set resolution, so the picker no longer reimplements it and
// a single effect (no mount duplicate) owns loading.
$effect(() => {
if (items) {
priorities = [...items].sort((a, b) => a.sort_order - b.sort_order);
loading = false;
} else if (workspaceId) {
loadPriorities();
}
});

async function loadPriorities() {
if (!workspaceId) return;

try {
loading = true;
error = null;

const config = await workspaceDataStore.screenConfig(null);
const configuredPriorities = config?.priorities || [];
priorities = configuredPriorities.length > 0
? configuredPriorities
: await prioritiesApi.getAll();

// Sort by sort_order
priorities = priorities.sort((a, b) => a.sort_order - b.sort_order);

} catch (err) {
console.error('Failed to load priorities:', err);
error = 'Failed to load priorities';
return;
}
const id = workspaceId;
if (!id) {
priorities = [];
} finally {
loading = false;
return;
}
}
let cancelled = false;
loading = true;
error = null;
workspaces
.getPriorities(id)
.then((result) => {
if (cancelled) return;
priorities = [...(result || [])].sort((a, b) => a.sort_order - b.sort_order);
})
.catch((err) => {
if (cancelled) return;
console.error('Failed to load priorities:', err);
error = 'Failed to load priorities';
priorities = [];
})
.finally(() => {
if (!cancelled) loading = false;
});
return () => {
cancelled = true;
};
});

function handlePrioritySelect(priority) {
const id = priority?.id ?? null;
Expand All @@ -87,12 +90,6 @@
function getIconComponent(iconName) {
return priorityIconMap[iconName] || AlertCircle;
}

onMount(() => {
if (!items) {
loadPriorities();
}
});
</script>

{#if loading}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/settings/ModuleSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
});

onMount(() => {
moduleSettings.load().then(() => {
moduleSettings.ensureLoaded().then(() => {
initialLoad = false; // Enable auto-save after initial load
});
loadPlugins();
Expand Down
17 changes: 1 addition & 16 deletions frontend/src/lib/stores/collectionEditorOptions.svelte.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assets } from '../api/assets.js';
import { priorities } from '../api/configuration.js';
import { iterations, milestones } from '../api/planning.js';
import { customerOrganisations, portalCustomers } from '../api/portal.js';
import { teams } from '../api/teams.js';
Expand Down Expand Up @@ -36,21 +35,7 @@ const OPTION_LOADERS = {
milestones: (workspaceId, options) =>
milestones.getAll({ workspace_id: workspaceId, include_global: true }, options),
iterations: (workspaceId, options) => iterations.getAll({ workspace_id: workspaceId }, options),
priorities: async (workspaceId, options) => {
const workspace = await workspaces.get(workspaceId, options);
options.signal.throwIfAborted();
const configurationSetId = workspace?.configuration_set_id;
if (!configurationSetId) return priorities.getAll({}, options);

const configuredPriorities = await priorities.getAll(
{
configuration_set_id: configurationSetId,
},
options
);
options.signal.throwIfAborted();
return configuredPriorities.length > 0 ? configuredPriorities : priorities.getAll({}, options);
},
priorities: (workspaceId, options) => workspaces.getPriorities(workspaceId, options),
projects: (workspaceId, options) => workspaces.getProjects(workspaceId, options),
portalCustomers: (_workspaceId, options) => portalCustomers.getAll({}, options),
customerOrganisations: async (_workspaceId, options) =>
Expand Down
82 changes: 46 additions & 36 deletions frontend/src/lib/stores/collectionPreferences.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ class CollectionPreferencesStore {
#userId = null;
#loadPromise = null;
#generation = 0;
#dirtyViewScopes = new Set();
#dirtyFilterScopes = new Set();
#dirtyCommentSort = false;
// Unsaved writes belong to the authenticated session that produced them.
// They are stored as the actual patch to send, not as dirty scope names, so
// a later flush cannot reconstruct a delete from whichever snapshot happens
// to be current. #session increments on reset so a stale rejection cannot
// re-arm an old account's write.
#session = 0;
#pendingViewState = {};
#pendingSavedFilters = {};
#pendingCommentSort = null;
#saveTimer = null;
#saving = null;

Expand Down Expand Up @@ -74,6 +80,7 @@ class CollectionPreferencesStore {
/** Clear the cache on logout or user change. */
reset() {
this.#generation += 1;
this.#session += 1;
setCalendarPreference('auto');
setWeekStartPreference('auto');
this.viewState = {};
Expand All @@ -83,9 +90,9 @@ class CollectionPreferencesStore {
this.loadError = false;
this.#userId = null;
this.#loadPromise = null;
this.#dirtyViewScopes.clear();
this.#dirtyFilterScopes.clear();
this.#dirtyCommentSort = false;
this.#pendingViewState = {};
this.#pendingSavedFilters = {};
this.#pendingCommentSort = null;
if (this.#saveTimer) {
clearTimeout(this.#saveTimer);
this.#saveTimer = null;
Expand All @@ -112,11 +119,12 @@ class CollectionPreferencesStore {
const scope = collectionScopeId(workspaceId, collectionId);
const currentScope = this.viewState[scope] ?? {};
const currentView = currentScope[view] ?? {};
const nextScope = { ...currentScope, [view]: { ...currentView, ...patch } };
this.viewState = {
...this.viewState,
[scope]: { ...currentScope, [view]: { ...currentView, ...patch } },
[scope]: nextScope,
};
this.#dirtyViewScopes.add(scope);
this.#pendingViewState[scope] = nextScope;
this.#scheduleSave();
}

Expand All @@ -129,7 +137,7 @@ class CollectionPreferencesStore {
setSavedFilters(workspaceId, collectionId, filters) {
const scope = collectionScopeId(workspaceId, collectionId);
this.savedFilters = { ...this.savedFilters, [scope]: filters };
this.#dirtyFilterScopes.add(scope);
this.#pendingSavedFilters[scope] = filters;
this.#scheduleSave();
}

Expand All @@ -138,7 +146,7 @@ class CollectionPreferencesStore {
const next = order === 'newest' ? 'newest' : 'oldest';
if (this.commentSortOrder === next) return;
this.commentSortOrder = next;
this.#dirtyCommentSort = true;
this.#pendingCommentSort = next;
this.#scheduleSave();
}

Expand All @@ -157,42 +165,44 @@ class CollectionPreferencesStore {
this.#saveTimer = null;
}
if (this.#saving) {
// A save is already in flight; the new dirty scopes ride the next flush.
// A save is already in flight; the new pending writes ride the next flush.
await this.#saving;
}
const viewScopes = [...this.#dirtyViewScopes];
const filterScopes = [...this.#dirtyFilterScopes];
const commentSortDirty = this.#dirtyCommentSort;
if (viewScopes.length === 0 && filterScopes.length === 0 && !commentSortDirty) return;

this.#dirtyViewScopes.clear();
this.#dirtyFilterScopes.clear();
this.#dirtyCommentSort = false;
const session = this.#session;
const viewState = this.#pendingViewState;
const savedFilters = this.#pendingSavedFilters;
const commentSort = this.#pendingCommentSort;
this.#pendingViewState = {};
this.#pendingSavedFilters = {};
this.#pendingCommentSort = null;

const viewScopes = Object.keys(viewState);
const filterScopes = Object.keys(savedFilters);
if (viewScopes.length === 0 && filterScopes.length === 0 && commentSort === null) return;

const body = {};
if (viewScopes.length > 0) {
body.view_state = Object.fromEntries(
viewScopes.map((scope) => [scope, this.viewState[scope] ?? null])
);
}
if (filterScopes.length > 0) {
body.saved_filters = Object.fromEntries(
filterScopes.map((scope) => [scope, this.savedFilters[scope] ?? null])
);
}
if (commentSortDirty) {
body.comment_sort_order = this.commentSortOrder;
}
if (viewScopes.length > 0) body.view_state = viewState;
if (filterScopes.length > 0) body.saved_filters = savedFilters;
if (commentSort !== null) body.comment_sort_order = commentSort;

const save = keepalive
? userPreferences.update(body, { keepalive: true })
: userPreferences.update(body);
this.#saving = save
.catch((error) => {
// Re-mark for retry; the local state stays authoritative.
for (const scope of viewScopes) this.#dirtyViewScopes.add(scope);
for (const scope of filterScopes) this.#dirtyFilterScopes.add(scope);
if (commentSortDirty) this.#dirtyCommentSort = true;
// Retry the exact failed patch, but only inside the session that made
// it. A newer pending value for the same scope wins over the retry.
if (session !== this.#session) return;
for (const scope of viewScopes) {
if (!(scope in this.#pendingViewState)) this.#pendingViewState[scope] = viewState[scope];
}
for (const scope of filterScopes) {
if (!(scope in this.#pendingSavedFilters))
this.#pendingSavedFilters[scope] = savedFilters[scope];
}
if (commentSort !== null && this.#pendingCommentSort === null) {
this.#pendingCommentSort = commentSort;
}
console.warn('[collectionPreferences] failed to save preferences', error);
})
.finally(() => {
Expand Down
Loading
Loading