Sengi-Windows-MacOS-Linux/src/app/components/stream/status/status.component.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-09-19 07:03:07 +02:00
import { Component, OnInit, Input, Inject, LOCALE_ID } from "@angular/core";
import { Status } from "../../../services/models/mastodon.interfaces";
2018-09-19 07:03:07 +02:00
import { formatDate } from '@angular/common';
@Component({
2018-09-19 07:03:07 +02:00
selector: "app-status",
templateUrl: "./status.component.html",
styleUrls: ["./status.component.scss"]
})
export class StatusComponent implements OnInit {
2018-09-19 07:03:07 +02:00
@Input() status: Status;
2018-09-19 07:03:07 +02:00
constructor(@Inject(LOCALE_ID) private locale: string) { }
2018-09-19 07:03:07 +02:00
ngOnInit() {
}
2018-09-19 07:03:07 +02:00
getCompactRelativeTime(d: string): string {
const date = (new Date(d)).getTime();
const now = Date.now();
2018-09-19 07:23:19 +02:00
const timeDelta = (now - date) / (1000);
2018-09-19 07:03:07 +02:00
if (timeDelta < 60) {
2018-09-19 07:23:19 +02:00
return `${timeDelta | 0}s`;
2018-09-19 07:23:50 +02:00
} else if (timeDelta < 60 * 60) {
2018-09-19 07:23:19 +02:00
return `${timeDelta / 60 | 0}m`;
} else if (timeDelta < 60 * 60 * 24) {
return `${timeDelta / (60 * 60)| 0}h`;
} else if (timeDelta < 60 * 60 * 24 * 31) {
return `${timeDelta / (60 * 60 * 24) | 0}d`;
2018-09-19 07:03:07 +02:00
}
return formatDate(date, 'MM/dd', this.locale);
}
}