Refactor frontend date formatting to eliminate duplication
Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com>
This commit is contained in:
+39
-45
@@ -358,15 +358,12 @@ function AppContent() {
|
|||||||
setScheduleDays(storedDays ? Number(storedDays) : 30);
|
setScheduleDays(storedDays ? Number(storedDays) : 30);
|
||||||
|
|
||||||
// Load manually collapsed/expanded days from localStorage
|
// Load manually collapsed/expanded days from localStorage
|
||||||
const storedCollapsed = localStorage.getItem(userStorageKey(user.id, "collapsedDays"));
|
const { collapsed, expanded } = loadCollapsedDaysFromStorage(
|
||||||
const storedExpanded = localStorage.getItem(userStorageKey(user.id, "expandedDays"));
|
userStorageKey(user.id, "collapsedDays"),
|
||||||
try {
|
userStorageKey(user.id, "expandedDays")
|
||||||
setManuallyCollapsedDays(storedCollapsed ? new Set(JSON.parse(storedCollapsed)) : new Set());
|
);
|
||||||
setManuallyExpandedDays(storedExpanded ? new Set(JSON.parse(storedExpanded)) : new Set());
|
setManuallyCollapsedDays(collapsed);
|
||||||
} catch {
|
setManuallyExpandedDays(expanded);
|
||||||
setManuallyCollapsedDays(new Set());
|
|
||||||
setManuallyExpandedDays(new Set());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [user?.id]);
|
}, [user?.id]);
|
||||||
|
|
||||||
@@ -2895,22 +2892,36 @@ function toIsoString(value: string) {
|
|||||||
return Number.isNaN(date.getTime()) ? new Date().toISOString() : date.toISOString();
|
return Number.isNaN(date.getTime()) ? new Date().toISOString() : date.toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function pad2(n: number): string {
|
||||||
|
return String(n).padStart(2, '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadCollapsedDaysFromStorage(collapsedKey: string, expandedKey: string): { collapsed: Set<string>; expanded: Set<string> } {
|
||||||
|
const storedCollapsed = localStorage.getItem(collapsedKey);
|
||||||
|
const storedExpanded = localStorage.getItem(expandedKey);
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
collapsed: storedCollapsed ? new Set(JSON.parse(storedCollapsed)) : new Set(),
|
||||||
|
expanded: storedExpanded ? new Set(JSON.parse(storedExpanded)) : new Set()
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
return {
|
||||||
|
collapsed: new Set(),
|
||||||
|
expanded: new Set()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function toDateValue(date: Date | string): string {
|
function toDateValue(date: Date | string): string {
|
||||||
const d = typeof date === 'string' ? new Date(date) : date;
|
const d = typeof date === 'string' ? new Date(date) : date;
|
||||||
if (Number.isNaN(d.getTime())) {
|
const fallback = Number.isNaN(d.getTime()) ? new Date() : d;
|
||||||
const now = new Date();
|
return `${fallback.getFullYear()}-${pad2(fallback.getMonth() + 1)}-${pad2(fallback.getDate())}`;
|
||||||
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
|
|
||||||
}
|
|
||||||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function toTimeValue(date: Date | string): string {
|
function toTimeValue(date: Date | string): string {
|
||||||
const d = typeof date === 'string' ? new Date(date) : date;
|
const d = typeof date === 'string' ? new Date(date) : date;
|
||||||
if (Number.isNaN(d.getTime())) {
|
const fallback = Number.isNaN(d.getTime()) ? new Date() : d;
|
||||||
const now = new Date();
|
return `${pad2(fallback.getHours())}:${pad2(fallback.getMinutes())}`;
|
||||||
return `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
|
|
||||||
}
|
|
||||||
return `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function combineDateAndTime(dateStr: string, timeStr: string): string {
|
function combineDateAndTime(dateStr: string, timeStr: string): string {
|
||||||
@@ -2920,23 +2931,9 @@ function combineDateAndTime(dateStr: string, timeStr: string): string {
|
|||||||
|
|
||||||
function toInputValue(value: string) {
|
function toInputValue(value: string) {
|
||||||
const date = new Date(value);
|
const date = new Date(value);
|
||||||
if (Number.isNaN(date.getTime())) {
|
const d = Number.isNaN(date.getTime()) ? new Date() : date;
|
||||||
// Return current local time in datetime-local format
|
// Use existing helper functions to avoid duplication
|
||||||
const now = new Date();
|
return `${toDateValue(d)}T${toTimeValue(d)}`;
|
||||||
const year = now.getFullYear();
|
|
||||||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
|
||||||
const day = String(now.getDate()).padStart(2, '0');
|
|
||||||
const hours = String(now.getHours()).padStart(2, '0');
|
|
||||||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
|
||||||
return `${year}-${month}-${day}T${hours}:${minutes}`;
|
|
||||||
}
|
|
||||||
// Convert to local time format for datetime-local input
|
|
||||||
const year = date.getFullYear();
|
|
||||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
||||||
const day = String(date.getDate()).padStart(2, '0');
|
|
||||||
const hours = String(date.getHours()).padStart(2, '0');
|
|
||||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
||||||
return `${year}-${month}-${day}T${hours}:${minutes}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDateTime(value: string, locale: string) {
|
function formatDateTime(value: string, locale: string) {
|
||||||
@@ -3449,15 +3446,12 @@ function SharedSchedule() {
|
|||||||
// Load collapsed/expanded state from localStorage
|
// Load collapsed/expanded state from localStorage
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (token && typeof window !== "undefined") {
|
if (token && typeof window !== "undefined") {
|
||||||
const storedCollapsed = localStorage.getItem(`share_${token}_collapsedDays`);
|
const { collapsed, expanded } = loadCollapsedDaysFromStorage(
|
||||||
const storedExpanded = localStorage.getItem(`share_${token}_expandedDays`);
|
`share_${token}_collapsedDays`,
|
||||||
try {
|
`share_${token}_expandedDays`
|
||||||
setManuallyCollapsedDays(storedCollapsed ? new Set(JSON.parse(storedCollapsed)) : new Set());
|
);
|
||||||
setManuallyExpandedDays(storedExpanded ? new Set(JSON.parse(storedExpanded)) : new Set());
|
setManuallyCollapsedDays(collapsed);
|
||||||
} catch {
|
setManuallyExpandedDays(expanded);
|
||||||
setManuallyCollapsedDays(new Set());
|
|
||||||
setManuallyExpandedDays(new Set());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [token]);
|
}, [token]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user