Skip to content

Commit

Permalink
Remove dependency on dayjs
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden committed Aug 27, 2024
1 parent b17a4a1 commit b6c4ac3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@fortawesome/angular-fontawesome": "^0.15.0",
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"dayjs": "^1.11.11",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
Expand Down
54 changes: 33 additions & 21 deletions src/app/components/ui/date.component.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
import {Component, Inject, Input, PLATFORM_ID} from '@angular/core';
import {Component, Input} from '@angular/core';
import {TooltipComponent} from "./text/tooltip.component";
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { isPlatformBrowser } from "@angular/common";

dayjs.extend(relativeTime);

// FIXME: this component can't be used from SSR due to hydration problems
// best to just nuke dayJs entirely and write our own function
@Component({
selector: 'app-date',
standalone: true,
imports: [
TooltipComponent
],
template: `
@if (isBrowserOnly) {
<app-tooltip [text]="getFormattedDate()">
<!--
for some stupid fucking reason we need to defer this,
because otherwise this breaks the layout when SSR renders this component
-->
@defer {
<app-tooltip [text]="getFormattedDate()" class>
{{ getMoment() }}
</app-tooltip>
} @placeholder {
<span>{{ recentText }}</span>
}
@else {
<span>...</span>
}
`
`
})
export class DateComponent {
private _date: Date = new Date();

constructor(@Inject(PLATFORM_ID) private platformId: string) {}

get isBrowserOnly(): boolean {
return isPlatformBrowser(this.platformId);
}

protected recentText = "just now";

@Input({required: true, alias: "date"})
set date(value: Date) {
this._date = new Date(value);
}

getMoment(): string {
return dayjs(this._date).fromNow();
const now = new Date();
const seconds = Math.floor((now.getTime() - this._date.getTime()) / 1000);

const intervals: { [key: string]: number } = {
year: 31536000,
month: 2592000,
week: 604800,
day: 86400,
hour: 3600,
minute: 60,
second: 1,
};

for (const interval in intervals) {
const time = Math.floor(seconds / intervals[interval]);
if (time >= 1) {
return `${time} ${interval}${time > 1 ? 's' : ''} ago`;
}
}

return this.recentText;
}

getFormattedDate(): string {
Expand Down

0 comments on commit b6c4ac3

Please sign in to comment.