refactor(web): finding 7 - extract makeCustomSection, derive isStandard

Extract makeCustomSection factory to eliminate duplicate CustomSection
object literal. Replace hand-maintained SectionType array in
isStandardSectionId with membership check via `id in sections`.

Claude-Session: https://claude.ai/code/session_012Bnvt1MghwHj4qQRxuQUGa
This commit is contained in:
Amruth Pillai
2026-07-04 21:29:20 +02:00
parent f5ec471318
commit 27efeab796
+23 -72
View File
@@ -31,27 +31,9 @@ type MoveTargetPage = {
// Helper Functions
// ============================================================================
/**
* Checks if a section ID belongs to a standard section.
* Standard sections have predefined keys like "experience", "education", etc.
*/
function isStandardSectionId(sectionId: string): sectionId is SectionType {
const standardSections: SectionType[] = [
"profiles",
"experience",
"education",
"projects",
"skills",
"languages",
"interests",
"awards",
"certifications",
"publications",
"volunteer",
"references",
];
return standardSections.includes(sectionId as SectionType);
// ponytail: derive standard section membership from data instead of a hand-maintained list
function isStandardSectionId(sectionId: string, sections: Record<string, unknown>): sectionId is SectionType {
return sectionId in sections;
}
// ============================================================================
@@ -111,7 +93,7 @@ export function getCompatibleMoveTargets(
}
// Check if it's a standard section with matching type
if (isStandardSectionId(sectionId) && sectionId === sourceType) {
if (isStandardSectionId(sectionId, resumeData.sections) && sectionId === sourceType) {
compatibleSections.push({
sectionId,
sectionTitle: getDefaultSectionTitle(sectionId),
@@ -189,7 +171,7 @@ export function addItemToSection(
type: CustomSectionType,
): void {
// Check if target is a standard section
if (isStandardSectionId(targetSectionId) && targetSectionId === type) {
if (isStandardSectionId(targetSectionId, draft.sections) && targetSectionId === type) {
const section = draft.sections[type as SectionType];
if ("items" in section) {
section.items.push(item as never);
@@ -214,6 +196,20 @@ export function addItemToSection(
* @param targetPageIndex - The page index to add the section to
* @returns The ID of the newly created custom section
*/
function makeCustomSection(id: string, type: CustomSectionType, title: string, item: SectionItem): CustomSection {
return {
id,
type,
title,
icon: "",
columns: 1,
hidden: false,
keepTogether: false,
startOnNewPage: false,
items: [item as never],
};
}
export function createCustomSectionWithItem(
draft: WritableDraft<ResumeData>,
item: SectionItem,
@@ -222,39 +218,14 @@ export function createCustomSectionWithItem(
targetPageIndex: number,
): string {
const newSectionId = generateId();
draft.customSections.push(makeCustomSection(newSectionId, type, sectionTitle, item) as WritableDraft<CustomSection>);
// Create the new custom section
const newSection: CustomSection = {
id: newSectionId,
type,
title: sectionTitle,
icon: "",
columns: 1,
hidden: false,
keepTogether: false,
startOnNewPage: false,
items: [item as never],
};
draft.customSections.push(newSection as WritableDraft<CustomSection>);
// Add the section to the target page's main column
const page = draft.metadata.layout.pages[targetPageIndex];
if (page) {
page.main.push(newSectionId);
}
if (page) page.main.push(newSectionId);
return newSectionId;
}
/**
* Creates a new page with a custom section containing the given item.
*
* @param draft - The immer draft of resume data
* @param item - The item to add to the new section
* @param type - The section type for the new custom section
* @param sectionTitle - The title for the new custom section
*/
export function createPageWithSection(
draft: WritableDraft<ResumeData>,
item: SectionItem,
@@ -262,26 +233,6 @@ export function createPageWithSection(
sectionTitle: string,
): void {
const newSectionId = generateId();
// Create the new custom section
const newSection: CustomSection = {
id: newSectionId,
type,
title: sectionTitle,
icon: "",
columns: 1,
hidden: false,
keepTogether: false,
startOnNewPage: false,
items: [item as never],
};
draft.customSections.push(newSection as WritableDraft<CustomSection>);
// Create the new page with the section in the main column
draft.metadata.layout.pages.push({
fullWidth: false,
main: [newSectionId],
sidebar: [],
});
draft.customSections.push(makeCustomSection(newSectionId, type, sectionTitle, item) as WritableDraft<CustomSection>);
draft.metadata.layout.pages.push({ fullWidth: false, main: [newSectionId], sidebar: [] });
}