import { Location } from '@angular/common'; import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, NgZone, computed, effect, inject, input, signal, untracked, viewChild, } from '@angular/core'; import { MatButton } from '@angular/material/button'; import { Router, RouterLink } from '@angular/router'; import { Store } from '@ngrx/store'; import { Icon } from '../../shared/icon/icon'; import { GorodarActions } from '../../state'; @Component({ selector: 'app-collect-success', imports: [Icon, RouterLink, MatButton], templateUrl: './collect-success.html', styleUrl: './collect-success.scss', schemas: [CUSTOM_ELEMENTS_SCHEMA], changeDetection: ChangeDetectionStrategy.OnPush, }) export class CollectSuccess { private readonly store = inject(Store); private readonly location = inject(Location); private readonly router = inject(Router); private readonly zone = inject(NgZone); private readonly viewer = viewChild>('viewer'); readonly mode = input<'claim' | 'preview'>('claim'); readonly title = input.required(); readonly image = input.required(); readonly model = input(undefined); readonly cityName = input(''); readonly opened = input(0); readonly total = input(1); readonly areaId = input(''); readonly alreadyVisited = input(false); protected readonly view = signal<'model' | 'image'>('model'); protected readonly modelLoaded = signal(false); constructor() { effect(() => { const src = this.model(); untracked(() => { this.view.set('model'); this.modelLoaded.set(false); }); if (!src) { return; } void import('@google/model-viewer/dist/model-viewer.min.js'); }); effect((onCleanup) => { const element = this.viewer()?.nativeElement; if (!element) { return; } const markLoaded = (): void => { this.zone.run(() => this.modelLoaded.set(true)); }; if (element.loaded) { markLoaded(); } element.addEventListener('load', markLoaded); onCleanup(() => element.removeEventListener('load', markLoaded)); }); } protected showModel(): void { this.view.set('model'); } protected showImage(): void { this.view.set('image'); } protected readonly from = computed(() => { const total = this.total(); if (total <= 0) { return 0; } return Math.max(0, Math.round(((this.opened() - 1) / total) * 100)); }); protected readonly to = computed(() => { const total = this.total(); if (total <= 0) { return 0; } return Math.round((this.opened() / total) * 100); }); protected readonly rays = Array.from({ length: 24 }, (_, index) => { const angle = (index / 24) * Math.PI * 2; const inner = 96; const outer = index % 2 === 0 ? 168 : 138; return { x1: 170 + Math.cos(angle) * inner, y1: 170 + Math.sin(angle) * inner, x2: 170 + Math.cos(angle) * outer, y2: 170 + Math.sin(angle) * outer, strong: index % 2 === 0, }; }); protected leave(): void { this.store.dispatch(GorodarActions.claimReset()); } protected goBack(): void { if (window.history.length > 1) { this.location.back(); return; } const areaId = this.areaId(); void this.router.navigate(areaId ? ['/city', areaId] : ['/']); } } interface ModelViewerElement extends HTMLElement { loaded: boolean; }