Skip to content

Commit 70463a2

Browse files
authored
Merge pull request #1106 from writer/AB-273
fix(ui): avoid opening Key-Value editor in freehand mode - AB-273
2 parents db2c335 + 7475e04 commit 70463a2

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/ui/src/builder/settings/composables/useKeyValueEditor.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,50 @@ describe(useKeyValueEditor.name, () => {
136136
});
137137
});
138138
});
139+
140+
describe("initial mode", () => {
141+
it("should open record in assisted", () => {
142+
const { mode } = useKeyValueEditor({
143+
foo: "bar",
144+
});
145+
expect(mode.value).toBe("assisted");
146+
});
147+
148+
it("should open string record in assisted", () => {
149+
const { mode } = useKeyValueEditor(
150+
JSON.stringify({
151+
foo: "bar",
152+
}),
153+
);
154+
expect(mode.value).toBe("assisted");
155+
});
156+
157+
it("should open simple template in freehand", () => {
158+
const { mode } = useKeyValueEditor(
159+
JSON.stringify({
160+
foo: "@{var}",
161+
}),
162+
);
163+
expect(mode.value).toBe("freehand");
164+
});
165+
166+
it("should open full template in freehand", () => {
167+
const { mode } = useKeyValueEditor("@{var}");
168+
expect(mode.value).toBe("freehand");
169+
});
170+
171+
it("should open malformed JSON in freehand", () => {
172+
const { mode } = useKeyValueEditor("{");
173+
expect(mode.value).toBe("freehand");
174+
});
175+
176+
it("should open record in freehand", () => {
177+
const { mode } = useKeyValueEditor(
178+
JSON.stringify({
179+
foo: [1, 2],
180+
}),
181+
);
182+
expect(mode.value).toBe("freehand");
183+
});
184+
});
139185
});

src/ui/src/builder/settings/composables/useKeyValueEditor.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ function tryToParse(value: string) {
2121
}
2222
}
2323

24+
function isFlatRecordString(obj: object): obj is Record<string, string> {
25+
return Object.values(obj).every((v) => typeof v === "string");
26+
}
27+
2428
export function useKeyValueEditor(originalValue: string | JSONValue) {
2529
const getId = useId();
2630

@@ -105,11 +109,16 @@ export function useKeyValueEditor(originalValue: string | JSONValue) {
105109
}
106110

107111
function computeInitialMode(objectOrString: string | JSONValue): Mode {
108-
return typeof objectOrString === "string" &&
109-
(TEMPLATE_REGEX.exec(objectOrString) ||
110-
!isValidJSON(objectOrString))
111-
? "freehand"
112-
: "assisted";
112+
if (typeof objectOrString !== "string") {
113+
return isFlatRecordString(objectOrString) ? "assisted" : "freehand";
114+
}
115+
116+
if (TEMPLATE_REGEX.exec(objectOrString)) return "freehand";
117+
if (!isValidJSON(objectOrString)) return "freehand";
118+
119+
return isFlatRecordString(tryToParse(objectOrString))
120+
? "assisted"
121+
: "freehand";
113122
}
114123

115124
function initializeAssistedEntries(objectOrString: string | JSONValue) {

0 commit comments

Comments
 (0)