Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 29, 2025

Eliminates repeated authentication checks and date formatting patterns across the repair module by extracting shared utilities.

Changes

Created src/utils/repair.ts

  • validateRepairRole() - Role validation logic
  • checkAuthAndRedirect() - Authentication with automatic login redirect
  • requireRepairRole() - Combined auth + role check

Created src/utils/date.ts

  • formatDateTime(), formatDate(), formatShortDateTime() - Standardized date formatting

Refactored 6 components to use shared utilities:

  • RepairAdmin.tsx, EventDetail.tsx, RepairHistoryCard.tsx
  • RepairHistoryPage.tsx, TicketDetail.tsx, TicketForm.tsx

Before/After

// Before: Repeated in 5 files
const authenticated = await makeLogtoClient().isAuthenticated()
if (!authenticated) {
  window.location.href = `/repair/login-hint?redirectUrl=${path}`
  return
}
const res = await makeLogtoClient().getIdTokenClaims()

// After: Single call
const userInfo = await checkAuthAndRedirect(path)
// Before: Repeated in 4 files
dayjs(event.gmtCreate).format("YYYY-MM-DD HH:mm")

// After: Consistent formatting
formatDateTime(event.gmtCreate)

Impact: -66 duplicated lines, +122 reusable utility lines across 8 files

Original prompt

Find and refactor duplicated code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Oct 29, 2025

Deploying home with  Cloudflare Pages  Cloudflare Pages

Latest commit: f1b082b
Status:🚫  Build failed.

View logs

Co-authored-by: wen-templari <52404670+wen-templari@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code for better maintainability Refactor duplicated authentication and date formatting logic Oct 29, 2025
Copilot AI requested a review from wen-templari October 29, 2025 03:09
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