From 6719f6d7e7634af9e5249838a6d58a1ecec4acec Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 17:19:54 +0000 Subject: [PATCH 1/4] =?UTF-8?q?feat(ui):=20the=20create-package=20dialog?= =?UTF-8?q?=20offers=20=E3=80=8C=E5=90=8C=E6=97=B6=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E3=80=8D,=20ticked=20by=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QSeKYmaapyPFdBwrb3SfFp --- ...ackageFormDialog.newAppStep-36748.test.tsx | 223 +++++++++++++++++- .../metadata-admin/PackageFormDialog.tsx | 121 +++++----- ...esPage.installedAppsUpdates-34722.test.tsx | 44 +++- .../src/views/metadata-admin/PackagesPage.tsx | 19 +- ui/packages/i18n/src/namespaces/studio/en.ts | 13 +- ui/packages/i18n/src/namespaces/studio/zh.ts | 4 + 6 files changed, 345 insertions(+), 79 deletions(-) diff --git a/ui/packages/app-shell/src/views/metadata-admin/PackageFormDialog.newAppStep-36748.test.tsx b/ui/packages/app-shell/src/views/metadata-admin/PackageFormDialog.newAppStep-36748.test.tsx index 9e744c2396e..94d03b5fcf4 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/PackageFormDialog.newAppStep-36748.test.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/PackageFormDialog.newAppStep-36748.test.tsx @@ -4,9 +4,11 @@ * objectstack#36748 — `PackageFormDialog` handed a `newApp` step * (`newAppStep`, `studio-design/newAppDoor.ts`) ends with an app. * - * 1. The dialog is titled 「新建应用」/"New app" and worded for an app: a - * no-code author reads 「应用」, never 「软件包」, on this door (the - * maintainer's ruling). + * 1. (objectstack#37134) The step is offered as 「同时创建应用」 / "Also + * create an app", ticked by default, on the create-package dialog's own + * face: the app-worded dialog went with the Studio home's 「新建应用」 + * door (objectstack#36908), and the one create-package dialog now + * carries the choice. * 2. One confirm creates the package AND its app: the package `POST`, the * app's draft under the first-write pin, then that one app's publish * (objectstack#36511: a human-created item is live at once). The author @@ -21,6 +23,12 @@ * create mode, `newApp={newAppStep(client, locale)}`, and Studio on the new * package after save. * + * objectstack#37134 — the create-package dialog (`CreatePackageDialog`, the + * one every create-package door mounts) hands the dialog that step, so its + * cases below mount the REAL wrapper: ticked creates the package and its app, + * unticked the package alone, and a taken app name surfaces as above. The + * last describe pins that every door mounts that wrapper. + * * The REAL `PackageFormDialog`, `newAppStep`, metadata client and * `StudioDesignSurface` over a fetch stub that keeps the server's state: * packages, published apps and app drafts. @@ -59,7 +67,11 @@ vi.mock('../studio-design/StudioAiCopilot', () => ({ StudioChatDock: () => null vi.mock('../../preview/DraftChangesPanel', () => ({ DraftChangesPanel: () => null })); import { toast } from 'sonner'; +import { readFileSync, readdirSync, statSync } from 'node:fs'; +import { dirname, join, relative, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { PackageFormDialog } from './PackageFormDialog'; +import { CreatePackageDialog } from './PackagesPage'; import { useMetadataClient } from './useMetadata'; import { t, useMetadataLocale } from './i18n'; import { newAppStep } from '../studio-design/newAppDoor'; @@ -206,18 +218,23 @@ const nameInput = (dialog: HTMLElement) => const describeWrite = (w: Write) => `${w.method} ${w.path}`; -describe('PackageFormDialog with a new-app step: one dialog worded for an app (objectstack#36748)', () => { - it('titled 「新建应用」, its submit 「创建应用」, and no 「软件包」 anywhere on its face', async () => { +describe('PackageFormDialog with a new-app step: offered as 「同时创建应用」, ticked (objectstack#37134)', () => { + it('the create-package dialog, with 「同时创建应用」 ticked under the name', async () => { localeFixture = 'zh-CN'; renderDoor(); const dialog = await openNewAppDialog(); - expect(within(dialog).getByRole('heading', { name: '新建应用' })).toBeInTheDocument(); + expect(within(dialog).getByRole('heading', { name: '新建软件包' })).toBeInTheDocument(); + const box = within(dialog).getByRole('checkbox', { name: '同时创建应用' }); + expect(box).toHaveAttribute('data-state', 'checked'); fireEvent.change(nameInput(dialog), { target: { value: 'Visits' } }); - expect(within(dialog).getByTestId('package-form-submit')).toHaveTextContent('创建应用'); - // Advanced opened too: its rows say 「应用 ID」. - fireEvent.click(within(dialog).getByTestId('package-form-advanced-toggle')); - expect(within(dialog).getByText('应用 ID')).toBeInTheDocument(); - expect(dialog.textContent).not.toMatch(/软件包/); + expect(within(dialog).getByTestId('package-form-submit')).toHaveTextContent('创建软件包'); + }); + + it('CONTROL: without a step the dialog offers no app', async () => { + render( {}} />); + const dialog = await screen.findByRole('dialog'); + expect(within(dialog).queryByTestId('package-form-also-app')).toBeNull(); + expect(within(dialog).queryByRole('checkbox')).toBeNull(); }); }); @@ -280,3 +297,187 @@ describe('PackageFormDialog with a new-app step: one confirm creates the package expect(srv.writes).toEqual([]); }); }); + +// --------------------------------------------------------------------------- +// objectstack#37134 — the create-package dialog every door mounts +// (`CreatePackageDialog`) offers 「同时创建应用」, ticked by default. +// --------------------------------------------------------------------------- + +/** `CreatePackageDialog` as Setup → Packages mounts it: Studio on the new package after create. */ +function CreatePackageHost() { + const navigate = useNavigate(); + const [open, setOpen] = React.useState(false); + return ( + <> + + navigate(studioHomePath(id))} /> + + ); +} + +function renderCreatePackage() { + return render( + + + } /> + } /> + } /> + } /> + + , + ); +} + +const alsoAppBox = (dialog: HTMLElement) => + within(dialog).getByRole('checkbox', { name: t('engine.packages.create.alsoApp', localeFixture) }); + +async function nameAndWait(dialog: HTMLElement, name: string) { + fireEvent.change(nameInput(dialog), { target: { value: name } }); + await waitFor(() => expect(within(dialog).getByTestId('package-form-submit')).toBeEnabled()); +} + +describe('CreatePackageDialog: 「同时创建应用」 is offered, ticked by default (objectstack#37134)', () => { + it.each(['en-US', 'zh-CN'] as const)('%s: the box is there and ticked on every opening', async (locale) => { + localeFixture = locale; + renderCreatePackage(); + let dialog = await openNewAppDialog(); + const label = locale === 'zh-CN' ? '同时创建应用' : 'Also create an app'; + expect(within(dialog).getByRole('checkbox', { name: label })).toHaveAttribute('data-state', 'checked'); + // Unticked, closed and opened again: ticked again. + fireEvent.click(within(dialog).getByRole('checkbox', { name: label })); + expect(within(dialog).getByRole('checkbox', { name: label })).toHaveAttribute('data-state', 'unchecked'); + fireEvent.click(within(dialog).getByRole('button', { name: t('engine.cancel', locale) })); + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); + dialog = await openNewAppDialog(); + expect(within(dialog).getByRole('checkbox', { name: label })).toHaveAttribute('data-state', 'checked'); + }); +}); + +describe('CreatePackageDialog: ticked creates the package and its app; unticked only the package (objectstack#37134)', () => { + it('ticked: package, app draft, app publish — then the Studio header names the app', async () => { + renderCreatePackage(); + const dialog = await openNewAppDialog(); + await nameAndWait(dialog, 'Visits'); + fireEvent.click(within(dialog).getByTestId('package-form-submit')); + + await waitFor(() => expect(srv.writes).toHaveLength(3), { timeout: 8000 }); + const manifest = srv.writes[0].body?.manifest as Record; + const packageId = String(manifest.id); + const namespace = String(manifest.namespace); + expect(srv.writes.map(describeWrite)).toEqual([ + 'POST /api/v1/packages', + `PUT /api/v1/meta/app/${namespace}`, + `POST /api/v1/meta/app/${namespace}/publish`, + ]); + expect(srv.writes[1].search.get('package')).toBe(packageId); + expect(srv.apps.map((a) => a.name)).toEqual([namespace]); + expect(toast.success).toHaveBeenCalledWith('Created package “Visits” and its app.'); + expect(toast.error).not.toHaveBeenCalled(); + + // Where the wrapper lands does not change (Studio on the new package), + // and with an app the header names it. + const header = await screen.findByTestId('studio-shell-header', undefined, { timeout: 8000 }); + await waitFor( + () => expect(within(header).getByTestId('studio-back-app-name')).toHaveAttribute('data-names', 'app'), + { timeout: 8000 }, + ); + expect(within(header).getByTestId('studio-back')).toHaveTextContent(/^Back to app·Visits·Studio$/); + }); + + it('unticked: only the package is created, as before', async () => { + renderCreatePackage(); + const dialog = await openNewAppDialog(); + await nameAndWait(dialog, 'Shared parts'); + fireEvent.click(alsoAppBox(dialog)); + expect(alsoAppBox(dialog)).toHaveAttribute('data-state', 'unchecked'); + fireEvent.click(within(dialog).getByTestId('package-form-submit')); + + await waitFor(() => expect(toast.success).toHaveBeenCalledWith('Created package “Shared parts”.')); + expect(srv.writes.map(describeWrite)).toEqual(['POST /api/v1/packages']); + expect(srv.apps).toEqual([]); + expect(srv.appDrafts).toEqual([]); + // The same landing: Studio on the new package, which has no app to name. + const header = await screen.findByTestId('studio-shell-header', undefined, { timeout: 8000 }); + const packageId = String((srv.writes[0].body?.manifest as Record).id); + expect(await screen.findByTestId('studio-back-app-name', undefined, { timeout: 8000 })).toHaveAttribute( + 'data-names', + 'package', + ); + expect(header).toBeInTheDocument(); + expect(packageId).toMatch(/shared-parts$/); + }); + + it('ticked, an app name already taken refuses the confirm and writes nothing; unticked, the package goes through', async () => { + renderCreatePackage(); + const dialog = await openNewAppDialog(); + await nameAndWait(dialog, 'Visits'); + const namespace = (within(dialog).getByTestId('package-form-derived-summary').textContent ?? '').match( + /will start with (\w+?)_\./, + )?.[1]; + expect(namespace, 'the derived namespace is read off the dialog').toBeTruthy(); + srv.apps = [ + { name: namespace, label: 'Other visits' }, + { name: `${namespace}_app`, label: 'Other visits app' }, + ]; + fireEvent.click(within(dialog).getByTestId('package-form-submit')); + expect(await within(dialog).findByTestId('package-form-error')).toHaveTextContent( + `The app “Other visits” already uses the identifier “${namespace}”.`, + ); + expect(srv.writes).toEqual([]); + // The namespace it names sits behind Advanced, which opened. + expect(within(dialog).getByTestId('package-form-advanced-toggle')).toHaveAttribute('data-state', 'open'); + + // The author may keep the package and drop the app instead. + fireEvent.click(alsoAppBox(dialog)); + expect(within(dialog).queryByTestId('package-form-error')).toBeNull(); + fireEvent.click(within(dialog).getByTestId('package-form-submit')); + await waitFor(() => expect(srv.writes.map(describeWrite)).toEqual(['POST /api/v1/packages'])); + }); +}); + +// --------------------------------------------------------------------------- +// objectstack#37134 — every create-package door mounts `CreatePackageDialog`, +// so every one of them offers the box. Read off the source: a create-mode +// `PackageFormDialog` mounted anywhere else, or a door that stops mounting +// the wrapper, reds here. +// --------------------------------------------------------------------------- + +const HERE = dirname(fileURLToPath(import.meta.url)); +const SRC = resolve(HERE, '../..'); + +function sourceFiles(dir: string): string[] { + return readdirSync(dir).flatMap((entry) => { + const path = join(dir, entry); + if (statSync(path).isDirectory()) return sourceFiles(path); + return /\.tsx?$/.test(entry) && !/\.test\.tsx?$/.test(entry) ? [path] : []; + }); +} + +/** Every JSX mount of `component` in app-shell's source, as `file: `. */ +function mounts(component: string): string[] { + const tag = new RegExp(`<${component}\\b[^>]*>`, 'g'); + return sourceFiles(SRC).flatMap((file) => + [...readFileSync(file, 'utf8').matchAll(tag)].map((m) => `${relative(SRC, file)}: ${m[0].replace(/\s+/g, ' ')}`), + ); +} + +describe('every create-package door offers 「同时创建应用」 (objectstack#37134)', () => { + it('Setup → Packages (its button and the `?new=package` door) and the resource list mount CreatePackageDialog', () => { + const doors = mounts('CreatePackageDialog').map((m) => m.split(':')[0]); + expect(doors.sort()).toEqual(['views/metadata-admin/PackagesPage.tsx', 'views/metadata-admin/ResourceListPage.tsx']); + }); + + it('no create-mode PackageFormDialog is mounted outside CreatePackageDialog, and that one passes the step', () => { + const creates = mounts('PackageFormDialog').filter((m) => !/mode="(edit|view)"/.test(m)); + expect(creates).toHaveLength(1); + expect(creates[0]).toMatch(/^views\/metadata-admin\/PackagesPage\.tsx: ; /** - * The new-app door only (objectstack#36748): the app created with the - * package, or `null` when the package was created and its app was refused. + * Create mode with 「同时创建应用」 ticked only (objectstack#37134): the app + * created with the package, or `null` when the package was created and its + * app was refused. */ app?: string | null; } -/** The spec form's rows whose help line the new-app door words for an app. */ -const APP_DOOR_HELP_ROWS = ['name', 'description', 'namespace', 'version', 'type', 'defaultDatasource', 'dependencies'] as const; - -/** - * The new-app door's wording for the spec form's own rows (objectstack#36748): - * every help line names the app, Advanced's included. - */ -function withAppDoorWording(form: FormViewSpec, locale: string): FormViewSpec { - const help: Record = {}; - for (const row of APP_DOOR_HELP_ROWS) help[row] = t(`engine.studio.newApp.help.${row}`, locale); - return { - ...form, - sections: (form.sections ?? []).map((section) => ({ - ...section, - fields: section.fields?.map((f) => { - const name = fieldName(f); - if (!(name in help)) return f; - return { ...(typeof f === 'string' ? { field: f } : f), helpText: help[name] }; - }), - })), - }; -} - /** * Was this envelope's prose MARKED, i.e. did the producer address it to the end * user (`error.userMessage`, objectstack `79c46da90`) rather than to whoever is debugging? @@ -425,9 +404,10 @@ export function PackageFormDialog({ manifest?: ManifestRecord | null; onSaved?: (result: PackageSaveResult) => void; /** - * Create mode only: the Studio landing's 「新建应用」 door - * (objectstack#36748). The dialog is worded for an app, and the package's - * app is created and made live right after the package. + * Create mode only: the app step (`newAppStep`, objectstack#36748). The + * dialog offers 「同时创建应用」 (objectstack#37134), ticked by default; + * ticked, the package's app is created and made live right after the + * package. */ newApp?: NewAppStep; }) { @@ -438,21 +418,17 @@ export function PackageFormDialog({ // resident, so the form is rebuilt once it lands; keyed on the locale alone // it kept the `en` answer of its first render under a translated title. const catalogueResident = isStudioCatalogueLoaded(locale); - // objectstack#36748 — the new-app door: create mode with an app step. - const appDoor = mode === 'create' && newApp !== undefined; const form = React.useMemo( - () => { - const spec = getPackageForm(locale, { create: mode === 'create' }); - return appDoor ? withAppDoorWording(spec, locale) : spec; - }, + () => getPackageForm(locale, { create: mode === 'create' }), // eslint-disable-next-line react-hooks/exhaustive-deps -- the residency flag is the dependency: the form reads the catalogue through `t` - [locale, mode, catalogueResident, appDoor], + [locale, mode, catalogueResident], ); // objectui#11792 — the `` of a derived `com..` id. Null // outside an AuthProvider and whenever the session has no active // organization; `deriveCreatePackageId` then leaves the segment out. const orgSlug = useAuth().activeOrganization?.slug ?? null; const idInputId = React.useId(); + const alsoAppId = React.useId(); const createMode = mode === 'create'; const readOnly = mode === 'view'; @@ -475,6 +451,10 @@ export function PackageFormDialog({ // objectui#13278 — false while a dependencies row has an empty or duplicate // package name; the editor flags the row and save waits for it. const [dependenciesValid, setDependenciesValid] = React.useState(true); + // objectstack#37134 — 「同时创建应用」: ticked by default each time the + // dialog opens. Only offered in create mode with an app step. + const [alsoApp, setAlsoApp] = React.useState(true); + const offersApp = mode === 'create' && newApp !== undefined; React.useEffect(() => { if (!open) return; @@ -494,6 +474,7 @@ export function PackageFormDialog({ setBusy(false); setDependenciesValid(true); setAdvancedOpen(false); + setAlsoApp(true); }, [open, createMode, manifest]); const handleDependenciesChange = React.useCallback((dependencies: DependencyMap, valid: boolean) => { @@ -572,9 +553,7 @@ export function PackageFormDialog({ // The help line SchemaForm would have shown under the id: the localized row // when there is one, else the spec's own `.describe()` (see package-schema). - const idHelp = appDoor - ? t('engine.studio.newApp.help.id', locale) - : (tOptional('engine.packages.form.help.id', locale) ?? ManifestSchema.shape.id.description); + const idHelp = tOptional('engine.packages.form.help.id', locale) ?? ManifestSchema.shape.id.description; // Spec validation → inline issues (only where fields are editable). const issues: SchemaFormIssue[] = React.useMemo(() => { @@ -659,12 +638,14 @@ export function PackageFormDialog({ version: versionStr, type: draft.type ?? 'app', }; - // objectstack#36748 — the new-app door reserves its app's name before - // anything is written: a taken one refuses the whole confirm, and the - // namespace it is made from sits behind Advanced. + // objectstack#36748 — with 「同时创建应用」 ticked (objectstack#37134) + // the app's name is reserved before anything is written: a taken one + // refuses the whole confirm, and the namespace it is made from sits + // behind Advanced. + const appStep = offersApp && alsoApp ? newApp : undefined; let appName: string | null = null; - if (newApp) { - const reserved = await newApp.reserve(String(draft.namespace ?? '').trim()); + if (appStep) { + const reserved = await appStep.reserve(String(draft.namespace ?? '').trim()); if ('taken' in reserved) { setError(tFormat('engine.studio.newApp.nameTaken', locale, reserved.taken)); setAdvancedOpen(true); @@ -680,14 +661,14 @@ export function PackageFormDialog({ const id = String((created?.manifest?.id as string) ?? manifestBody.id); const createdName = String((created?.manifest?.name as string) ?? manifestBody.name); result = { id, mode, package: created }; - if (newApp && appName) { + if (appStep && appName) { // The package is there now, so a refused app does not keep the // author in the dialog: the toast says so and the top bar's Create // app is the way to try again. try { - await newApp.create(id, appName, createdName); + await appStep.create(id, appName, createdName); result.app = appName; - toast.success(tFormat('engine.studio.newApp.created', locale, { name: createdName })); + toast.success(tFormat('engine.packages.create.createdWithApp', locale, { name: createdName })); } catch (e) { result.app = null; toast.error( @@ -757,7 +738,7 @@ export function PackageFormDialog({ // Hence `readEnvelopeUserMessage` at the throw site and this read here. const marked: boolean = typeof e?.userMessage === 'string' && e.userMessage !== ''; if (e?.status === 409 || /already exists/i.test(msg)) { - setError(marked ? msg : t(appDoor ? 'engine.studio.newApp.exists' : 'engine.packages.create.exists', locale)); + setError(marked ? msg : t('engine.packages.create.exists', locale)); } else if (e?.status === 403 || /manage_metadata/i.test(msg)) { // objectstack#8270 — for an UNMARKED body this arm still answers // exactly as it did, and that is the whole of what objectstack-ai/objectstack#8270 measured. The @@ -778,7 +759,7 @@ export function PackageFormDialog({ } else { setError( msg || - t(appDoor ? 'engine.studio.newApp.failed' : createMode ? 'engine.packages.create.failed' : 'engine.packages.edit.failed', locale), + t(createMode ? 'engine.packages.create.failed' : 'engine.packages.edit.failed', locale), ); } } finally { @@ -816,7 +797,7 @@ export function PackageFormDialog({ const title = mode === 'create' - ? t(appDoor ? 'engine.studio.newApp.title' : 'engine.packages.create.title', locale) + ? t('engine.packages.create.title', locale) : mode === 'edit' ? t('engine.packages.edit.title', locale) : t('engine.packages.view.title', locale); @@ -828,7 +809,7 @@ export function PackageFormDialog({ {title} {mode === 'create' ? ( - {t(appDoor ? 'engine.studio.newApp.description' : 'engine.packages.create.description', locale)} + {t('engine.packages.create.description', locale)} ) : ( @@ -863,6 +844,26 @@ export function PackageFormDialog({
+ {offersApp && ( +
+ { + setAlsoApp(v === true); + setError(null); + }} + className="mt-0.5" + data-testid="package-form-also-app-checkbox" + /> +
+ +

{t('engine.packages.create.alsoAppHint', locale)}

+
+
+ )} {/* objectstack#27833 — the technical rows. The content stays mounted while closed (`forceMount` plus `hidden`) so the id @@ -882,7 +883,7 @@ export function PackageFormDialog({ {t('engine.packages.form.advanced', locale)} - {t(appDoor ? 'engine.studio.newApp.advancedHint' : 'engine.packages.create.advancedHint', locale)} + {t('engine.packages.create.advancedHint', locale)} @@ -894,7 +895,7 @@ export function PackageFormDialog({ testId="package-form-id" >