From 17752e001a9bd23f96ff02e38f416d65f588298f Mon Sep 17 00:00:00 2001 From: DaKheera47 Date: Mon, 19 Jan 2026 19:50:37 +0000 Subject: [PATCH] reduce duplicated code --- .../src/client/components/DiscoveredPanel.tsx | 17 +-------- orchestrator/src/client/lib/dateUtils.ts | 36 ++++++++++++++++++ .../src/client/pages/OrchestratorPage.tsx | 38 +------------------ .../src/client/pages/UkVisaJobsPage.tsx | 38 +------------------ 4 files changed, 39 insertions(+), 90 deletions(-) create mode 100644 orchestrator/src/client/lib/dateUtils.ts diff --git a/orchestrator/src/client/components/DiscoveredPanel.tsx b/orchestrator/src/client/components/DiscoveredPanel.tsx index 487c974..80f8cc3 100644 --- a/orchestrator/src/client/components/DiscoveredPanel.tsx +++ b/orchestrator/src/client/components/DiscoveredPanel.tsx @@ -29,6 +29,7 @@ import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Separator } from "@/components/ui/separator"; import { cn } from "@/lib/utils"; +import { formatDate } from "../lib/dateUtils"; import * as api from "../api"; import { FitAssessment } from "."; import type { Job, ResumeProjectCatalogItem } from "../../shared/types"; @@ -49,22 +50,6 @@ interface DiscoveredPanelProps { // Helpers // ───────────────────────────────────────────────────────────────────────────── -const formatDate = (dateStr: string | null) => { - if (!dateStr) return null; - try { - const normalized = dateStr.includes("T") ? dateStr : dateStr.replace(" ", "T"); - const parsed = new Date(normalized); - if (Number.isNaN(parsed.getTime())) return dateStr; - return parsed.toLocaleDateString("en-GB", { - day: "numeric", - month: "short", - year: "numeric", - }); - } catch { - return dateStr; - } -}; - const stripHtml = (value: string) => value .replace(/<[^>]*>/g, " ") diff --git a/orchestrator/src/client/lib/dateUtils.ts b/orchestrator/src/client/lib/dateUtils.ts new file mode 100644 index 0000000..4154e22 --- /dev/null +++ b/orchestrator/src/client/lib/dateUtils.ts @@ -0,0 +1,36 @@ +export const formatDate = (dateStr?: string | null) => { + if (!dateStr) return null; + try { + const normalized = dateStr.includes("T") ? dateStr : dateStr.replace(" ", "T"); + const parsed = new Date(normalized); + if (Number.isNaN(parsed.getTime())) return dateStr; + return parsed.toLocaleDateString("en-GB", { + day: "numeric", + month: "short", + year: "numeric", + }); + } catch { + return dateStr; + } +}; + +export const formatDateTime = (dateStr?: string | null) => { + if (!dateStr) return null; + try { + const normalized = dateStr.includes("T") ? dateStr : dateStr.replace(" ", "T"); + const parsed = new Date(normalized); + if (Number.isNaN(parsed.getTime())) return dateStr; + const date = parsed.toLocaleDateString("en-GB", { + day: "numeric", + month: "short", + year: "numeric", + }); + const time = parsed.toLocaleTimeString("en-GB", { + hour: "2-digit", + minute: "2-digit", + }); + return `${date} ${time}`; + } catch { + return dateStr; + } +}; diff --git a/orchestrator/src/client/pages/OrchestratorPage.tsx b/orchestrator/src/client/pages/OrchestratorPage.tsx index c4db118..317feb3 100644 --- a/orchestrator/src/client/pages/OrchestratorPage.tsx +++ b/orchestrator/src/client/pages/OrchestratorPage.tsx @@ -60,6 +60,7 @@ import { } from "@/components/ui/sheet"; import { cn } from "@/lib/utils"; import { copyTextToClipboard, formatJobForWebhook } from "@client/lib/jobCopy"; +import { formatDate, formatDateTime } from "../lib/dateUtils"; import { PipelineProgress, DiscoveredPanel, ManualImportSheet } from "../components"; import { ReadyPanel } from "../components/ReadyPanel"; import * as api from "../api"; @@ -155,43 +156,6 @@ const emptyStateCopy: Record = { all: "No jobs in the system yet. Run the pipeline to get started.", }; -const formatDate = (dateStr: string | null) => { - if (!dateStr) return null; - try { - const normalized = dateStr.includes("T") ? dateStr : dateStr.replace(" ", "T"); - const parsed = new Date(normalized); - if (Number.isNaN(parsed.getTime())) return dateStr; - return parsed.toLocaleDateString("en-GB", { - day: "numeric", - month: "short", - year: "numeric", - }); - } catch { - return dateStr; - } -}; - -const formatDateTime = (dateStr: string | null) => { - if (!dateStr) return null; - try { - const normalized = dateStr.includes("T") ? dateStr : dateStr.replace(" ", "T"); - const parsed = new Date(normalized); - if (Number.isNaN(parsed.getTime())) return dateStr; - const date = parsed.toLocaleDateString("en-GB", { - day: "numeric", - month: "short", - year: "numeric", - }); - const time = parsed.toLocaleTimeString("en-GB", { - hour: "2-digit", - minute: "2-digit", - }); - return `${date} ${time}`; - } catch { - return dateStr; - } -}; - const safeFilenamePart = (value: string) => value.replace(/[^a-z0-9]/gi, "_"); const dateValue = (value: string | null) => { diff --git a/orchestrator/src/client/pages/UkVisaJobsPage.tsx b/orchestrator/src/client/pages/UkVisaJobsPage.tsx index f2c88a7..5bb8667 100644 --- a/orchestrator/src/client/pages/UkVisaJobsPage.tsx +++ b/orchestrator/src/client/pages/UkVisaJobsPage.tsx @@ -38,46 +38,10 @@ import { SheetTrigger, } from "@/components/ui/sheet"; import { cn } from "@/lib/utils"; +import { formatDate, formatDateTime } from "../lib/dateUtils"; import * as api from "../api"; import type { CreateJobInput } from "../../shared/types"; -const formatDate = (dateStr?: string | null) => { - if (!dateStr) return null; - try { - const normalized = dateStr.includes("T") ? dateStr : dateStr.replace(" ", "T"); - const parsed = new Date(normalized); - if (Number.isNaN(parsed.getTime())) return dateStr; - return parsed.toLocaleDateString("en-GB", { - day: "numeric", - month: "short", - year: "numeric", - }); - } catch { - return dateStr; - } -}; - -const formatDateTime = (dateStr?: string | null) => { - if (!dateStr) return null; - try { - const normalized = dateStr.includes("T") ? dateStr : dateStr.replace(" ", "T"); - const parsed = new Date(normalized); - if (Number.isNaN(parsed.getTime())) return dateStr; - const date = parsed.toLocaleDateString("en-GB", { - day: "numeric", - month: "short", - year: "numeric", - }); - const time = parsed.toLocaleTimeString("en-GB", { - hour: "2-digit", - minute: "2-digit", - }); - return `${date} ${time}`; - } catch { - return dateStr; - } -}; - const stripHtml = (value: string) => value.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim(); const clampText = (value: string, max = 160) => (value.length > max ? `${value.slice(0, max).trim()}...` : value);