Skip to content

refactor: eliminate duplicated buildDataRows and data-table query patterns - #9

Draft
DevItaliya22 with Copilot wants to merge 2 commits into
mainfrom
copilot/refactor-duplicated-code
Draft

refactor: eliminate duplicated buildDataRows and data-table query patterns#9
DevItaliya22 with Copilot wants to merge 2 commits into
mainfrom
copilot/refactor-duplicated-code

Conversation

Copilot AI commented Mar 3, 2026

Copy link
Copy Markdown

Two blocks of logic were copied verbatim across multiple files with no abstraction. This extracts each into a single shared location.

buildDataRowsFromPayload — 4 import sheets → 1 utility

buildDataRows was copy-pasted identically into SkuImportSheet, CategoryImportSheet, BillImportSheet, and CustomerImportSheet. Extracted to lib/utils/csv-mapping.ts:

// Before: each sheet had this ~30-line useCallback duplicated
const buildDataRows = useCallback((): Record<string, unknown>[] => {
  if (!filePayload) return [];
  if (filePayload.type === "csv") { /* ... */ }
  if (filePayload.type === "excel") { /* ... */ }
  return [];
}, [filePayload]);

// After: one shared utility, called directly
const dataRows = buildDataRowsFromPayload(filePayload);

FileLoadedPayload was also moved from the React component into csv-mapping.ts (no React deps) and re-exported for backward compat.

useDataTableQuery — 5 query hooks → 1 generic hook

useCustomersQuery, useSkusQuery, useBillsQuery, useCategoriesQuery, and useCampaignsQuery all had the same body: read search params → getDataTableConfigbuildApiParamsuseQuery. Extracted to hooks/useDataTableQuery.ts:

// New shared hook
export function useDataTableQuery<TResponse>(
  tableId: DataTableConfigId,
  queryKey: string,
  endpoint: string,
) { /* single implementation */ }

// Each entity hook is now a one-liner
export function useCustomersQuery() {
  return useDataTableQuery<CustomersResponse>("customers", "customers", "/api/customers");
}

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel

vercel Bot commented Mar 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
reelo-landing Ready Ready Preview, Comment Mar 3, 2026 11:03am
reelo-web Ready Ready Preview, Comment Mar 3, 2026 11:03am

- Add FileLoadedPayload type to csv-mapping.ts utility (moved from CsvHeaderMapping component, re-exported for compat)
- Add buildDataRowsFromPayload() to csv-mapping.ts, eliminating identical buildDataRows useCallback in SkuImportSheet, CategoryImportSheet, BillImportSheet, CustomerImportSheet
- Create hooks/useDataTableQuery.ts with generic useDataTableQuery<TResponse> hook
- Simplify useCustomersQuery, useSkusQuery, useBillsQuery, useCategoriesQuery, useCampaignsQuery to delegate to useDataTableQuery

Co-authored-by: DevItaliya22 <132957942+DevItaliya22@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code for improved maintainability refactor: eliminate duplicated buildDataRows and data-table query patterns Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants