-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor duplicated date formatting and breadcrumb navigation code #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d67192
Initial plan
Copilot 2083442
Refactor duplicated code: create reusable date formatter hook and bre…
Copilot 23a22a4
Address code review feedback: improve useLocalizedDateFormatter with …
Copilot 1bf10d2
Improve date validation and error handling in useLocalizedDateFormatter
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import React from 'react'; | ||
| import { Link } from 'react-router-dom'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { | ||
| Breadcrumb, | ||
| BreadcrumbList, | ||
| BreadcrumbItem, | ||
| BreadcrumbLink, | ||
| BreadcrumbPage, | ||
| BreadcrumbSeparator, | ||
| } from '@/components/ui/breadcrumb'; | ||
|
|
||
| interface BreadcrumbItem { | ||
| label: string; | ||
| path?: string; | ||
| } | ||
|
|
||
| interface PageBreadcrumbProps { | ||
| currentPage: string; | ||
| items?: BreadcrumbItem[]; | ||
| className?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Reusable breadcrumb navigation component | ||
| * @param currentPage - Translation key for current page (e.g., 'navigation.blog') | ||
| * @param items - Optional array of intermediate breadcrumb items | ||
| * @param className - Optional CSS classes for container | ||
| */ | ||
| export const PageBreadcrumb = ({ | ||
| currentPage, | ||
| items = [], | ||
| className = 'max-w-7xl mx-auto px-4 pt-4' | ||
| }: PageBreadcrumbProps) => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| return ( | ||
| <div className={className}> | ||
| <Breadcrumb> | ||
| <BreadcrumbList> | ||
| <BreadcrumbItem> | ||
| <BreadcrumbLink asChild> | ||
| <Link to="/">{t('navigation.home')}</Link> | ||
| </BreadcrumbLink> | ||
| </BreadcrumbItem> | ||
|
|
||
| {items.map((item, index) => ( | ||
| <React.Fragment key={index}> | ||
| <BreadcrumbSeparator /> | ||
| <BreadcrumbItem> | ||
| {item.path ? ( | ||
| <BreadcrumbLink asChild> | ||
| <Link to={item.path}>{item.label}</Link> | ||
| </BreadcrumbLink> | ||
| ) : ( | ||
| <BreadcrumbPage>{item.label}</BreadcrumbPage> | ||
| )} | ||
| </BreadcrumbItem> | ||
| </React.Fragment> | ||
| ))} | ||
|
|
||
| <BreadcrumbSeparator /> | ||
| <BreadcrumbItem> | ||
| <BreadcrumbPage>{t(currentPage)}</BreadcrumbPage> | ||
| </BreadcrumbItem> | ||
| </BreadcrumbList> | ||
| </Breadcrumb> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { useMemo, useCallback } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { getNormalizedLocale } from '@/lib/i18n'; | ||
|
|
||
| type DateStyle = 'short' | 'medium' | 'long' | 'full'; | ||
| type TimeStyle = 'short' | 'medium' | 'long' | 'full'; | ||
|
|
||
| interface DateFormatterOptions { | ||
| dateStyle?: DateStyle; | ||
| timeStyle?: TimeStyle; | ||
| year?: 'numeric' | '2-digit'; | ||
| month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow'; | ||
| day?: 'numeric' | '2-digit'; | ||
| } | ||
|
|
||
| interface UseLocalizedDateFormatterOptions { | ||
| /** | ||
| * Intl.DateTimeFormat options for date formatting | ||
| */ | ||
| dateOptions?: DateFormatterOptions; | ||
| /** | ||
| * Fallback options if normalized locale fails (defaults to en-US with same options) | ||
| */ | ||
| fallbackOptions?: DateFormatterOptions; | ||
| /** | ||
| * String to return when date is null/undefined (defaults to '—') | ||
| */ | ||
| nullFallback?: string; | ||
| } | ||
|
|
||
| interface UseLocalizedDateFormatterReturn { | ||
| formatDate: (date: string | Date | null | undefined) => string; | ||
| dateFormatter: Intl.DateTimeFormat; | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to convert date input to Date object | ||
| * @throws Error if date is invalid | ||
| */ | ||
| const toDateObject = (date: string | Date): Date => { | ||
| const dateObj = typeof date === 'string' ? new Date(date) : date; | ||
|
|
||
| // Check if date is valid | ||
| if (Number.isNaN(dateObj.getTime())) { | ||
| throw new Error('Invalid date'); | ||
| } | ||
|
|
||
| return dateObj; | ||
| }; | ||
|
|
||
| /** | ||
| * Custom hook for localized date formatting with automatic fallback | ||
| * @param options - Configuration options for the date formatter | ||
| * @returns Object containing formatDate function and dateFormatter instance | ||
| */ | ||
| export const useLocalizedDateFormatter = ( | ||
| options: UseLocalizedDateFormatterOptions = {} | ||
| ): UseLocalizedDateFormatterReturn => { | ||
| const { | ||
| dateOptions = { dateStyle: 'medium' }, | ||
| fallbackOptions, | ||
| nullFallback = '—', | ||
| } = options; | ||
|
|
||
| const { i18n } = useTranslation(); | ||
|
|
||
| const normalizedLocale = useMemo( | ||
| () => getNormalizedLocale(i18n.language), | ||
| [i18n.language] | ||
| ); | ||
|
|
||
| const fallbackDateFormatter = useMemo( | ||
| () => new Intl.DateTimeFormat('en-US', fallbackOptions ?? dateOptions), | ||
| [fallbackOptions, dateOptions] | ||
| ); | ||
|
|
||
| const dateFormatter = useMemo(() => { | ||
| try { | ||
| return new Intl.DateTimeFormat(normalizedLocale, dateOptions); | ||
| } catch (error) { | ||
| console.error('Unsupported locale for date formatting', error); | ||
| return fallbackDateFormatter; | ||
| } | ||
| }, [normalizedLocale, dateOptions, fallbackDateFormatter]); | ||
|
|
||
| const formatDate = useCallback( | ||
| (date: string | Date | null | undefined) => { | ||
| if (!date) return nullFallback; | ||
|
|
||
| try { | ||
| const dateObject = toDateObject(date); | ||
|
|
||
| try { | ||
| return dateFormatter.format(dateObject); | ||
| } catch (formattingError) { | ||
| console.error('Error formatting date with primary formatter', formattingError); | ||
| return fallbackDateFormatter.format(dateObject); | ||
| } | ||
| } catch (dateError) { | ||
| console.error('Invalid date provided', dateError); | ||
| return nullFallback; | ||
| } | ||
| }, | ||
| [dateFormatter, fallbackDateFormatter, nullFallback] | ||
| ); | ||
|
|
||
| return { formatDate, dateFormatter }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The top-level React import drops
useCallback, buthandlePageChangestill callsuseCallbacklater in this file. This will cause a compile/runtime failure (useCallbackis not defined) when building the blog page. Re‑adduseCallbackto the import list so the pagination handler continues to work.Useful? React with 👍 / 👎.