Skip to content

Write HTML. Render video. Built for agents.

feat(studio): add per-project UI preference scoping (#4093)

Miguel Ángela192701adb11
diff --git a/packages/studio/src/utils/studioUiPreferences.test.ts b/packages/studio/src/utils/studioUiPreferences.test.ts
index 9e80b0abf2..01f1754dfa 100644
--- a/packages/studio/src/utils/studioUiPreferences.test.ts
+++ b/packages/studio/src/utils/studioUiPreferences.test.ts
@@ -101,6 +101,34 @@ describe("thumbnailMode preference", () => {
});
});
+describe("per-project scoping", () => {
+ it("keeps two projects' preferences independent", () => {
+ const storage = createStorage();
+ writeStudioUiPreferences({ leftWidth: 300 }, storage, "alpha");
+ writeStudioUiPreferences({ leftWidth: 500 }, storage, "beta");
+
+ expect(readStudioUiPreferences(storage, "alpha").leftWidth).toBe(300);
+ expect(readStudioUiPreferences(storage, "beta").leftWidth).toBe(500);
+ });
+
+ it("a project with nothing saved yet inherits the pre-scoping global entry once", () => {
+ const storage = createStorage();
+ storage.setItem("hf-studio-ui-preferences", JSON.stringify({ leftWidth: 600 }));
+
+ expect(readStudioUiPreferences(storage, "gamma").leftWidth).toBe(600);
+ });
+
+ it("stops inheriting the global entry once the project has its own write", () => {
+ const storage = createStorage();
+ storage.setItem("hf-studio-ui-preferences", JSON.stringify({ leftWidth: 600 }));
+
+ writeStudioUiPreferences({ leftWidth: 250 }, storage, "gamma");
+
+ expect(readStudioUiPreferences(storage, "gamma").leftWidth).toBe(250);
+ expect(readStudioUiPreferences(storage, null).leftWidth).toBe(600);
+ });
+});
+
describe("timeline zoom pin persistence", () => {
it("round-trips a pinned manual zoom (survives the post-edit reload)", () => {
const storage = createStorage();
diff --git a/packages/studio/src/utils/studioUiPreferences.ts b/packages/studio/src/utils/studioUiPreferences.ts
index e38d89d395..f4ae7f775b 100644
--- a/packages/studio/src/utils/studioUiPreferences.ts
+++ b/packages/studio/src/utils/studioUiPreferences.ts
@@ -62,11 +62,15 @@ function getBrowserStorage(): Storage | null {
}
}
+function storageKeyFor(projectId: string | null): string {
+ return projectId ? `${STUDIO_UI_PREFERENCES_KEY}:${projectId}` : STUDIO_UI_PREFERENCES_KEY;
+}
+
// fallow-ignore-next-line complexity
-function readStorage(storage: Storage | null): StudioUiPreferences {
+function readStorage(storage: Storage | null, key: string): StudioUiPreferences {
if (!storage) return {};
try {
- const raw = storage.getItem(STUDIO_UI_PREFERENCES_KEY);
+ const raw = storage.getItem(key);
if (!raw) return {};
const parsed: unknown = JSON.parse(raw);
if (!isRecord(parsed)) return {};
@@ -163,21 +167,30 @@ function readStorage(storage: Storage | null): StudioUiPreferences {
}
}
-export function readStudioUiPreferences(storage: Storage | null = getBrowserStorage()) {
- return readStorage(storage);
+/** `projectId` opts a caller into a per-project entry (falls back once to the
+ * shared entry so a project's first read isn't blank). Defaults to `null`:
+ * most callers read once at mount, never on a live project switch. */
+export function readStudioUiPreferences(
+ storage: Storage | null = getBrowserStorage(),
+ projectId: string | null = null,
+): StudioUiPreferences {
+ const scoped = readStorage(storage, storageKeyFor(projectId));
+ if (!projectId || Object.keys(scoped).length > 0) return scoped;
+ return readStorage(storage, STUDIO_UI_PREFERENCES_KEY);
}
export function writeStudioUiPreferences(
patch: StudioUiPreferences,
storage: Storage | null = getBrowserStorage(),
+ projectId: string | null = null,
) {
if (!storage) return;
try {
const next = {
- ...readStorage(storage),
+ ...readStudioUiPreferences(storage, projectId),
...patch,
};
- storage.setItem(STUDIO_UI_PREFERENCES_KEY, JSON.stringify(next));
+ storage.setItem(storageKeyFor(projectId), JSON.stringify(next));
} catch {
/* localStorage may be unavailable or full */
}