diff --git a/src/App.jsx b/src/App.jsx index b4fcf31..4aa932b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -31,8 +31,37 @@ const DATE_RANGE_OPTIONS = [ const parseDate = (value) => { if (!value) return null; - const date = new Date(value); - return Number.isNaN(date.getTime()) ? null : date; + + const toDate = (input) => { + const date = new Date(input); + return Number.isNaN(date.getTime()) ? null : date; + }; + + let date = value instanceof Date ? value : null; + + if (!date && (typeof value === "number" || typeof value === "string")) { + date = toDate(value); + + if (!date && typeof value === "string") { + const trimmed = value.trim(); + const dayMatch = trimmed.match(/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{2,4})$/); + + if (dayMatch) { + const [, day, month, year] = dayMatch; + const normalizedYear = year.length === 2 ? `20${year}` : year; + date = toDate(`${normalizedYear.padStart(4, "0")}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`); + } else { + const monthMatch = trimmed.match(/^([0-9]{1,2})[\/\-]([0-9]{2,4})$/); + if (monthMatch) { + const [, month, year] = monthMatch; + const normalizedYear = year.length === 2 ? `20${year}` : year; + date = toDate(`${normalizedYear.padStart(4, "0")}-${month.padStart(2, "0")}-01`); + } + } + } + } + + return date; }; const matchesDateRange = (annonce, range) => { diff --git a/src/components/AddModal.jsx b/src/components/AddModal.jsx index 17ce4aa..30db9dc 100644 --- a/src/components/AddModal.jsx +++ b/src/components/AddModal.jsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useMemo, useState } from "react"; import { X, Plus, Loader2, Link } from "lucide-react"; export default function AddModal({ open, onClose, onSubmit, existingUrls }) { @@ -6,11 +6,29 @@ export default function AddModal({ open, onClose, onSubmit, existingUrls }) { const [loading, setLoading] = useState(false); const [urlError, setUrlError] = useState(""); + const normalizeUrl = (raw) => { + if (!raw) return ""; + try { + const parsed = new URL(raw.trim()); + parsed.hash = ""; + parsed.search = ""; + return `${parsed.origin}${parsed.pathname}`; + } catch (err) { + return raw.trim().split(/[?#]/)[0]; + } + }; + + const normalizedExisting = useMemo( + () => existingUrls.map((item) => normalizeUrl(item)), + [existingUrls] + ); + if (!open) return null; const handleUrlChange = (val) => { setUrl(val); - if (existingUrls.includes(val.trim())) { + const normalized = normalizeUrl(val); + if (normalized && normalizedExisting.includes(normalized)) { setUrlError("Cette URL existe deja dans la liste !"); } else { setUrlError(""); diff --git a/src/components/AppartCard.jsx b/src/components/AppartCard.jsx index 1bf1630..940b66e 100644 --- a/src/components/AppartCard.jsx +++ b/src/components/AppartCard.jsx @@ -65,12 +65,20 @@ const formatFrDate = (value) => { date = toDate(value); if (!date && typeof value === "string") { - const match = value.trim().match(/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{2,4})$/); + const trimmed = value.trim(); + const dayMatch = trimmed.match(/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{2,4})$/); - if (match) { - const [, day, month, year] = match; + if (dayMatch) { + const [, day, month, year] = dayMatch; const normalizedYear = year.length === 2 ? `20${year}` : year; date = toDate(`${normalizedYear.padStart(4, "0")}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`); + } else { + const monthMatch = trimmed.match(/^([0-9]{1,2})[\/\-]([0-9]{2,4})$/); + if (monthMatch) { + const [, month, year] = monthMatch; + const normalizedYear = year.length === 2 ? `20${year}` : year; + date = toDate(`${normalizedYear.padStart(4, "0")}-${month.padStart(2, "0")}-01`); + } } } }