Replace moment with date-fns

This commit is contained in:
2026-05-11 21:38:11 +02:00
parent 0140f2af3c
commit 405184b1f8
13 changed files with 73 additions and 77 deletions
@@ -1,5 +1,4 @@
import { Component, Input, OnDestroy, ElementRef } from '@angular/core';
import * as moment from 'moment';
import { AdminModel } from '../../../services/adminModel';
import { Account } from '../../../../common/adminInterfaces';
import { ChatDate, createChatDate, createDateRange } from '../../../../common/adminUtils';
@@ -7,6 +6,7 @@ import { faSearch, faSpinner, faSync, faFileAlt, faTimes, faChevronLeft, faChevr
import { removeAllNodes, appendAllNodes, showTextInNewTab } from '../../../../client/htmlUtils';
import { includes } from '../../../../common/utils';
import { replaceSwears } from '../../../../client/adminHtmlUtils';
import { addDays } from 'date-fns';
@Component({
selector: 'admin-chat-log',
@@ -25,7 +25,7 @@ export class AdminChatLog implements OnDestroy {
accounts: Account[] = [];
search?: string;
open = false;
today: ChatDate = createChatDate(moment());
today: ChatDate = createChatDate(new Date());
dates: ChatDate[] = [/*{ value: 'all', label: 'All' },*/ ...createDateRange(new Date(), 14)];
date?: ChatDate;
chatRaw?: string;
@@ -133,7 +133,9 @@ export class AdminChatLog implements OnDestroy {
}
private switchDate(days: number) {
const validDate = this.date && this.date.value !== 'all';
this.date = validDate ? createChatDate(moment(this.date!.value).add(days, 'days')) : this.today;
this.date = validDate
? createChatDate(addDays(new Date(this.date!.value), days))
: this.today;
this.refresh();
}
private stopInterval() {
+6 -4
View File
@@ -1,6 +1,6 @@
import { Component, Input, OnInit, OnDestroy, OnChanges, ElementRef, ChangeDetectionStrategy } from '@angular/core';
import * as moment from 'moment';
import { IntervalUpdateService } from '../../services/intervalUpdateService';
import { formatDistanceToNow } from 'date-fns';
@Component({
selector: 'from-now',
@@ -9,13 +9,13 @@ import { IntervalUpdateService } from '../../services/intervalUpdateService';
})
export class FromNow implements OnInit, OnDestroy, OnChanges {
@Input() time?: any;
private moment?: moment.Moment;
private date?: Date;
private text?: string;
private unsubscribe?: () => void;
constructor(private element: ElementRef, private updateService: IntervalUpdateService) {
}
ngOnChanges() {
this.moment = this.time ? moment(this.time) : undefined;
this.date = this.time ? new Date(this.time) : undefined;
this.update();
}
ngOnInit() {
@@ -26,7 +26,9 @@ export class FromNow implements OnInit, OnDestroy, OnChanges {
this.unsubscribe && this.unsubscribe();
}
private update() {
const text = this.moment ? this.moment.fromNow(true).replace('seconds', 'secs') : '';
const text = this.date
? formatDistanceToNow(this.date, { addSuffix: false }).replace('seconds', 'secs')
: '';
if (this.text !== text) {
this.text = text;
+10 -5
View File
@@ -13,8 +13,8 @@ import { faCaretUp, faArrowDown, faSearch } from '../../../client/icons';
import { sampleMessages } from '../../../common/debugData';
import { findEntityById } from '../../../client/worldMap';
import { colorToRGBA, rgb2hsl, HSL, hsl2CSS } from '../../../common/color';
import * as moment from 'moment';
import { isMobile } from '../../../client/data';
import { format } from 'date-fns';
interface IndexEntryUser {
id: number;
@@ -143,13 +143,18 @@ export function updateChatLogLine(line: ChatLogLineDOM, entry: ChatLogMessage, h
replaceNodes(line.message, message);
}
function updateTime(line: ChatLogLineDOM, hourMode?: '12' | '24') {
line.time.style.display = 'inline';
if (!hourMode) return;
if (hourMode === '24')
replaceNodes(line.timeContent, `[${moment().format('HH:mm:ss')}] `);
if (hourMode === '12')
replaceNodes(line.timeContent, `[${moment().format('LTS')}] `);
if (hourMode === '24') {
replaceNodes(line.timeContent, `[${format(new Date(), 'HH:mm:ss')}] `);
}
if (hourMode === '12') {
replaceNodes(line.timeContent, `[${format(new Date(), 'hh:mm:ss a')}] `);
}
}
function setNameColors(line: ChatLogLineDOM | undefined, colors?: string[]) {