- Integrated `SubscriptionInfoBar` component into the profile page for displaying subscription details. - Updated GraphQL types to include subscription-related fields and filters. - Enhanced the profile data management by adding subscription handling capabilities. - Added a new utility function `getRemainingDays` to calculate remaining days until a specified date.
122 lines
3.2 KiB
TypeScript
122 lines
3.2 KiB
TypeScript
/* eslint-disable import/no-unassigned-import */
|
||
import { type OpUnitType } from 'dayjs';
|
||
import dayjs, { type ConfigType } from 'dayjs';
|
||
import timezone from 'dayjs/plugin/timezone';
|
||
import utc from 'dayjs/plugin/utc';
|
||
import 'dayjs/locale/ru';
|
||
|
||
type DateTime = Exclude<ConfigType, null | undefined>;
|
||
|
||
if (!dayjs.prototype.utc) {
|
||
dayjs.extend(utc);
|
||
}
|
||
|
||
if (!dayjs.prototype.tz) {
|
||
dayjs.extend(timezone);
|
||
}
|
||
|
||
dayjs.locale('ru');
|
||
|
||
const DEFAULT_TZ = 'Europe/Moscow';
|
||
|
||
/**
|
||
* Склеивает дату (Date/string) и время (string, HH:mm) в datetime в нужной таймзоне, возвращает ISO строку в UTC
|
||
*/
|
||
export function combineDateAndTimeToUTC(
|
||
date: DateTime,
|
||
time: string,
|
||
tz: string = DEFAULT_TZ,
|
||
): string {
|
||
if (!date || !time) return '';
|
||
const dateString = dayjs(date).format('YYYY-MM-DD');
|
||
|
||
return dayjs.tz(`${dateString}T${time}`, tz).utc().toISOString();
|
||
}
|
||
|
||
export function convertTimeString(time: string) {
|
||
const [hours = '00', minutes = '00'] = time.split(':');
|
||
|
||
return {
|
||
db: () => `${hours}:${minutes}:00.000`,
|
||
};
|
||
}
|
||
|
||
export function formatDate(datetime: DateTime) {
|
||
return {
|
||
db: () => dayjs(datetime).utc().toISOString(),
|
||
user: (template?: string, tz: string = DEFAULT_TZ) => {
|
||
return dayjs
|
||
.utc(datetime)
|
||
.tz(tz)
|
||
.format(template || 'D MMMM YYYY');
|
||
},
|
||
};
|
||
}
|
||
|
||
export function formatTime(datetime: ConfigType) {
|
||
return {
|
||
// db: () => dayjs(datetime).utc().toISOString(),
|
||
user: (tz: string = DEFAULT_TZ) => {
|
||
return dayjs.utc(datetime).tz(tz).format('HH:mm');
|
||
},
|
||
};
|
||
}
|
||
|
||
export function getDateUTCRange(date?: DateTime, tz: string = DEFAULT_TZ) {
|
||
return {
|
||
day: () => {
|
||
const startOfDay = dayjs(date).tz(tz).startOf('day').utc().toISOString();
|
||
const endOfDay = dayjs(date).tz(tz).endOf('day').utc().toISOString();
|
||
|
||
return { endOfDay, startOfDay };
|
||
},
|
||
month: () => {
|
||
const startOfMonth = dayjs(date).tz(tz).startOf('month').utc().toISOString();
|
||
const endOfMonth = dayjs(date).tz(tz).endOf('month').utc().toISOString();
|
||
|
||
return { endOfMonth, startOfMonth };
|
||
},
|
||
};
|
||
}
|
||
|
||
export function getMinutes(time: string) {
|
||
const [hours = '00', minutes = '00'] = time.split(':');
|
||
|
||
return Number.parseInt(hours, 10) * 60 + Number.parseInt(minutes, 10);
|
||
}
|
||
|
||
export function getRemainingDays(date: DateTime) {
|
||
return dayjs(date).diff(dayjs(), 'day');
|
||
}
|
||
|
||
export function getTimeZoneLabel(tz: string = DEFAULT_TZ): string {
|
||
if (tz === DEFAULT_TZ) return 'МСК';
|
||
const offset = dayjs().tz(tz).format('Z');
|
||
|
||
return `GMT${offset}`;
|
||
}
|
||
|
||
/**
|
||
* This indicates whether the date is before the other supplied date-time.
|
||
* ```
|
||
* // Default unit - 'day':
|
||
* isBeforeNow(date, 'day')
|
||
* ```
|
||
*/
|
||
export function isBeforeNow(date: Date | string, unit: OpUnitType = 'day') {
|
||
const now = dayjs().tz(DEFAULT_TZ);
|
||
const inputDate = dayjs(date).tz(DEFAULT_TZ);
|
||
|
||
return inputDate.isBefore(now, unit);
|
||
}
|
||
|
||
export function isNowOrAfter(date: Date | string, unit: OpUnitType = 'day') {
|
||
return !isBeforeNow(date, unit);
|
||
}
|
||
|
||
export function sumTime(datetime: DateTime, durationMinutes: number) {
|
||
if (!datetime) return '';
|
||
|
||
return dayjs(datetime).add(durationMinutes, 'minute').toISOString();
|
||
}
|