Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

fix(builder): remove sections emptied by item moves (#3417)

Amruth Pillai97ccb4ba06f8
diff --git a/apps/web/src/libs/resume/move-item-round-trip.test.ts b/apps/web/src/libs/resume/move-item-round-trip.test.ts
new file mode 100644
index 000000000..0586f0373
--- /dev/null
+++ b/apps/web/src/libs/resume/move-item-round-trip.test.ts
@@ -0,0 +1,186 @@
+import { describe, expect, it } from "vitest";
+import { applyPatches, enablePatches, produce, produceWithPatches } from "immer";
+import { defaultResumeData } from "@reactive-resume/schema/resume/default";
+import { moveItem } from "./move-item";
+
+const company = (id: string) => ({
+ id,
+ hidden: false,
+ company: `Company ${id}`,
+ position: `Position ${id}`,
+ location: "",
+ period: "",
+ description: "",
+ roles: [],
+ website: { url: "", label: "", inlineLink: false },
+});
+const base = () =>
+ produce(defaultResumeData, (draft) => {
+ draft.sections.experience.items = [company("1"), company("2")];
+ draft.metadata.layout.pages = [{ fullWidth: false, main: ["experience"], sidebar: [] }];
+ });
+const split = () =>
+ produce(base(), (draft) => {
+ moveItem(draft, { itemId: "2", type: "experience", target: { type: "new-page", title: "Experience" } });
+ });
+
+describe("moving the last custom-section item (#3180)", () => {
+ it("restores the original JSON after moving an experience item to a new page and back", () => {
+ const initial = base();
+ const moved = split();
+ expect(moved.metadata.layout.pages).toHaveLength(2);
+ const restored = produce(moved, (draft) => {
+ moveItem(draft, {
+ itemId: "2",
+ type: "experience",
+ customSectionId: moved.customSections[0].id,
+ target: { type: "section", sectionId: "experience" },
+ });
+ });
+ expect(restored).toEqual(initial);
+ });
+ it("preserves unrelated blank pages and pages with other section references", () => {
+ const moved = produce(split(), (draft) => {
+ draft.metadata.layout.pages.push({ fullWidth: true, main: [], sidebar: [] });
+ draft.metadata.layout.pages[1].sidebar.push("skills");
+ });
+ const restored = produce(moved, (draft) => {
+ moveItem(draft, {
+ itemId: "2",
+ type: "experience",
+ customSectionId: moved.customSections[0].id,
+ target: { type: "section", sectionId: "experience" },
+ });
+ });
+ expect(restored.customSections).toEqual([]);
+ expect(restored.metadata.layout.pages).toEqual([
+ { fullWidth: false, main: ["experience"], sidebar: [] },
+ { fullWidth: false, main: [], sidebar: ["skills"] },
+ { fullWidth: true, main: [], sidebar: [] },
+ ]);
+ });
+ it("preserves the first page when its only custom section becomes empty", () => {
+ const moved = produce(split(), (draft) => {
+ draft.metadata.layout.pages.reverse();
+ });
+ const restored = produce(moved, (draft) => {
+ moveItem(draft, {
+ itemId: "2",
+ type: "experience",
+ customSectionId: moved.customSections[0].id,
+ target: { type: "section", sectionId: "experience" },
+ });
+ });
+ expect(restored.metadata.layout.pages).toEqual([
+ { fullWidth: false, main: [], sidebar: [] },
+ { fullWidth: false, main: ["experience"], sidebar: [] },
+ ]);
+ });
+ it("inserts into the selected later page before pruning the source page", () => {
+ const moved = produce(split(), (draft) => {
+ draft.metadata.layout.pages.push({ fullWidth: true, main: [], sidebar: ["skills"] });
+ });
+ const restored = produce(moved, (draft) => {
+ moveItem(draft, {
+ itemId: "2",
+ type: "experience",
+ customSectionId: moved.customSections[0].id,
+ target: { type: "new-section", pageIndex: 2, title: "Later" },
+ });
+ });
+ expect(restored.metadata.layout.pages).toHaveLength(2);
+ expect(restored.customSections).toHaveLength(1);
+ expect(restored.customSections[0]).toMatchObject({ title: "Later", items: [company("2")] });
+ expect(restored.metadata.layout.pages[1]).toEqual({
+ fullWidth: true,
+ main: [restored.customSections[0].id],
+ sidebar: ["skills"],
+ });
+ });
+ it("keeps a custom section with hidden remaining items", () => {
+ const moved = produce(split(), (draft) => {
+ draft.customSections[0].items.push({ ...company("3"), hidden: true });
+ });
+ const restored = produce(moved, (draft) => {
+ moveItem(draft, {
+ itemId: "2",
+ type: "experience",
+ customSectionId: moved.customSections[0].id,
+ target: { type: "section", sectionId: "experience" },
+ });
+ });
+ expect(restored.customSections[0].items).toEqual([{ ...company("3"), hidden: true }]);
+ expect(restored.metadata.layout.pages).toHaveLength(2);
+ });
+ it.each([
+ { type: "section", sectionId: "missing" },
+ { type: "section", sectionId: "skills" },
+ { type: "new-section", pageIndex: 99, title: "Missing" },
+ ] as const)("leaves data intact for an invalid destination $type", (target) => {
+ const moved = split();
+ expect(
+ produce(moved, (draft) => {
+ moveItem(draft, { itemId: "2", type: "experience", customSectionId: moved.customSections[0].id, target });
+ }),
+ ).toEqual(moved);
+ });
+
+ it("keeps data intact when the source is missing or destination is the source", () => {
+ const moved = split();
+ const sourceId = moved.customSections[0].id;
+ for (const [itemId, sectionId] of [
+ ["missing", "experience"],
+ ["2", sourceId],
+ ]) {
+ expect(
+ produce(moved, (draft) => {
+ moveItem(draft, {
+ itemId,
+ type: "experience",
+ customSectionId: sourceId,
+ target: { type: "section", sectionId },
+ });
+ }),
+ ).toEqual(moved);
+ }
+ });
+ it("cleans the source sidebar while preserving unrelated blank pages when moving to a new page", () => {
+ const moved = produce(split(), (draft) => {
+ const page = draft.metadata.layout.pages[1];
+ page.sidebar = page.main;
+ page.main = [];
+ draft.metadata.layout.pages.push({ fullWidth: true, main: [], sidebar: [] });
+ });
+ const restored = produce(moved, (draft) => {
+ moveItem(draft, {
+ itemId: "2",
+ type: "experience",
+ customSectionId: moved.customSections[0].id,
+ target: { type: "new-page", title: "New destination" },
+ });
+ });
+ expect(restored.customSections).toHaveLength(1);
+ expect(restored.metadata.layout.pages).toEqual([
+ { fullWidth: false, main: ["experience"], sidebar: [] },
+ { fullWidth: true, main: [], sidebar: [] },
+ { fullWidth: false, main: [restored.customSections[0].id], sidebar: [] },
+ ]);
+ expect(restored.customSections[0].items).toEqual([company("2")]);
+ });
+ it("undo restores the custom section, layout page, and custom settings", () => {
+ enablePatches();
+ const moved = produce(split(), (draft) => {
+ Object.assign(draft.customSections[0], { title: "Custom title", icon: "star", columns: 2, keepTogether: true });
+ });
+ const [restored, , inverse] = produceWithPatches(moved, (draft) => {
+ moveItem(draft, {
+ itemId: "2",
+ type: "experience",
+ customSectionId: moved.customSections[0].id,
+ target: { type: "section", sectionId: "experience" },
+ });
+ });
+ expect(restored.customSections).toEqual([]);
+ expect(applyPatches(restored, inverse)).toEqual(moved);
+ });
+});
diff --git a/apps/web/src/libs/resume/move-item.ts b/apps/web/src/libs/resume/move-item.ts
index 50b30200b..b76f1abb4 100644
--- a/apps/web/src/libs/resume/move-item.ts
+++ b/apps/web/src/libs/resume/move-item.ts
@@ -236,3 +236,49 @@ export function createPageWithSection(
draft.customSections.push(makeCustomSection(newSectionId, type, sectionTitle, item) as WritableDraft<CustomSection>);
draft.metadata.layout.pages.push({ fullWidth: false, main: [newSectionId], sidebar: [] });
}
+
+type MoveItemInput = {
+ itemId: string;
+ type: CustomSectionType;
+ customSectionId?: string | undefined;
+ target:
+ | { type: "section"; sectionId: string }
+ | { type: "new-section"; pageIndex: number; title: string }
+ | { type: "new-page"; title: string };
+};
+
+/** Moves an item atomically, then removes only custom source sections emptied by that move. */
+export function moveItem(draft: WritableDraft<ResumeData>, input: MoveItemInput): void {
+ const { itemId, type, customSectionId, target } = input;
+ const source = customSectionId
+ ? draft.customSections.find((section) => section.id === customSectionId && section.type === type)
+ : draft.sections[type as SectionType];
+ if (!source?.items.some((item) => item.id === itemId)) return;
+
+ if (target.type === "section") {
+ if (target.sectionId === (customSectionId ?? type)) return;
+ const compatible = isStandardSectionId(target.sectionId, draft.sections)
+ ? target.sectionId === type
+ : draft.customSections.some((section) => section.id === target.sectionId && section.type === type);
+ if (!compatible) return;
+ } else if (target.type === "new-section" && !draft.metadata.layout.pages[target.pageIndex]) return;
+
+ const item = removeItemFromSource(draft, itemId, type, customSectionId);
+ if (!item) return;
+ if (target.type === "section") addItemToSection(draft, item, target.sectionId, type);
+ else if (target.type === "new-section")
+ createCustomSectionWithItem(draft, item, type, target.title, target.pageIndex);
+ else createPageWithSection(draft, item, type, target.title);
+
+ // Insert first so removing the source page cannot change the chosen destination index.
+ if (!customSectionId || source.items.length > 0) return;
+ draft.customSections = draft.customSections.filter((section) => section.id !== customSectionId);
+ draft.metadata.layout.pages = draft.metadata.layout.pages.filter((page, index) => {
+ const affected = page.main.includes(customSectionId) || page.sidebar.includes(customSectionId);
+ if (!affected) return true;
+ page.main = page.main.filter((sectionId) => sectionId !== customSectionId);
+ page.sidebar = page.sidebar.filter((sectionId) => sectionId !== customSectionId);
+ // Keep the header page and any page with other references, even if those sections are hidden.
+ return index === 0 || page.main.length > 0 || page.sidebar.length > 0;
+ });
+}
diff --git a/apps/web/src/routes/builder/$resumeId/-sidebar/left/shared/section-item.tsx b/apps/web/src/routes/builder/$resumeId/-sidebar/left/shared/section-item.tsx
index d7f053ec8..71b7567d6 100644
--- a/apps/web/src/routes/builder/$resumeId/-sidebar/left/shared/section-item.tsx
+++ b/apps/web/src/routes/builder/$resumeId/-sidebar/left/shared/section-item.tsx
@@ -40,14 +40,7 @@ import { useDialogStore } from "@/dialogs/store";
import { useCurrentResume, useUpdateResumeData } from "@/features/resume/builder/draft";
import { useConfirm } from "@/hooks/use-confirm";
import { atsFindingItemElementId } from "@/libs/resume/ats";
-import {
- addItemToSection,
- createCustomSectionWithItem,
- createPageWithSection,
- getCompatibleMoveTargets,
- getSourceSectionTitle,
- removeItemFromSource,
-} from "@/libs/resume/move-item";
+import { getCompatibleMoveTargets, getSourceSectionTitle, moveItem } from "@/libs/resume/move-item";
// ============================================================================
// MoveItemSubmenu Component
@@ -85,27 +78,36 @@ function MoveItemSubmenu({ type, item, customSectionId }: MoveItemSubmenuProps)
/** Handler: Move item to an existing section */
const handleMoveToSection = (targetSectionId: string) => {
updateResumeData((draft) => {
- const removedItem = removeItemFromSource(draft, item.id, type, customSectionId);
- if (!removedItem) return;
- addItemToSection(draft, removedItem, targetSectionId, type);
+ moveItem(draft, {
+ itemId: item.id,
+ type,
+ customSectionId,
+ target: { type: "section", sectionId: targetSectionId },
+ });
});
};
/** Handler: Create a new custom section on an existing page and move the item there */
const handleNewSectionOnPage = (pageIndex: number) => {
updateResumeData((draft) => {
- const removedItem = removeItemFromSource(draft, item.id, type, customSectionId);
- if (!removedItem) return;
- createCustomSectionWithItem(draft, removedItem, type, currentSectionTitle, pageIndex);
+ moveItem(draft, {
+ itemId: item.id,
+ type,
+ customSectionId,
+ target: { type: "new-section", title: currentSectionTitle, pageIndex },
+ });
});
};
/** Handler: Create a new page with a new custom section and move the item there */
const handleNewPage = () => {
updateResumeData((draft) => {
- const removedItem = removeItemFromSource(draft, item.id, type, customSectionId);
- if (!removedItem) return;
- createPageWithSection(draft, removedItem, type, currentSectionTitle);
+ moveItem(draft, {
+ itemId: item.id,
+ type,
+ customSectionId,
+ target: { type: "new-page", title: currentSectionTitle },
+ });
});
};