A full-featured CRM built with React, shadcn/ui, and Supabase.
Refactor shared i18n helpers
Anibal Svarcas2904bd4a7505
diff --git a/registry.json b/registry.jsonindex 78d7cd51..36877dbb 100644--- a/registry.json+++ b/registry.json@@ -409,10 +409,6 @@"path": "src/components/atomic-crm/misc/isLinkedInUrl.ts","type": "registry:component"},- {- "path": "src/components/atomic-crm/misc/formatRelativeTime.ts",- "type": "registry:component"- },{"path": "src/components/atomic-crm/misc/fetchWithTimeout.ts","type": "registry:component"@@ -545,10 +541,6 @@"path": "src/components/atomic-crm/deals/dealUtils.ts","type": "registry:component"},- {- "path": "src/components/atomic-crm/deals/deal.ts",- "type": "registry:component"- },{"path": "src/components/atomic-crm/deals/OnlyMineInput.tsx","type": "registry:component"@@ -645,14 +637,6 @@"path": "src/components/atomic-crm/contacts/index.tsx","type": "registry:component"},- {- "path": "src/components/atomic-crm/contacts/getTranslatedPersonalInfoTypeLabel.ts",- "type": "registry:component"- },- {- "path": "src/components/atomic-crm/contacts/getTranslatedContactGenderLabel.ts",- "type": "registry:component"- },{"path": "src/components/atomic-crm/contacts/exportToVCard.ts","type": "registry:component"diff --git a/src/components/atomic-crm/contacts/ContactEditSheet.tsx b/src/components/atomic-crm/contacts/ContactEditSheet.tsxindex 2c7c67db..bcc9fe77 100644--- a/src/components/atomic-crm/contacts/ContactEditSheet.tsx+++ b/src/components/atomic-crm/contacts/ContactEditSheet.tsx@@ -1,4 +1,4 @@-import { RecordRepresentation, useTranslate, type Identifier } from "ra-core";+import type { Identifier } from "ra-core";import { EditSheet } from "../misc/EditSheet";import { ContactInputs } from "./ContactInputs";@@ -13,16 +13,10 @@ export const ContactEditSheet = ({onOpenChange,contactId,}: ContactEditSheetProps) => {- const translate = useTranslate();return (<EditSheetresource="contacts"id={contactId}- title={- <h1 className="text-xl font-semibold truncate pr-10">- {translate("resources.contacts.action.edit")} <RecordRepresentation />- </h1>- }open={open}onOpenChange={onOpenChange}>diff --git a/src/components/atomic-crm/contacts/contactGender.ts b/src/components/atomic-crm/contacts/contactGender.tsindex 9320bda8..f0ed6ca2 100644--- a/src/components/atomic-crm/contacts/contactGender.ts+++ b/src/components/atomic-crm/contacts/contactGender.ts@@ -2,12 +2,20 @@ import { Mars, NonBinary, Venus } from "lucide-react";import type { ContactGender } from "../types";+type TranslateFn = (key: string, options?: { [key: string]: any }) => string;+export const contactGenderDefaultLabels: Record<string, string> = {male: "He/Him",female: "She/Her",nonbinary: "They/Them",};+const personalInfoTypeMap: Record<string, string> = {+ Work: "work",+ Home: "home",+ Other: "other",+};+export const contactGender: ContactGender[] = [{value: "male",@@ -25,3 +33,22 @@ export const contactGender: ContactGender[] = [icon: NonBinary,},];++export const translateContactGenderLabel = (+ gender: { value: string; label: string },+ translate: TranslateFn,+) =>+ translate(gender.label, {+ _: contactGenderDefaultLabels[gender.value] ?? gender.label,+ });++export const translatePersonalInfoTypeLabel = (+ type: string,+ translate: TranslateFn,+) =>+ translate(+ `resources.contacts.inputs.personal_info_types.${personalInfoTypeMap[type] ?? type.toLowerCase()}`,+ {+ _: type,+ },+ );diff --git a/src/components/atomic-crm/contacts/getTranslatedContactGenderLabel.ts b/src/components/atomic-crm/contacts/getTranslatedContactGenderLabel.tsdeleted file mode 100644index 1e357cba..00000000--- a/src/components/atomic-crm/contacts/getTranslatedContactGenderLabel.ts+++ /dev/null@@ -1,11 +0,0 @@-import { contactGenderDefaultLabels } from "./contactGender";--type TranslateFn = (key: string, options?: { [key: string]: any }) => string;--export const getTranslatedContactGenderLabel = (- gender: { value: string; label: string },- translate: TranslateFn,-) =>- translate(gender.label, {- _: contactGenderDefaultLabels[gender.value] ?? gender.label,- });diff --git a/src/components/atomic-crm/contacts/getTranslatedPersonalInfoTypeLabel.ts b/src/components/atomic-crm/contacts/getTranslatedPersonalInfoTypeLabel.tsdeleted file mode 100644index 8e05b8c7..00000000--- a/src/components/atomic-crm/contacts/getTranslatedPersonalInfoTypeLabel.ts+++ /dev/null@@ -1,18 +0,0 @@-type TranslateFn = (key: string, options?: { [key: string]: any }) => string;--const personalInfoTypeMap: Record<string, string> = {- Work: "work",- Home: "home",- Other: "other",-};--export const getTranslatedPersonalInfoTypeLabel = (- type: string,- translate: TranslateFn,-) =>- translate(- `resources.contacts.inputs.personal_info_types.${personalInfoTypeMap[type] ?? type.toLowerCase()}`,- {- _: type,- },- );diff --git a/src/components/atomic-crm/deals/deal.ts b/src/components/atomic-crm/deals/deal.tsdeleted file mode 100644index 5f08c1a4..00000000--- a/src/components/atomic-crm/deals/deal.ts+++ /dev/null@@ -1,16 +0,0 @@-import type { DealStage } from "../types";--type TranslateFn = (key: string, options?: { [key: string]: any }) => string;--export const findDealLabel = (- dealStages: DealStage[],- dealValue: string,- _translate?: TranslateFn,-) => {- const dealStage = dealStages.find((stage) => stage.value === dealValue);- if (!dealStage) {- return undefined;- }-- return dealStage.label;-};diff --git a/src/components/atomic-crm/deals/dealUtils.ts b/src/components/atomic-crm/deals/dealUtils.tsindex 581d5396..91ba7582 100644--- a/src/components/atomic-crm/deals/dealUtils.ts+++ b/src/components/atomic-crm/deals/dealUtils.ts@@ -1,6 +1,16 @@import { format } from "date-fns";-export function getRelativeTimeString(dateString: string): string {+import type { DealStage } from "../types";++export const findDealLabel = (dealStages: DealStage[], dealValue: string) => {+ const dealStage = dealStages.find((stage) => stage.value === dealValue);+ return dealStage?.label;+};++export function getRelativeTimeString(+ dateString: string,+ locale = "en",+): string {const date = new Date(dateString);date.setHours(0, 0, 0, 0);@@ -12,14 +22,14 @@ export function getRelativeTimeString(dateString: string): string {// Check if the date is more than one week oldif (Math.abs(unitDiff) > 7) {- return new Intl.DateTimeFormat(undefined, {+ return new Intl.DateTimeFormat(locale, {day: "numeric",month: "long",}).format(date);}// Intl.RelativeTimeFormat for dates within the last week- const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });+ const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });return ucFirst(rtf.format(unitDiff, "day"));}diff --git a/src/components/atomic-crm/misc/EditSheet.tsx b/src/components/atomic-crm/misc/EditSheet.tsxindex 60c80f3a..7ddd518b 100644--- a/src/components/atomic-crm/misc/EditSheet.tsx+++ b/src/components/atomic-crm/misc/EditSheet.tsx@@ -10,6 +10,7 @@ import {import {EditBase,Form,+ useEditContext,useNotify,useRedirect,useResourceContext,@@ -84,7 +85,7 @@ export const EditSheet = ({children,open,onOpenChange,- title = "Edit",+ title,redirect: redirectTo = "show",mutationOptions,mutationMode = "undoable",@@ -143,11 +144,7 @@ export const EditSheet = ({><SheetHeader className="border-b"><SheetTitle>- {typeof title === "string" ? (- <span className="text-xl font-semibold">{title}</span>- ) : (- title- )}+ <EditSheetTitle title={title} /></SheetTitle></SheetHeader>@@ -167,3 +164,22 @@ export const EditSheet = ({</Sheet>);};++const EditSheetTitle = ({ title }: { title?: ReactNode | string | false }) => {+ const { defaultTitle } = useEditContext();++ if (title === false) {+ return null;+ }++ const resolvedTitle = title === undefined ? defaultTitle : title;+ if (resolvedTitle == null) {+ return null;+ }++ return typeof resolvedTitle === "string" ? (+ <span className="text-xl font-semibold">{resolvedTitle}</span>+ ) : (+ resolvedTitle+ );+};diff --git a/src/components/atomic-crm/misc/RelativeDate.tsx b/src/components/atomic-crm/misc/RelativeDate.tsxindex 081cf6b2..ea13f63f 100644--- a/src/components/atomic-crm/misc/RelativeDate.tsx+++ b/src/components/atomic-crm/misc/RelativeDate.tsx@@ -1,16 +1,36 @@+/* eslint-disable react-refresh/only-export-components */import { differenceInDays, formatRelative } from "date-fns";import { enUS, fr } from "date-fns/locale";import { useLocaleState } from "ra-core";-export function RelativeDate({ date }: { date: string }) {- const [locale = "en"] = useLocaleState();+const getDateFnsLocale = (locale: string) =>+ locale.startsWith("fr") ? fr : enUS;++export const formatLocalizedDate = (date: string, locale = "en") =>+ new Intl.DateTimeFormat(locale, {+ year: "numeric",+ month: "long",+ day: "numeric",+ }).format(new Date(date));++export const formatRelativeDate = (date: string, locale = "en") => {const dateObj = new Date(date);const now = new Date();- const dateFnsLocale = locale.startsWith("fr") ? fr : enUS;+ const dateFnsLocale = getDateFnsLocale(locale);if (differenceInDays(now, dateObj) > 6) {return new Intl.DateTimeFormat(locale).format(dateObj);}return formatRelative(dateObj, now, { locale: dateFnsLocale });+};++export const useRelativeDate = (date: string) => {+ const [locale = "en"] = useLocaleState();++ return formatRelativeDate(date, locale);+};++export function RelativeDate({ date }: { date: string }) {+ return useRelativeDate(date);}diff --git a/src/components/atomic-crm/misc/formatRelativeTime.ts b/src/components/atomic-crm/misc/formatRelativeTime.tsdeleted file mode 100644index 9fc297ac..00000000--- a/src/components/atomic-crm/misc/formatRelativeTime.ts+++ /dev/null@@ -1,35 +0,0 @@-const DIVISIONS: Array<{- amount: number;- unit: Intl.RelativeTimeFormatUnit;-}> = [- { amount: 60, unit: "second" },- { amount: 60, unit: "minute" },- { amount: 24, unit: "hour" },- { amount: 7, unit: "day" },- { amount: 4.34524, unit: "week" },- { amount: 12, unit: "month" },- { amount: Number.POSITIVE_INFINITY, unit: "year" },-];--export const formatRelativeTime = (- value: string | Date,- locale = "en",- now = new Date(),-) => {- const date = value instanceof Date ? value : new Date(value);- const diffInSeconds = (date.getTime() - now.getTime()) / 1000;-- const formatter = new Intl.RelativeTimeFormat(locale, {- numeric: "auto",- });-- let duration = diffInSeconds;- for (const division of DIVISIONS) {- if (Math.abs(duration) < division.amount) {- return formatter.format(Math.round(duration), division.unit);- }- duration /= division.amount;- }-- return formatter.format(Math.round(duration), "year");-};