-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcapture-item.component.ts
114 lines (102 loc) · 3.41 KB
/
capture-item.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Component, HostListener, Input } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { ActivatedRoute, Router } from '@angular/router';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { ReplaySubject, combineLatest, iif, of } from 'rxjs';
import {
catchError,
concatMap,
distinctUntilChanged,
first,
map,
shareReplay,
switchMap,
} from 'rxjs/operators';
import { CaptureService } from '../../../../shared/capture/capture.service';
import { DiaBackendAssetRepository } from '../../../../shared/dia-backend/asset/dia-backend-asset-repository.service';
import { getOldProof } from '../../../../shared/repositories/proof/old-proof-adapter';
import { Proof } from '../../../../shared/repositories/proof/proof';
import { normalizeGeolocation } from '../../details/information/session/information-session.service';
@UntilDestroy()
@Component({
selector: 'app-capture-item',
templateUrl: './capture-item.component.html',
styleUrls: ['./capture-item.component.scss'],
})
export class CaptureItemComponent {
private readonly proof$ = new ReplaySubject<Proof>(1);
@Input()
set proof(value: Proof | undefined) {
if (value) this.proof$.next(value);
}
private readonly oldProofHash$ = this.proof$.pipe(
map(proof => getOldProof(proof).hash)
);
readonly thumbnailUrl$ = this.proof$.pipe(
distinctUntilChanged((x, y) => getOldProof(x).hash === getOldProof(y).hash),
switchMap(proof => proof.thumbnailUrl$),
map(url => {
if (url) return this.sanitizer.bypassSecurityTrustUrl(url);
return undefined;
}),
catchError(() => of(undefined)),
shareReplay({ bufferSize: 1, refCount: true })
);
readonly hasUploaded$ = this.proof$.pipe(
map(proof => !!proof.diaBackendAssetId)
);
readonly isCollecting$ = combineLatest([
this.oldProofHash$,
this.captureService.collectingOldProofHashes$,
]).pipe(
map(([oldProofHash, collectingOldProofHashes]) =>
collectingOldProofHashes.has(oldProofHash)
),
shareReplay({ bufferSize: 1, refCount: true })
);
readonly hasGeolocation$ = this.proof$.pipe(
map(
proof =>
!!normalizeGeolocation({
latitude: proof.geolocationLatitude,
longitude: proof.geolocationLongitude,
})
)
);
readonly hasCaption$ = this.proof$.pipe(map(proof => proof.caption !== ''));
readonly isVideo$ = this.proof$.pipe(
concatMap(proof => proof.getFirstAssetMeta()),
map(meta => meta.mimeType.startsWith('video'))
);
readonly isThumbnailMissing$ = this.thumbnailUrl$.pipe(map(url => !url));
constructor(
private readonly captureService: CaptureService,
private readonly router: Router,
private readonly route: ActivatedRoute,
private readonly sanitizer: DomSanitizer,
private readonly diaBackendAssetRepository: DiaBackendAssetRepository
) {}
@HostListener('click')
async onClick() {
this.isCollecting$
.pipe(
first(),
switchMap(isCollecting =>
iif(
() => !isCollecting,
this.oldProofHash$.pipe(
first(),
concatMap(oldProofHash =>
this.router.navigate(
['details', { type: 'capture', hash: oldProofHash }],
{ relativeTo: this.route }
)
)
)
)
),
untilDestroyed(this)
)
.subscribe();
}
}