Skip to content

Commit 3e2c554

Browse files
Copilothotlong
andcommitted
refactor: improve readability of sidebar cva variants and reorder formatRelativeDate before formatDate
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent f47677a commit 3e2c554

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

packages/components/src/ui/sidebar.tsx

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/fields/src/index.tsx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ export function formatPercent(value: number, precision: number = 0): string {
104104
return `${displayValue.toFixed(precision)}%`;
105105
}
106106

107+
/**
108+
* Format date as relative time (e.g., "2 days ago", "Today", "Overdue 3d")
109+
*/
110+
export function formatRelativeDate(value: string | Date): string {
111+
if (!value) return '-';
112+
const date = typeof value === 'string' ? new Date(value) : value;
113+
if (isNaN(date.getTime())) return '-';
114+
115+
const now = new Date();
116+
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
117+
const startOfDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
118+
const diffMs = startOfDate.getTime() - startOfToday.getTime();
119+
const diffDays = Math.round(diffMs / (1000 * 60 * 60 * 24));
120+
121+
if (diffDays === 0) return 'Today';
122+
if (diffDays === 1) return 'Tomorrow';
123+
if (diffDays === -1) return 'Yesterday';
124+
if (diffDays < -1) {
125+
const absDays = Math.abs(diffDays);
126+
if (absDays <= 30) return `${absDays} days ago`;
127+
return formatDate(date);
128+
}
129+
if (diffDays > 1 && diffDays <= 30) return `In ${diffDays} days`;
130+
return formatDate(date);
131+
}
132+
107133
/**
108134
* Format date value
109135
*/
@@ -132,32 +158,6 @@ export function formatDate(value: string | Date, style?: string): string {
132158
});
133159
}
134160

135-
/**
136-
* Format date as relative time (e.g., "2 days ago", "Today", "Overdue 3d")
137-
*/
138-
export function formatRelativeDate(value: string | Date): string {
139-
if (!value) return '-';
140-
const date = typeof value === 'string' ? new Date(value) : value;
141-
if (isNaN(date.getTime())) return '-';
142-
143-
const now = new Date();
144-
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
145-
const startOfDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
146-
const diffMs = startOfDate.getTime() - startOfToday.getTime();
147-
const diffDays = Math.round(diffMs / (1000 * 60 * 60 * 24));
148-
149-
if (diffDays === 0) return 'Today';
150-
if (diffDays === 1) return 'Tomorrow';
151-
if (diffDays === -1) return 'Yesterday';
152-
if (diffDays < -1) {
153-
const absDays = Math.abs(diffDays);
154-
if (absDays <= 30) return `${absDays} days ago`;
155-
return formatDate(date);
156-
}
157-
if (diffDays > 1 && diffDays <= 30) return `In ${diffDays} days`;
158-
return formatDate(date);
159-
}
160-
161161
/**
162162
* Format datetime value
163163
*/

0 commit comments

Comments
 (0)