Skip to content

Commit

Permalink
Extract relative time component for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiru committed Dec 3, 2024
1 parent 0c28552 commit 9a84a6f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<div
class="op-ian-actors--date"
[title]="fixedTime"
[textContent]="relativeTime$ | async"
></div>
<op-in-app-notification-relative-time
[notification]="notification"
/>
<div class="op-ian-actors--container">
<ng-container *ngFor="let actor of actors | slice:0:3; let idx = index; let last = last">
<span *ngIf="last && actors.length > 1 && actors.length < 4" textContent=" {{ text.and }} "></span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
align-items: center
color: var(--fgColor-muted)

&--date
@include text-shortener
max-width: 100%
line-height: 1rem

&--container
@include text-shortener
display: flex
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div
class="op-ian-relative-time"
[title]="fixedTime"
[textContent]="relativeTime$ | async">
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "helpers"

.op-ian-relative-time
@include text-shortener
max-width: 100%
line-height: 1rem
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
ChangeDetectionStrategy,
Component,
Input,
OnInit,
ViewEncapsulation,
} from '@angular/core';
import { TimezoneService } from 'core-app/core/datetime/timezone.service';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { INotification } from 'core-app/core/state/in-app-notifications/in-app-notification.model';
import { Observable, timer } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';

@Component({
selector: 'op-in-app-notification-relative-time',
templateUrl: './in-app-notification-relative-time.component.html',
styleUrls: ['./in-app-notification-relative-time.component.sass'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class InAppNotificationRelativeTimeComponent implements OnInit {
@Input() notification:INotification;
@Input() hasActorByLine:boolean = true;

// Fixed notification time
fixedTime:string;

// Format relative elapsed time (n seconds/minutes/hours ago)
// at an interval for auto updating
relativeTime$:Observable<string>;

text = {
updated_by_at: (age:string):string => this.I18n.t(
'js.notifications.center.text_update_date_by',
{ date: age },
),
};

constructor(
private I18n:I18nService,
private timezoneService:TimezoneService,
) { }

ngOnInit():void {
this.buildTime();
}

private buildTime() {
this.fixedTime = this.timezoneService.formattedDatetime(this.notification.createdAt);
this.relativeTime$ = timer(0, 10000)
.pipe(
map(() => {
const time = this.timezoneService.formattedRelativeDateTime(this.notification.createdAt);
if (this.hasActorByLine && this.notification._links.actor) {
return this.text.updated_by_at(time);
}

return time;
}),
distinctUntilChanged(),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { NgModule } from '@angular/core';
import { OpSharedModule } from 'core-app/shared/shared.module';
import { ScrollingModule } from '@angular/cdk/scrolling';
import { CommonModule } from '@angular/common';
import { IconModule } from 'core-app/shared/components/icon/icon.module';
import { NgModule } from '@angular/core';
import {
InAppNotificationBellComponent,
} from 'core-app/features/in-app-notifications/bell/in-app-notification-bell.component';
import {
InAppNotificationEntryComponent,
} from 'core-app/features/in-app-notifications/entry/in-app-notification-entry.component';
import { OpenprojectPrincipalRenderingModule } from 'core-app/shared/components/principal/principal-rendering.module';
import { ScrollingModule } from '@angular/cdk/scrolling';
import { IanBellService } from 'core-app/features/in-app-notifications/bell/state/ian-bell.service';
import {
InAppNotificationCenterComponent,
} from 'core-app/features/in-app-notifications/center/in-app-notification-center.component';
import { IanCenterService } from 'core-app/features/in-app-notifications/center/state/ian-center.service';
import {
InAppNotificationsDateAlertsUpsaleComponent,
} from 'core-app/features/in-app-notifications/date-alerts-upsale/ian-date-alerts-upsale.component';
import {
InAppNotificationEntryComponent,
} from 'core-app/features/in-app-notifications/entry/in-app-notification-entry.component';
import { OpenprojectWorkPackagesModule } from 'core-app/features/work-packages/openproject-work-packages.module';
import { DynamicModule } from 'ng-dynamic-component';
import { InAppNotificationStatusComponent } from './entry/status/in-app-notification-status.component';
import { IconModule } from 'core-app/shared/components/icon/icon.module';
import {
OpenprojectContentLoaderModule,
} from 'core-app/shared/components/op-content-loader/openproject-content-loader.module';
import { IanBellService } from 'core-app/features/in-app-notifications/bell/state/ian-bell.service';
import { OpenprojectPrincipalRenderingModule } from 'core-app/shared/components/principal/principal-rendering.module';
import { OpSharedModule } from 'core-app/shared/shared.module';
import { DynamicModule } from 'ng-dynamic-component';
import { InAppNotificationActorsLineComponent } from './entry/actors-line/in-app-notification-actors-line.component';
import { InAppNotificationDateAlertComponent } from './entry/date-alert/in-app-notification-date-alert.component';
import {
InAppNotificationsDateAlertsUpsaleComponent,
} from 'core-app/features/in-app-notifications/date-alerts-upsale/ian-date-alerts-upsale.component';
import { IanCenterService } from 'core-app/features/in-app-notifications/center/state/ian-center.service';
import { InAppNotificationRelativeTimeComponent } from './entry/relative-time/in-app-notification-relative-time.component';
import { InAppNotificationStatusComponent } from './entry/status/in-app-notification-status.component';

@NgModule({
declarations: [
Expand All @@ -36,6 +37,7 @@ import { IanCenterService } from 'core-app/features/in-app-notifications/center/
InAppNotificationActorsLineComponent,
InAppNotificationDateAlertComponent,
InAppNotificationsDateAlertsUpsaleComponent,
InAppNotificationRelativeTimeComponent,
],
imports: [
OpSharedModule,
Expand Down

0 comments on commit 9a84a6f

Please sign in to comment.