fix(mysql): open editor when creating a new event

This commit is contained in:
jearhe authored and GitHub committed 2026-08-30 00:47:21 +08:00
1 parent c24e4352e8
commit 508369ee39
9 files changed
+368 -18

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
import { describe, expect, it } from "vitest";
import { resolveInitialEventEditorRequest } from "./eventEditorRequest";
const base = { openedRequestKey: "" };
describe("resolveInitialEventEditorRequest", () => {
describe("create request (New Event)", () => {
it("opens CREATE mode without requiring an existing EVENT row", () => {
const decision = resolveInitialEventEditorRequest({ ...base, eventCreateRequestId: 7, hasEventRow: false });
expect(decision).toEqual({ type: "create", requestKey: "create:7" });
});
it("dedupes only the same request id", () => {
expect(resolveInitialEventEditorRequest({ ...base, eventCreateRequestId: 3, openedRequestKey: "create:2" })).toEqual({ type: "create", requestKey: "create:3" });
expect(resolveInitialEventEditorRequest({ openedRequestKey: "create:3", eventCreateRequestId: 3 })).toEqual({ type: "ignore" });
});
it("re-enters CREATE mode when the request id increments on a reused tab", () => {
const first = resolveInitialEventEditorRequest({ openedRequestKey: "", eventCreateRequestId: 1 });
expect(first).toEqual({ type: "create", requestKey: "create:1" });
// second click on the SAME tab with a fresh request id must open again
const second = resolveInitialEventEditorRequest({ openedRequestKey: first.type === "create" ? first.requestKey : "", eventCreateRequestId: 2 });
expect(second).toEqual({ type: "create", requestKey: "create:2" });
});
it("defers while the object list is still loading", () => {
expect(resolveInitialEventEditorRequest({ ...base, eventCreateRequestId: 1, loadingObjects: true })).toEqual({ type: "ignore" });
expect(resolveInitialEventEditorRequest({ ...base, eventCreateRequestId: 1, loadingObjects: false })).toEqual({ type: "create", requestKey: "create:1" });
});
});
describe("edit request (existing event)", () => {
it("opens the existing EVENT editor only when the row is present", () => {
expect(resolveInitialEventEditorRequest({ ...base, eventName: "foo_event", eventOpenRequestId: 2, hasEventRow: true })).toEqual({ type: "edit", requestKey: "2:foo_event" });
expect(resolveInitialEventEditorRequest({ ...base, eventName: "foo_event", eventOpenRequestId: 2, hasEventRow: false })).toEqual({ type: "ignore" });
});
it("remains ALTER-mode safe: same name + new request id reopens", () => {
const first = resolveInitialEventEditorRequest({ openedRequestKey: "", eventName: "foo_event", eventOpenRequestId: 1, hasEventRow: true });
expect(first).toEqual({ type: "edit", requestKey: "1:foo_event" });
const second = resolveInitialEventEditorRequest({ openedRequestKey: first.type === "edit" ? first.requestKey : "", eventName: "foo_event", eventOpenRequestId: 2, hasEventRow: true });
expect(second).toEqual({ type: "edit", requestKey: "2:foo_event" });
});
});
describe("plain Event list open", () => {
it("does not open any editor when there is no event intent", () => {
expect(resolveInitialEventEditorRequest({ ...base, openedRequestKey: "" })).toEqual({ type: "ignore" });
expect(resolveInitialEventEditorRequest({ ...base, openedRequestKey: "", initialObjectFilterIntent: undefined } as any)).toEqual({ type: "ignore" });
});
it("does not open CREATE mode for stale blank names", () => {
expect(resolveInitialEventEditorRequest({ ...base, eventName: " " })).toEqual({ type: "ignore" });
});
});
describe("create priority over stale edit target", () => {
it("honors a fresh create request even when an edit target lingers", () => {
const decision = resolveInitialEventEditorRequest({ ...base, eventCreateRequestId: 5, eventName: "old_event", eventOpenRequestId: 9, hasEventRow: true });
expect(decision).toEqual({ type: "create", requestKey: "create:5" });
});
});
});
@@ -0,0 +1,50 @@
/**
* Resolve which MySQL Event editor request an ObjectBrowser tab should apply.
*
* ObjectBrowser receives event-open intent through three independent props:
*
* - `eventCreateRequestId`: explicit "create a NEW event" request. It does not
* depend on the EVENT row existing in the object list, so it can open the
* MySqlEventEditor in CREATE mode even before the list is loaded.
* - `eventName` + `eventOpenRequestId`: "open/edit THIS existing event". It
* requires the EVENT row to be present in the loaded object list.
*
* The create request takes priority over an edit request so a stale edit target
* can never swallow a fresh "New Event" action. Callers (queryStore) keep the
* two states mutually exclusive, but this resolver stays defensive anyway.
*/
export type InitialEventEditorDecision = { type: "create"; requestKey: string } | { type: "edit"; requestKey: string } | { type: "ignore" };
export interface InitialEventEditorRequestInput {
/** Monotonic "New Event" request id (undefined = no create intent). */
eventCreateRequestId?: number;
/** Existing event name to edit (undefined = no edit intent). */
eventName?: string;
/** Request id that pairs with eventName for repeat open-intent. */
eventOpenRequestId?: number;
/** Request key already honored by this ObjectBrowser instance (dedupe). */
openedRequestKey: string;
/** Whether the requested event already exists in the loaded object list. */
hasEventRow?: boolean;
/** Object list is still loading; defer honoring any request. */
loadingObjects?: boolean;
}
export function resolveInitialEventEditorRequest(input: InitialEventEditorRequestInput): InitialEventEditorDecision {
const { eventCreateRequestId, eventName, eventOpenRequestId, openedRequestKey, hasEventRow = false, loadingObjects = false } = input;
// A "New Event" request opens the CREATE editor without needing an existing
// EVENT row. It is deduped by its own monotonic request id so the SAME tab
// can be asked to re-enter create mode on every click.
if (eventCreateRequestId !== undefined) {
const requestKey = `create:${eventCreateRequestId}`;
if (loadingObjects || openedRequestKey === requestKey) return { type: "ignore" };
return { type: "create", requestKey };
}
const name = eventName?.trim();
if (!name) return { type: "ignore" };
const requestKey = `${eventOpenRequestId ?? 0}:${name}`;
if (loadingObjects || openedRequestKey === requestKey || !hasEventRow) return { type: "ignore" };
return { type: "edit", requestKey };
}