Skip to content

fix(RelativeRangeDatePicker): last period preset title #130

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 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@
"From start of month": "From start of month",
"From start of year": "From start of year",
"Previous month": "Previous month",
"Last {count} {unit}": "Last {{count}} {{unit}}",
"m": ["minute", "minutes", "minutes"],
"h": ["hour", "hours", "hours"],
"d": ["day", "days", "days"],
"w": ["week", "weeks", "weeks"],
"M": ["month", "months", "months"],
"y": ["year", "years", "years"],
"Last second": "Last second",
"Last minute": "Last minute",
"Last {count} second": [
"Last {{count}} second",
"Last {{count}} seconds",
"Last {{count}} seconds"
],
"Last {count} minute": [
"Last {{count}} minute",
"Last {{count}} minutes",
"Last {{count}} minutes"
],
"Last {count} hour": ["Last {{count}} hour", "Last {{count}} hours", "Last {{count}} hours"],
"Last {count} day": ["Last {{count}} day", "Last {{count}} days", "Last {{count}} days"],
"Last {count} week": ["Last {{count}} week", "Last {{count}} weeks", "Last {{count}} weeks"],
"Last {count} month": ["Last {{count}} month", "Last {{count}} months", "Last {{count}} months"],
"Last {count} year": ["Last {{count}} year", "Last {{count}} years", "Last {{count}} years"],
"Main": "Main",
"Other": "Other",
"Range": "Range",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,43 @@
"From start of month": "С начала месяца",
"From start of year": "С начала года",
"Previous month": "Предыдущий месяц",
"Last {count} {unit}": "Last {{count}} {{unit}}",
"m": ["minute", "minutes", "minutes"],
"h": ["hour", "hours", "hours"],
"d": ["day", "days", "days"],
"w": ["week", "weeks", "weeks"],
"M": ["month", "months", "months"],
"y": ["year", "years", "years"],
"Last second": "Последняя секунда",
"Last minute": "Последняя минута",
"Last {count} second": [
"Последняя {{count}} секунда",
"Последние {{count}} секунды",
"Последние {{count}} секунд"
],
"Last {count} minute": [
"Последняя {{count}} минута",
"Последние {{count}} минуты",
"Последние {{count}} минут"
],
"Last {count} hour": [
"Последний {{count}} час",
"Последние {{count}} часа",
"Последние {{count}} часов"
],
"Last {count} day": [
"Последний {{count}} день",
"Последние {{count}} дня",
"Последние {{count}} дней"
],
"Last {count} week": [
"Последняя {{count}} неделя",
"Последние {{count}} недели",
"Последние {{count}} недель"
],
"Last {count} month": [
"Последний {{count}} месяц",
"Последние {{count}} месяца",
"Последние {{count}} месяцев"
],
"Last {count} year": [
"Последний {{count}} год",
"Последние {{count}} года",
"Последние {{count}} лет"
],
"Main": "Основные",
"Other": "Другие",
"Range": "Период",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import {
import type {Preset} from './defaultPresets';
import {i18n} from './i18n';

const lastRe = /^now-(\d+)([smhdwMy])$/;

const oneUnit = {
s: 'Last second',
m: 'Last minute',
h: 'Last hour',
d: 'Last day',
w: 'Last week',
M: 'Last month',
y: 'Last year',
} as const;

const countUnit = {
s: 'Last {count} second',
m: 'Last {count} minute',
h: 'Last {count} hour',
d: 'Last {count} day',
w: 'Last {count} week',
M: 'Last {count} month',
y: 'Last {count} year',
} as const;

export function getPresetTitle(start: string, end: string, presets: Preset[] = allPresets) {
const startText = start.replace(/\s+/g, '');
const endText = end.replace(/\s+/g, '');
Expand All @@ -21,23 +43,21 @@ export function getPresetTitle(start: string, end: string, presets: Preset[] = a
}

if (end === 'now') {
const match = /^now-(\d+)([m|h|d|w|M|y])$/.exec(start);
const match = lastRe.exec(startText);
if (match) {
const [, count, unit] = match;
if (isDateUnit(unit)) {
return i18n('Last {count} {unit}', {
count,
unit: i18n(unit, {count: Number(count)}),
});
const template = Number(count) === 1 ? oneUnit[unit] : countUnit[unit];
return i18n(template, {count});
}
}
}

return startText + ' — ' + endText;
}

function isDateUnit(value: string): value is 'm' | 'h' | 'd' | 'w' | 'M' | 'y' {
return ['m', 'h', 'd', 'w', 'M', 'y'].includes(value);
function isDateUnit(value: string): value is 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y' {
return ['s', 'm', 'h', 'd', 'w', 'M', 'y'].includes(value);
}

export function filterPresets(presets: Preset[], minValue?: DateTime) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/RelativeRangeDatePicker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function getDefaultTitle({
value.start?.type === 'relative' &&
value.end?.type === 'relative'
) {
return `${getPresetTitle(value.start.value, value.end.value, presets)}${tz}`;
return `${getPresetTitle(value.start.value, value.end.value, presets)}`;
}

const delimiter = ' — ';
Expand Down
Loading