@cosmicdrift/kumiko-renderer 0.413.0 → 0.413.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ export type TrackFormUpload = <T>(upload: () => Promise<T>) => Promise<T>;
2
+ export declare const FormUploadTrackerProvider: import("react").Provider<TrackFormUpload>;
3
+ export declare function useTrackFormUpload(): TrackFormUpload;
4
+ export declare function useFormPendingUploadsHost(): {
5
+ pendingUploads: number;
6
+ pendingUploadsRef: import("react").RefObject<number>;
7
+ trackUpload: TrackFormUpload;
8
+ };
@@ -0,0 +1,27 @@
1
+ import { createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
2
+ const runUntracked = (upload) => upload();
3
+ const FormUploadTrackerContext = createContext(runUntracked);
4
+ export const FormUploadTrackerProvider = FormUploadTrackerContext.Provider;
5
+ // A widget registers its in-flight upload here so the surrounding form can hold
6
+ // back submit until the file id has reached the form value. Outside a form the
7
+ // upload simply runs.
8
+ export function useTrackFormUpload() {
9
+ return useContext(FormUploadTrackerContext);
10
+ }
11
+ export function useFormPendingUploadsHost() {
12
+ const [pendingUploads, setPendingUploads] = useState(0);
13
+ // Mirror for handleSubmit: it must see a running upload before React re-renders.
14
+ const pendingUploadsRef = useRef(0);
15
+ const trackUpload = useCallback(async (upload) => {
16
+ pendingUploadsRef.current += 1;
17
+ setPendingUploads(pendingUploadsRef.current);
18
+ try {
19
+ return await upload();
20
+ }
21
+ finally {
22
+ pendingUploadsRef.current -= 1;
23
+ setPendingUploads(pendingUploadsRef.current);
24
+ }
25
+ }, []);
26
+ return useMemo(() => ({ pendingUploads, pendingUploadsRef, trackUpload }), [pendingUploads, trackUpload]);
27
+ }
@@ -2,6 +2,9 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
2
2
  import { FOOTER_ACTION_ROLE_PROP, NARROW_LABEL_PROP, STICKY_PRIMARY_ACTION_PROP, } from "../primitives.js";
3
3
  import { EditSlotMount } from "./edit-mounts.js";
4
4
  import { isSubmitBlockedByLayoutCondition, resolveExtensionEntityId } from "./render-edit-logic.js";
5
+ function isSubmitHeldBack(state) {
6
+ return state.isSubmitting || state.pendingUploads > 0;
7
+ }
5
8
  function renderWizardBackButton({ engine: { state, nav }, footer, }) {
6
9
  const { Button } = state.primitives;
7
10
  return (footer.showWizardBack && (_jsx(Button, { type: "button", variant: "secondary", icon: "chevron-left", onClick: nav.handleWizardBack, testId: "render-edit-wizard-back", [FOOTER_ACTION_ROLE_PROP]: "back", children: state.translate("kumiko.actions.back") })));
@@ -16,7 +19,7 @@ function renderSaveAndCloseButton({ engine: { props, state, steps, handleSubmit
16
19
  return (state.isWizard &&
17
20
  (!steps.isLastWizardStep || saveAndCloseOnCreate) &&
18
21
  footer.showsSubmit &&
19
- (!state.isCreateMode || saveAndCloseOnCreate) && (_jsx(Button, { type: "button", variant: "ghost", disabled: state.isSubmitting || props.disabled, onClick: () => void handleSubmit({ saveNow: true }), testId: "render-edit-wizard-save-close", [STICKY_PRIMARY_ACTION_PROP]: true, children: state.translate("kumiko.wizard.save-and-close") })));
22
+ (!state.isCreateMode || saveAndCloseOnCreate) && (_jsx(Button, { type: "button", variant: "ghost", disabled: isSubmitHeldBack(state) || props.disabled, onClick: () => void handleSubmit({ saveNow: true }), testId: "render-edit-wizard-save-close", [STICKY_PRIMARY_ACTION_PROP]: true, children: state.translate("kumiko.wizard.save-and-close") })));
20
23
  }
21
24
  function renderWizardNextButton({ engine: { state }, footer, }) {
22
25
  const { Button } = state.primitives;
@@ -29,7 +32,7 @@ function renderWizardNextButton({ engine: { state }, footer, }) {
29
32
  function renderFooterActionButtons({ engine: { props, state, form, handleSubmit }, footer, }) {
30
33
  const { Button } = state.primitives;
31
34
  return (footer.showSubmit &&
32
- props.footerActions?.map((action) => (_jsx(Button, { type: "button", variant: action.variant ?? "secondary", ...(action.icon !== undefined && { icon: action.icon }), disabled: state.isSubmitting || props.disabled, onClick: () => {
35
+ props.footerActions?.map((action) => (_jsx(Button, { type: "button", variant: action.variant ?? "secondary", ...(action.icon !== undefined && { icon: action.icon }), disabled: isSubmitHeldBack(state) || props.disabled, onClick: () => {
33
36
  form.controller.setValues(action.patch); // @cast-boundary form-values
34
37
  void handleSubmit();
35
38
  }, testId: `render-edit-footer-action-${action.id}`, children: state.translate(action.label) }, action.id))));
@@ -49,7 +52,7 @@ function isSubmitDisabled({ engine: { props, state, form }, footer, }) {
49
52
  !state.insideDrawer &&
50
53
  props.submitWithoutChanges !== true;
51
54
  return (isPristine ||
52
- state.isSubmitting ||
55
+ isSubmitHeldBack(state) ||
53
56
  props.disabled ||
54
57
  props.submitBlocked ||
55
58
  isSubmitBlockedByLayoutCondition(props.screen.layout, form.snapshot.values));
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { ExtensionFormRegistryProvider } from "../app/extension-form-submit.js";
3
+ import { FormUploadTrackerProvider } from "./form-pending-uploads.js";
3
4
  import { renderActionErrorBanner, renderAllRequiredHint, renderDeleteDialog, renderDraftPicker, renderExtensionErrorBanner, renderFormErrorBanner, renderRootIssueBanners, renderTabStrip, renderWizardProgress, } from "./render-edit-banners.js";
4
5
  import { renderFormActions } from "./render-edit-form-actions.js";
5
6
  import { buildHeaderActions } from "./render-edit-header-actions.js";
@@ -71,5 +72,5 @@ export function renderEditLayout(model) {
71
72
  const { props, state, sections, handleSubmit } = model.engine;
72
73
  const header = buildHeaderActions(model);
73
74
  const { Form } = state.primitives;
74
- return (_jsxs(ExtensionFormRegistryProvider, { value: state.extensionFormRegistry, children: [state.menuActionConfirmDialog, renderPageHeader(model, header), _jsx(Form, { onSubmit: () => void handleSubmit(), ...(state.focusFirstErrorKey > 0 && { focusFirstErrorKey: state.focusFirstErrorKey }), ...buildFormHeadingProps(model), ...buildFormActionProps(model, header), testId: "render-edit-form", stickyActions: state.isWizard || props.fillScreenHeight === true, ...buildFormChromeProps(model, header), children: _jsxs(AllFieldsRequiredProvider, { value: sections.showsAllRequiredHint || props.screen.layout.hideRequiredMarker === true, children: [props.leadContent, sections.showsAllRequiredHint && !state.insideDrawer && renderAllRequiredHint(model), renderDraftPicker(model), renderWizardProgress(model), state.isTabs && state.isStepped && renderTabStrip(model), renderEditSections(model), renderFormErrorBanner(model), renderRootIssueBanners(model), renderExtensionErrorBanner(model), renderActionErrorBanner(model), renderDeleteDialog(model)] }) })] }));
75
+ return (_jsx(ExtensionFormRegistryProvider, { value: state.extensionFormRegistry, children: _jsxs(FormUploadTrackerProvider, { value: state.trackUpload, children: [state.menuActionConfirmDialog, renderPageHeader(model, header), _jsx(Form, { onSubmit: () => void handleSubmit(), ...(state.focusFirstErrorKey > 0 && { focusFirstErrorKey: state.focusFirstErrorKey }), ...buildFormHeadingProps(model), ...buildFormActionProps(model, header), testId: "render-edit-form", stickyActions: state.isWizard || props.fillScreenHeight === true, ...buildFormChromeProps(model, header), children: _jsxs(AllFieldsRequiredProvider, { value: sections.showsAllRequiredHint || props.screen.layout.hideRequiredMarker === true, children: [props.leadContent, sections.showsAllRequiredHint && !state.insideDrawer && renderAllRequiredHint(model), renderDraftPicker(model), renderWizardProgress(model), state.isTabs && state.isStepped && renderTabStrip(model), renderEditSections(model), renderFormErrorBanner(model), renderRootIssueBanners(model), renderExtensionErrorBanner(model), renderActionErrorBanner(model), renderDeleteDialog(model)] }) })] }) }));
75
76
  }
@@ -77,7 +77,8 @@ export function createHandleSubmit(engine) {
77
77
  return (props.disabled === true ||
78
78
  props.submitBlocked === true ||
79
79
  isSubmitBlockedByLayoutCondition(props.screen.layout, controller.getSnapshot().values) ||
80
- isSubmittingRef.current);
80
+ isSubmittingRef.current ||
81
+ state.pendingUploadsRef.current > 0);
81
82
  }
82
83
  // Awaits only where the original did: a validation-blocked customSubmit
83
84
  // must settle in the same tick so setIsSubmitting(true/false) batch.
@@ -3,16 +3,19 @@ import { useUnsavedInputSource } from "../leave-guard.js";
3
3
  import { useWizardFormDraftRestore } from "./use-wizard-form-draft.js";
4
4
  export function useRenderEditControlledSync({ props, state, form, draft, }) {
5
5
  const { controller, snapshot } = form;
6
- const { extensionDirty } = state;
6
+ const { extensionDirty, pendingUploads } = state;
7
7
  const onDirtyChangeRef = useRef(props.onDirtyChange);
8
8
  onDirtyChangeRef.current = props.onDirtyChange;
9
- const hasUnsavedInput = snapshot.isDirty || extensionDirty;
9
+ const hasUnsavedInput = snapshot.isDirty || extensionDirty || pendingUploads > 0;
10
10
  useEffect(() => {
11
11
  onDirtyChangeRef.current?.(hasUnsavedInput);
12
12
  }, [hasUnsavedInput]);
13
13
  // Draft wizards keep their input server-side, so leaving loses nothing.
14
14
  const persistsDraft = state.isWizard && props.screen.layout.draft === true;
15
- useUnsavedInputSource(() => !persistsDraft && (controller.getSnapshot().isDirty || state.extensionDirtyRef.current));
15
+ useUnsavedInputSource(() => !persistsDraft &&
16
+ (controller.getSnapshot().isDirty ||
17
+ state.extensionDirtyRef.current ||
18
+ state.pendingUploadsRef.current > 0));
16
19
  useWizardFormDraftRestore({
17
20
  screenId: props.screen.id,
18
21
  isCreateMode: state.isCreateMode,
@@ -55,6 +55,9 @@ export declare function useRenderEditState({ screen, entityId, translate: transl
55
55
  didListRef: import("react").RefObject<boolean>;
56
56
  draftKey: string | undefined;
57
57
  };
58
+ pendingUploads: number;
59
+ pendingUploadsRef: import("react").RefObject<number>;
60
+ trackUpload: import("./form-pending-uploads.tsx").TrackFormUpload;
58
61
  primitives: import("../primitives.tsx").PrimitivesRegistry;
59
62
  menuActionToItem: (action: import("./render-edit-types.ts").RenderEditAction) => import("../primitives.tsx").ActionMenuItemSpec;
60
63
  menuActionConfirmDialog: import("react").ReactNode;
@@ -6,6 +6,7 @@ import { useLocale, useTranslation } from "../i18n.js";
6
6
  import { useInsideDrawer } from "../inside-drawer.js";
7
7
  import { usePageHeaderCompact, usePageHeaderSlotAvailable } from "../page-header-slot.js";
8
8
  import { usePrimitives } from "../primitives.js";
9
+ import { useFormPendingUploadsHost } from "./form-pending-uploads.js";
9
10
  import { useActionMenuItems } from "./use-action-menu-items.js";
10
11
  import { useWizardFormDraftState } from "./use-wizard-form-draft.js";
11
12
  function useSubmitFeedbackState() {
@@ -124,6 +125,7 @@ export function useRenderEditState({ screen, entityId, translate: translateProp,
124
125
  draftStorage,
125
126
  });
126
127
  const extensions = useExtensionComposition();
128
+ const uploads = useFormPendingUploadsHost();
127
129
  const primitives = usePrimitives();
128
130
  const { toMenuItem: menuActionToItem, confirmDialog: menuActionConfirmDialog } = useActionMenuItems({ Dialog: primitives.Dialog, onError: feedback.setActionError });
129
131
  const pageHeaderSlotAvailable = usePageHeaderSlotAvailable();
@@ -149,6 +151,9 @@ export function useRenderEditState({ screen, entityId, translate: translateProp,
149
151
  ...stepState,
150
152
  draftState,
151
153
  ...extensions,
154
+ pendingUploads: uploads.pendingUploads,
155
+ pendingUploadsRef: uploads.pendingUploadsRef,
156
+ trackUpload: uploads.trackUpload,
152
157
  primitives,
153
158
  menuActionToItem,
154
159
  menuActionConfirmDialog,
package/dist/index.d.ts CHANGED
@@ -44,6 +44,7 @@ export { evalVisibleWhen, type VisibleWhenVerdict } from "./app/visible-when.ts"
44
44
  export { dispatcherErrorText, WriteFailedError } from "./app/write-failed-error.ts";
45
45
  export type { ErrorBoundaryFallbackProps, ErrorBoundaryProps, } from "./components/error-boundary.tsx";
46
46
  export { ErrorBoundary } from "./components/error-boundary.tsx";
47
+ export { useTrackFormUpload } from "./components/form-pending-uploads.tsx";
47
48
  export type { FormScreenLayoutProps } from "./components/form-screen-layout.tsx";
48
49
  export { FormScreenLayout } from "./components/form-screen-layout.tsx";
49
50
  export { RelatedListSection } from "./components/related-list-section.tsx";
package/dist/index.js CHANGED
@@ -37,6 +37,7 @@ export { VariableChips } from "./app/variable-chips.js";
37
37
  export { evalVisibleWhen } from "./app/visible-when.js";
38
38
  export { dispatcherErrorText, WriteFailedError } from "./app/write-failed-error.js";
39
39
  export { ErrorBoundary } from "./components/error-boundary.js";
40
+ export { useTrackFormUpload } from "./components/form-pending-uploads.js";
40
41
  export { FormScreenLayout } from "./components/form-screen-layout.js";
41
42
  export { RelatedListSection } from "./components/related-list-section.js";
42
43
  export { RenderEdit } from "./components/render-edit.js";
@@ -5,7 +5,7 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
5
5
  import { Loader2, Plus, X } from "lucide-react";
6
6
  import { useRef, useState } from "react";
7
7
  import { fileIdsOf } from "../../file-ids-of.js";
8
- import { useSessionEndedSignal, useTranslation } from "../../index.js";
8
+ import { useSessionEndedSignal, useTrackFormUpload, useTranslation } from "../../index.js";
9
9
  import { toAcceptAttr } from "../lib/accept-attr.js";
10
10
  import { cn } from "../lib/cn.js";
11
11
  import { resizeImageBeforeUpload } from "../lib/resize-image.js";
@@ -15,6 +15,7 @@ import { FileNameLink, FileThumbnail } from "./file-thumbnail.js";
15
15
  export function EmbeddedFileCell({ cellId, label, column, value, readOnly, uploadTarget, onChange, }) {
16
16
  const t = useTranslation();
17
17
  const sessionEnded = useSessionEndedSignal();
18
+ const trackFormUpload = useTrackFormUpload();
18
19
  const inputRef = useRef(null);
19
20
  const [uploading, setUploading] = useState(false);
20
21
  const [error, setError] = useState(null);
@@ -31,9 +32,11 @@ export function EmbeddedFileCell({ cellId, label, column, value, readOnly, uploa
31
32
  setUploading(true);
32
33
  setError(null);
33
34
  try {
34
- const uploadFile = await resizeImageBeforeUpload(file);
35
- const id = await uploadFileRef(uploadFile, { ...uploadTarget, subField: column.field });
36
- onChange([...ids, id]);
35
+ await trackFormUpload(async () => {
36
+ const uploadFile = await resizeImageBeforeUpload(file);
37
+ const id = await uploadFileRef(uploadFile, { ...uploadTarget, subField: column.field });
38
+ onChange([...ids, id]);
39
+ });
37
40
  }
38
41
  catch (cause) {
39
42
  if (isSessionEndedUploadError(cause))
@@ -6,7 +6,7 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
6
6
  // file name link. A disabled input shows the value only, without controls.
7
7
  import { ImageIcon, Loader2, Upload } from "lucide-react";
8
8
  import { useRef, useState } from "react";
9
- import { useSessionEndedSignal, useTranslation } from "../../index.js";
9
+ import { useSessionEndedSignal, useTrackFormUpload, useTranslation } from "../../index.js";
10
10
  import { toAcceptAttr } from "../lib/accept-attr.js";
11
11
  import { resizeImageBeforeUpload } from "../lib/resize-image.js";
12
12
  import { uploadErrorKey } from "../lib/upload-error-key.js";
@@ -18,6 +18,7 @@ const DIRECTORY_INPUT_ATTRS = { webkitdirectory: "", multiple: true };
18
18
  export function FileUploadInput({ kind, id, value, onChange, accept, disabled, entityType, uploadActionForm, entityId, fieldName, subField, imageVariant, capture, directory, onFilesPicked, pickLabel, testId, }) {
19
19
  const t = useTranslation();
20
20
  const sessionEnded = useSessionEndedSignal();
21
+ const trackFormUpload = useTrackFormUpload();
21
22
  const inputRef = useRef(null);
22
23
  const [uploading, setUploading] = useState(false);
23
24
  const [error, setError] = useState(null);
@@ -34,14 +35,16 @@ export function FileUploadInput({ kind, id, value, onChange, accept, disabled, e
34
35
  setUploading(true);
35
36
  setError(null);
36
37
  try {
37
- const uploadFile = await resizeImageBeforeUpload(file);
38
- onChange(await uploadFileRef(uploadFile, {
39
- entityType,
40
- entityId,
41
- fieldName,
42
- subField,
43
- actionForm: uploadActionForm,
44
- }));
38
+ await trackFormUpload(async () => {
39
+ const uploadFile = await resizeImageBeforeUpload(file);
40
+ onChange(await uploadFileRef(uploadFile, {
41
+ entityType,
42
+ entityId,
43
+ fieldName,
44
+ subField,
45
+ actionForm: uploadActionForm,
46
+ }));
47
+ });
45
48
  }
46
49
  catch (cause) {
47
50
  if (isSessionEndedUploadError(cause))
@@ -3,13 +3,14 @@ import { jsx as _jsx } from "react/jsx-runtime";
3
3
  // file, the field value is the array of FileRef ids of the files that uploaded. Failed files stay
4
4
  // out of the value; removing a row removes its id.
5
5
  import { useEffect, useRef } from "react";
6
- import { useSessionEndedSignal, useTranslation } from "../../index.js";
6
+ import { useSessionEndedSignal, useTrackFormUpload, useTranslation } from "../../index.js";
7
7
  import { FileCountLimitError, uploadErrorKey } from "../lib/upload-error-key.js";
8
8
  import { isSessionEndedUploadError, uploadFileRef, } from "../lib/upload-file-ref.js";
9
9
  import { UploadZone } from "../widgets/upload-zone.js";
10
10
  export function FilesDropzoneInput({ id, value, onChange, accept, maxSize, maxCount, disabled, entityType, uploadActionForm, entityId, fieldName, testId, }) {
11
11
  const t = useTranslation();
12
12
  const sessionEnded = useSessionEndedSignal();
13
+ const trackFormUpload = useTrackFormUpload();
13
14
  // Uploads of one batch finish independently; building each next value from the render's
14
15
  // `value` would drop the ids of the others. Re-synced whenever the owner changes the value.
15
16
  const latestIds = useRef(value);
@@ -29,10 +30,12 @@ export function FilesDropzoneInput({ id, value, onChange, accept, maxSize, maxCo
29
30
  }
30
31
  uploadsRunning.current += 1;
31
32
  try {
32
- const fileId = await uploadFileRef(file, target);
33
- latestIds.current = [...latestIds.current, fileId];
34
- onChange(latestIds.current);
35
- return fileId;
33
+ return await trackFormUpload(async () => {
34
+ const fileId = await uploadFileRef(file, target);
35
+ latestIds.current = [...latestIds.current, fileId];
36
+ onChange(latestIds.current);
37
+ return fileId;
38
+ });
36
39
  }
37
40
  catch (cause) {
38
41
  if (isSessionEndedUploadError(cause))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-renderer",
3
- "version": "0.413.0",
3
+ "version": "0.413.1",
4
4
  "description": "React renderer for Kumiko screens. The root export holds the platform-agnostic logic (primitives contract, hooks, KumikoScreen, navigation and SSE abstractions); ./headless the DOM-free UI logic, ./live the HTTP dispatcher, ./web the browser bindings (HTML primitives, history navigation, EventSource live events, createKumikoApp).",
5
5
  "license": "MIT",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -48,8 +48,8 @@
48
48
  }
49
49
  },
50
50
  "dependencies": {
51
- "@cosmicdrift/kumiko-framework": "0.413.0",
52
- "@cosmicdrift/kumiko-types": "0.413.0",
51
+ "@cosmicdrift/kumiko-framework": "0.413.1",
52
+ "@cosmicdrift/kumiko-types": "0.413.1",
53
53
  "@types/qrcode": "^1.5.6",
54
54
  "class-variance-authority": "^0.7.1",
55
55
  "clsx": "^2.1.1",
@@ -79,8 +79,8 @@
79
79
  }
80
80
  },
81
81
  "devDependencies": {
82
- "@cosmicdrift/kumiko-locale-de": "0.413.0",
83
- "@cosmicdrift/kumiko-testing": "0.413.0",
82
+ "@cosmicdrift/kumiko-locale-de": "0.413.1",
83
+ "@cosmicdrift/kumiko-testing": "0.413.1",
84
84
  "@tailwindcss/cli": "^4.3.3",
85
85
  "@testing-library/react": "^16.3.3",
86
86
  "@testing-library/user-event": "^14.6.8",
package/src/changes.json CHANGED
@@ -1,4 +1,10 @@
1
1
  [
2
+ {
3
+ "version": "0.413.1",
4
+ "type": "fix",
5
+ "title": "Edit forms wait for running file uploads before submitting",
6
+ "detail": "The submit button of an edit form stays disabled while a file upload (dropzone, single file or image, embedded list cell) is running, and `handleSubmit` returns early in that window, so Enter, save-and-close and draft save cannot send the form without the attachment either. Custom upload widgets can join the lock with `useTrackFormUpload()` from the renderer; outside a form it just runs the upload."
7
+ },
2
8
  {
3
9
  "version": "0.413.0",
4
10
  "type": "fix",