diff --git a/.changeset/28984-studio-draft-saved-named.md b/.changeset/28984-studio-draft-saved-named.md new file mode 100644 index 00000000000..a3a9e27a29a --- /dev/null +++ b/.changeset/28984-studio-draft-saved-named.md @@ -0,0 +1,14 @@ +--- +'@object-ui/app-shell': patch +'@object-ui/i18n': patch +--- + +fix(app-shell): a landed Studio draft autosave names what it saved + +Clause-②: no +Metadata-protocol: none + +A Studio draft autosave used to land with no feedback but the top bar's "Saved HH:MM", which names nothing. Now, once the save lands, Studio shows a toast that names the item: "Draft saved: Task" for an object (or for its customization, in a package that customizes a base), for an Interfaces page, dashboard or report, for an app's navigation, and for a flow. The toast uses one id per item, so the saves that follow each editing pause update the toast already on screen instead of stacking more toasts; another item saved meanwhile gets its own. The top bar's saved hint stays. A refused save keeps its refusal strip and shows no "saved" toast. + +- **`@object-ui/app-shell`**: the Data, Interfaces and Automations pillars announce each landed draft autosave by the item's label. +- **`@object-ui/i18n`**: one new studio-namespace string, `engine.studio.draftSaved` ("Draft saved: {item}"), in English and Chinese. diff --git a/ui/packages/app-shell/src/views/studio-design/DataPillar.draftSavedNamed-28984.test.tsx b/ui/packages/app-shell/src/views/studio-design/DataPillar.draftSavedNamed-28984.test.tsx new file mode 100644 index 00000000000..fbb16792649 --- /dev/null +++ b/ui/packages/app-shell/src/views/studio-design/DataPillar.draftSavedNamed-28984.test.tsx @@ -0,0 +1,186 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * objectstack#28984 — a landed Studio draft autosave names what it saved. + * + * The only feedback a landed draft autosave gave was the top bar's + * "Saved HH:MM", which names nothing; only feedback that names the item counts + * as write feedback (ruling 6545938831 ② on #28345). The Data pillar now + * toasts "Draft saved: LABEL" when the draft save lands, and keeps the hint. + * + * Driven through the pillar's real autosave and a real `MetadataClient` over a + * fetch double, with sonner's real `Toaster` mounted, so the assertions read + * the toasts a user would see: + * + * - a landed save shows one toast naming the object, beside the hint; + * - a refused save shows the refusal and no "saved" toast; + * - saving one object again (an Add field, then typing the field's label) + * keeps ONE toast on screen: every save announces under the same sonner id, + * which updates the toast in place. The toaster's duration is long here so + * the first toast is still up when the second save lands; a per-save id + * would then show two. + */ + +import '@testing-library/jest-dom/vitest'; +import * as React from 'react'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { render, screen, fireEvent, cleanup, act, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { MemoryRouter } from 'react-router-dom'; +import { Toaster, toast } from 'sonner'; +import { MetadataClient } from '@object-ui/data-objectstack'; + +const objectDef = { + name: 'showcase_task', + label: 'Task', + fields: [{ name: 'title', label: 'Title', type: 'text' }], +}; + +const puts: string[] = []; +const server = { refuse: false }; + +const fetchImpl = vi.fn(async (url: string, init?: RequestInit) => { + if (init?.method === 'PUT') { + puts.push(String(url)); + if (server.refuse) { + return new Response( + JSON.stringify({ success: false, error: { code: 'VALIDATION_ERROR', message: 'The draft was refused by the server.' } }), + { status: 422, headers: { 'content-type': 'application/json' } }, + ); + } + return new Response(JSON.stringify({ success: true, data: { state: 'draft' } }), { + status: 200, + headers: { 'content-type': 'application/json' }, + }); + } + return new Response('null', { status: 404, headers: { 'content-type': 'application/json' } }); +}); + +const client = new MetadataClient({ baseUrl: 'http://test.local', fetch: fetchImpl as unknown as typeof fetch }); +Object.assign(client, { + list: vi.fn(async () => [{ name: 'showcase_task', label: 'Task' }]), + listDrafts: vi.fn(async () => []), + layered: vi.fn(async () => ({ effective: objectDef, code: objectDef })), + getDraft: vi.fn(async () => null), +}); + +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { + const mod = await importOriginal(); + return { ...mod, useMetadataClient: () => client, useMetadataTypes: () => ({ entries: [] }) }; +}); + +vi.mock('./packages-io', async (importOriginal) => { + const mod = await importOriginal(); + return { ...mod, fetchPackages: vi.fn(async () => []) }; +}); + +vi.mock('@object-ui/react', async (importOriginal) => { + const mod = await importOriginal(); + return { ...mod, useAdapter: () => dataSource }; +}); + +import { DataPillar } from './StudioDesignSurface'; +import { addFieldFromFormToolbar } from './__tests__/dataPillarAddField'; +import { createEmptyDataSource } from './__tests__/emptyDataSource'; +import { registerBuiltinInspectors } from '../metadata-admin/inspectors'; +import { tFormat } from '../metadata-admin/i18n'; + +const dataSource = createEmptyDataSource(); +registerBuiltinInspectors(); + +const SAID = tFormat('engine.studio.draftSaved', 'en', { item: 'Task' }); +const TOAST_ID = 'studio-draft-saved:object:showcase_task'; + +afterEach(() => { + act(() => { + toast.dismiss(); + }); + cleanup(); + puts.length = 0; + server.refuse = false; + fetchImpl.mockClear(); + vi.restoreAllMocks(); +}); + +function mount(): void { + render( + <> + + + + + , + ); +} + +/** The sonner toasts on screen whose text is the draft-saved sentence. */ +function savedToasts(): HTMLElement[] { + return Array.from(document.querySelectorAll('[data-sonner-toast]')).filter((el) => + (el.textContent ?? '').includes(SAID), + ); +} + +/** The inspector control under a visible label. */ +function controlUnder(label: string): HTMLInputElement { + const lab = screen.getByText(label, { selector: 'label' }); + return lab.parentElement!.querySelector('input') as HTMLInputElement; +} + +/** Waits past the autosave's 1.5 s debounce and the save's round trip. */ +async function pastAutosave(): Promise { + await act(async () => { + await new Promise((r) => setTimeout(r, 2300)); + }); +} + +describe('Studio Data pillar — a landed draft autosave names what it saved (objectstack#28984)', () => { + it('a landed save toasts the object by its label, and the top-bar hint stays', async () => { + const success = vi.spyOn(toast, 'success'); + mount(); + fireEvent.click(await screen.findByRole('button', { name: 'Form' })); + await addFieldFromFormToolbar(); + await screen.findByTestId('data-saved-at', undefined, { timeout: 5000 }); + expect(puts.some((u) => u.includes('mode=draft')), `PUTs: ${puts.join(' | ')}`).toBe(true); + + await waitFor(() => expect(savedToasts()).toHaveLength(1)); + expect(SAID).toContain('Task'); + expect(success).toHaveBeenCalledWith(SAID, { id: TOAST_ID }); + }); + + it('a refused save shows its refusal and says nothing about saving', async () => { + server.refuse = true; + const success = vi.spyOn(toast, 'success'); + mount(); + fireEvent.click(await screen.findByRole('button', { name: 'Form' })); + await addFieldFromFormToolbar(); + await screen.findByTestId('studio-refusal', undefined, { timeout: 5000 }); + expect(puts.length, 'the autosave never sent: the harness is dead').toBeGreaterThan(0); + + expect(savedToasts()).toEqual([]); + expect(success.mock.calls.filter(([, opts]) => (opts as { id?: string } | undefined)?.id === TOAST_ID)).toEqual([]); + expect(screen.queryByTestId('data-saved-at')).toBeNull(); + }); + + it('saving one object again keeps one toast on screen, under one id', async () => { + const success = vi.spyOn(toast, 'success'); + mount(); + fireEvent.click(await screen.findByRole('button', { name: 'Form' })); + await addFieldFromFormToolbar(); + await screen.findByText('Label', { selector: 'label' }, { timeout: 4000 }); + await pastAutosave(); + await waitFor(() => expect(savedToasts()).toHaveLength(1)); + const sentFirst = puts.length; + + // The card's own edit: the field's label, typed in its inspector. + const label = controlUnder('Label'); + await userEvent.clear(label); + await userEvent.type(label, 'QA Remark'); + await pastAutosave(); + expect(puts.length, 'the label edit was never saved: the harness is dead').toBeGreaterThan(sentFirst); + + const announced = success.mock.calls.filter(([said]) => said === SAID); + expect(announced.length, 'two landed saves, two announcements').toBeGreaterThanOrEqual(2); + expect(savedToasts(), 'one toast, updated in place, not a pile').toHaveLength(1); + expect(new Set(announced.map(([, opts]) => (opts as { id?: string } | undefined)?.id))).toEqual(new Set([TOAST_ID])); + }); +}); diff --git a/ui/packages/app-shell/src/views/studio-design/StudioDesignSurface.tsx b/ui/packages/app-shell/src/views/studio-design/StudioDesignSurface.tsx index 407e1a9130b..501360611ef 100644 --- a/ui/packages/app-shell/src/views/studio-design/StudioDesignSurface.tsx +++ b/ui/packages/app-shell/src/views/studio-design/StudioDesignSurface.tsx @@ -866,6 +866,32 @@ interface StudioSavedDraft { name: string; } +/** + * objectstack#28984 — a landed draft AUTOSAVE says what it saved, by the + * item's label, as Studio's other writes do (publish names what went live, + * discard says "Draft discarded: LABEL"). The top bar's "Saved HH:MM" stays, + * but it names nothing, and only feedback that names the item counts as write + * feedback (ruling 6545938831 ② on #28345). + * + * The autosave lands after every editing pause, so the toast carries one + * sonner id per item: a later save of the same item UPDATES the toast already + * on screen instead of stacking another (the `PACKAGE_LIST_TOAST_ID` pattern + * above). Typing a label is one live toast, not one per pause; another item + * saved meanwhile gets its own. Called only once the save has landed: a + * refused save keeps its refusal strip and says nothing about saving, and a + * conflict the author settled by reloading saved nothing. + */ +function announceDraftSaved(saved: StudioSavedDraft, label: string, locale: string): void { + toast.success(tFormat('engine.studio.draftSaved', locale, { item: label || saved.name }), { + id: draftSavedToastId(saved), + }); +} + +/** The one sonner id a draft save of this item announces under. */ +function draftSavedToastId(saved: StudioSavedDraft): string { + return `studio-draft-saved:${saved.type}:${saved.name}`; +} + /** Top-bar package switcher: list app packages (writable base vs read-only * code), switch by navigation, create a new writable base via the standard * CreatePackageDialog, and open the standard PackageDetailSheet (info + @@ -5339,6 +5365,8 @@ export function InterfacesPillar({ setLeafSavedAt({ key: leafKeyOf(current), at: new Date() }); // objectui#11204 — clean only if nothing was edited while it was in flight. if (sent.unmoved()) setIfDirty(false); + // objectstack#28984 — named as the rail names the leaf. + announceDraftSaved({ type: current.type, name: current.name }, current.label, locale); onDraftSaved?.({ type: current.type, name: current.name }); } catch (e) { // objectui#11785 — a page's issue paths (its blocks) name no input this @@ -5590,6 +5618,8 @@ export function InterfacesPillar({ // and a clean buffer would let the re-read this save signals install the // served draft over it. Binding it is the edit that sends it. if (sent.unmoved() && !leftOut) setNavDirty(false); + // objectstack#28984 — the navigation is the app's: named by its label. + announceDraftSaved({ type: 'app', name: appName }, navItemLabelText(appLabel, locale), locale); onDraftSaved?.({ type: 'app', name: appName }); } catch (e) { setNavError( @@ -5602,7 +5632,7 @@ export function InterfacesPillar({ } finally { setNavSaving(false); } - }, [saveNavDraft, appName, appDraft, onDraftSaved, packageId, publishNonce, locale]); + }, [saveNavDraft, appName, appDraft, appLabel, onDraftSaved, packageId, publishNonce, locale]); // objectui#5813 — nav edits auto-save while edit mode is open. const { flush: flushNavSave, sending: navSending } = useDraftAutoSave({ // objectui#11232 — the app `doNavSave` addresses. The package is this @@ -8248,8 +8278,13 @@ export function DataPillar({ setObjVerdict(draftEnvelopeDiagnostics(read.answer)); setObjVerdictBody(extractDraftBody(read.answer) ?? undefined); }); - // No success toast: with auto-save (objectui#5813) it would fire after - // every editing pause — the quiet last-saved hint is the affordance. + // objectstack#28984 — named by the label this save stored; one toast per + // object however many editing pauses save it (`announceDraftSaved`). + announceDraftSaved( + { type: 'object', name: current.name }, + typeof body.label === 'string' && body.label ? body.label : current.label, + locale, + ); onDraftSaved?.({ type: 'object', name: current.name }); } catch (e) { setError(objectSaveRefusal(e, body, locale)); @@ -8314,6 +8349,9 @@ export function DataPillar({ setExtHasDraft(true); if (sent.unmoved()) setExtDirty(false); setSavedAt(new Date()); + // objectstack#28984 — the extension customizes the open object, so it + // is named by that object's label. + announceDraftSaved({ type: OBJECT_EXTENSION_TYPE, name }, current?.label ?? '', locale); onDraftSaved?.({ type: OBJECT_EXTENSION_TYPE, name }); } catch (e) { setError(plainRefusal(e)); @@ -8321,7 +8359,7 @@ export function DataPillar({ setSaving(false); } }, - [extOpen, customization, extPairFor, saveExtDraft, onDraftSaved], + [extOpen, customization, extPairFor, saveExtDraft, onDraftSaved, current, locale], ); const { flush: flushExtSave } = useDraftAutoSave({ target: `${OBJECT_EXTENSION_TYPE}:${current?.name ?? ''}`, @@ -10401,6 +10439,8 @@ export function AutomationsPillar({ rereadFlowVerdict(current.name, target.packageId); // objectui#11204 — clean only if nothing was edited while it was in flight. if (sent.unmoved()) setAutoDirty(false); + // objectstack#28984 — named as the rail names the flow. + announceDraftSaved({ type: 'flow', name: current.name }, current.label, locale); onDraftSaved?.({ type: 'flow', name: current.name }); } catch (e) { // objectui#11785 — read against `draft`, the body this save sent. diff --git a/ui/packages/i18n/src/namespaces/studio/en.ts b/ui/packages/i18n/src/namespaces/studio/en.ts index 08616d39ec3..c9da1326897 100644 --- a/ui/packages/i18n/src/namespaces/studio/en.ts +++ b/ui/packages/i18n/src/namespaces/studio/en.ts @@ -2085,6 +2085,8 @@ const STUDIO_EN: Record = { 'engine.studio.access.nameLabel': 'Name', 'engine.studio.access.idLabel': 'Identifier', 'engine.studio.data.savedDraft': 'Object “{label}” saved as draft', + // objectstack#28984 — a landed draft autosave, named; the voice of `engine.edit.draftDiscarded`. + 'engine.studio.draftSaved': 'Draft saved: {item}', 'engine.studio.data.lastSaved': 'Saved {time}', // Pillar tab labels 'engine.fieldType.text': 'Text', diff --git a/ui/packages/i18n/src/namespaces/studio/zh.ts b/ui/packages/i18n/src/namespaces/studio/zh.ts index febabd83767..254c8b8f17b 100644 --- a/ui/packages/i18n/src/namespaces/studio/zh.ts +++ b/ui/packages/i18n/src/namespaces/studio/zh.ts @@ -1888,6 +1888,7 @@ const STUDIO_ZH: Record = { 'engine.studio.access.nameLabel': '名称', 'engine.studio.access.idLabel': '标识', 'engine.studio.data.savedDraft': '对象「{label}」已存为草稿', + 'engine.studio.draftSaved': '已保存 {item} 的草稿', 'engine.studio.data.lastSaved': '已于 {time} 保存', // Pillar tab labels 'engine.fieldType.text': '单行文本',