mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
refactor dynamic time filter logic, let user choose any hour value in time filter params
This commit is contained in:
+1
-1
@@ -32,7 +32,7 @@ const ValidateRouteParams = () => {
|
|||||||
accountComments?.length > 0 &&
|
accountComments?.length > 0 &&
|
||||||
parseInt(accountCommentIndex) < accountComments.length);
|
parseInt(accountCommentIndex) < accountComments.length);
|
||||||
|
|
||||||
const isDynamicTimeFilter = (filter: string) => /^\d+[dwmy]$/.test(filter);
|
const isDynamicTimeFilter = (filter: string) => /^\d+[hdwmy]$/.test(filter);
|
||||||
const isTimeFilterNameValid =
|
const isTimeFilterNameValid =
|
||||||
!timeFilterName || timeFilterNames.includes(timeFilterName as any) || timeFilterName === lastVisitTimeFilterName || isDynamicTimeFilter(timeFilterName);
|
!timeFilterName || timeFilterNames.includes(timeFilterName as any) || timeFilterName === lastVisitTimeFilterName || isDynamicTimeFilter(timeFilterName);
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,31 @@ if (secondsSinceLastVisit > 30 * day) {
|
|||||||
|
|
||||||
export const timeFilterNames = [lastVisitTimeFilterName, '1h', '12h', '24h', '48h', '1w', '1m', '1y', 'all'];
|
export const timeFilterNames = [lastVisitTimeFilterName, '1h', '12h', '24h', '48h', '1w', '1m', '1y', 'all'];
|
||||||
|
|
||||||
|
function convertTimeStringToSeconds(timeString: string): number {
|
||||||
|
const match = timeString.match(/^(\d+)([hdwmy])$/);
|
||||||
|
if (!match) {
|
||||||
|
throw new Error(`Invalid time filter format: ${timeString}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [, value, unit] = match;
|
||||||
|
const numValue = parseInt(value, 10);
|
||||||
|
|
||||||
|
switch (unit) {
|
||||||
|
case 'h':
|
||||||
|
return numValue * 60 * 60;
|
||||||
|
case 'd':
|
||||||
|
return numValue * 24 * 60 * 60;
|
||||||
|
case 'w':
|
||||||
|
return numValue * 7 * 24 * 60 * 60;
|
||||||
|
case 'm':
|
||||||
|
return numValue * 30 * 24 * 60 * 60;
|
||||||
|
case 'y':
|
||||||
|
return numValue * 365 * 24 * 60 * 60;
|
||||||
|
default:
|
||||||
|
throw new Error(`Invalid time unit: ${unit}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const useTimeFilter = () => {
|
const useTimeFilter = () => {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
let timeFilterName = params.timeFilterName;
|
let timeFilterName = params.timeFilterName;
|
||||||
@@ -58,25 +83,11 @@ const useTimeFilter = () => {
|
|||||||
} else if (timeFilterName && timeFilterName in timeFilterNamesToSeconds) {
|
} else if (timeFilterName && timeFilterName in timeFilterNamesToSeconds) {
|
||||||
timeFilterSeconds = timeFilterNamesToSeconds[timeFilterName as keyof typeof timeFilterNamesToSeconds];
|
timeFilterSeconds = timeFilterNamesToSeconds[timeFilterName as keyof typeof timeFilterNamesToSeconds];
|
||||||
} else if (timeFilterName) {
|
} else if (timeFilterName) {
|
||||||
// Handle dynamic time filters (e.g., "3d", "2w")
|
try {
|
||||||
const match = timeFilterName.match(/^(\d+)([dwmy])$/);
|
timeFilterSeconds = convertTimeStringToSeconds(timeFilterName);
|
||||||
if (match) {
|
} catch (e) {
|
||||||
const [, value, unit] = match;
|
console.error(`Invalid time filter format: ${timeFilterName}`);
|
||||||
const numValue = parseInt(value, 10);
|
timeFilterSeconds = undefined;
|
||||||
switch (unit) {
|
|
||||||
case 'd':
|
|
||||||
timeFilterSeconds = numValue * 24 * 60 * 60;
|
|
||||||
break;
|
|
||||||
case 'w':
|
|
||||||
timeFilterSeconds = numValue * 7 * 24 * 60 * 60;
|
|
||||||
break;
|
|
||||||
case 'm':
|
|
||||||
timeFilterSeconds = numValue * 30 * 24 * 60 * 60;
|
|
||||||
break;
|
|
||||||
case 'y':
|
|
||||||
timeFilterSeconds = numValue * 365 * 24 * 60 * 60;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user