|
| 1 | +import { select, Store } from '@ngxs/store'; |
| 2 | + |
| 3 | +import { TranslatePipe } from '@ngx-translate/core'; |
| 4 | + |
| 5 | +import { Select } from 'primeng/select'; |
| 6 | +import { Tab, TabList, TabPanel, TabPanels, Tabs } from 'primeng/tabs'; |
| 7 | + |
| 8 | +import { ChangeDetectionStrategy, Component, computed, effect, inject, signal } from '@angular/core'; |
| 9 | +import { toSignal } from '@angular/core/rxjs-interop'; |
| 10 | +import { FormsModule } from '@angular/forms'; |
| 11 | + |
| 12 | +import { UserSelectors } from '@core/store/user'; |
| 13 | +import { |
| 14 | + AddonsSelectors, |
| 15 | + GetAddonsUserReference, |
| 16 | + GetAuthorizedCitationAddons, |
| 17 | + GetAuthorizedStorageAddons, |
| 18 | + GetCitationAddons, |
| 19 | + GetStorageAddons, |
| 20 | +} from '@osf/features/settings/addons/store'; |
| 21 | +import { SearchInputComponent, SubHeaderComponent } from '@shared/components'; |
| 22 | +import { AddonCardListComponent } from '@shared/components/addons'; |
| 23 | +import { IS_XSMALL } from '@shared/utils'; |
| 24 | + |
| 25 | +import { ADDON_CATEGORY_OPTIONS, ADDON_TAB_OPTIONS, AddonCategoryValue, AddonTabValue } from './addons.constants'; |
| 26 | + |
| 27 | +@Component({ |
| 28 | + selector: 'osf-addons', |
| 29 | + imports: [ |
| 30 | + AddonCardListComponent, |
| 31 | + SearchInputComponent, |
| 32 | + Select, |
| 33 | + SubHeaderComponent, |
| 34 | + Tab, |
| 35 | + TabList, |
| 36 | + TabPanel, |
| 37 | + TabPanels, |
| 38 | + Tabs, |
| 39 | + TranslatePipe, |
| 40 | + FormsModule, |
| 41 | + ], |
| 42 | + templateUrl: './addons.component.html', |
| 43 | + styleUrl: './addons.component.scss', |
| 44 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 45 | +}) |
| 46 | +export class AddonsComponent { |
| 47 | + protected readonly AddonTabValue = AddonTabValue; |
| 48 | + #store = inject(Store); |
| 49 | + protected readonly defaultTabValue = AddonTabValue.ALL_ADDONS; |
| 50 | + protected readonly isMobile = toSignal(inject(IS_XSMALL)); |
| 51 | + protected readonly searchValue = signal(''); |
| 52 | + protected readonly selectedCategory = signal<string>(AddonCategoryValue.EXTERNAL_STORAGE_SERVICES); |
| 53 | + protected readonly selectedTab = signal<number>(this.defaultTabValue); |
| 54 | + protected readonly currentUser = select(UserSelectors.getCurrentUser); |
| 55 | + protected readonly addonsUserReference = select(AddonsSelectors.getAddonUserReference); |
| 56 | + protected readonly storageAddons = select(AddonsSelectors.getStorageAddons); |
| 57 | + protected readonly citationAddons = select(AddonsSelectors.getCitationAddons); |
| 58 | + protected readonly authorizedStorageAddons = select(AddonsSelectors.getAuthorizedStorageAddons); |
| 59 | + protected readonly authorizedCitationAddons = this.#store.selectSignal(AddonsSelectors.getAuthorizedCitationAddons); |
| 60 | + protected readonly allAuthorizedAddons = computed(() => { |
| 61 | + const authorizedAddons = [...this.authorizedStorageAddons(), ...this.authorizedCitationAddons()]; |
| 62 | + |
| 63 | + const searchValue = this.searchValue().toLowerCase(); |
| 64 | + return authorizedAddons.filter((card) => card.displayName.includes(searchValue)); |
| 65 | + }); |
| 66 | + |
| 67 | + protected readonly userReferenceId = computed(() => { |
| 68 | + return this.addonsUserReference()[0]?.id; |
| 69 | + }); |
| 70 | + |
| 71 | + protected readonly currentAction = computed(() => |
| 72 | + this.selectedCategory() === AddonCategoryValue.EXTERNAL_STORAGE_SERVICES ? GetStorageAddons : GetCitationAddons |
| 73 | + ); |
| 74 | + |
| 75 | + protected readonly currentAddonsState = computed(() => |
| 76 | + this.selectedCategory() === AddonCategoryValue.EXTERNAL_STORAGE_SERVICES |
| 77 | + ? this.storageAddons() |
| 78 | + : this.citationAddons() |
| 79 | + ); |
| 80 | + |
| 81 | + protected readonly filteredAddonCards = computed(() => { |
| 82 | + const searchValue = this.searchValue().toLowerCase(); |
| 83 | + return this.currentAddonsState().filter((card) => card.externalServiceName.includes(searchValue)); |
| 84 | + }); |
| 85 | + |
| 86 | + protected readonly tabOptions = ADDON_TAB_OPTIONS; |
| 87 | + protected readonly categoryOptions = ADDON_CATEGORY_OPTIONS; |
| 88 | + |
| 89 | + protected onCategoryChange(value: string): void { |
| 90 | + this.selectedCategory.set(value); |
| 91 | + } |
| 92 | + |
| 93 | + constructor() { |
| 94 | + effect(() => { |
| 95 | + // Only proceed if we have the current user |
| 96 | + if (this.currentUser()) { |
| 97 | + this.#store.dispatch(GetAddonsUserReference); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + effect(() => { |
| 102 | + // Only proceed if we have both current user and user reference |
| 103 | + if (this.currentUser() && this.userReferenceId()) { |
| 104 | + this.#loadAddonsIfNeeded(this.userReferenceId()); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + #loadAddonsIfNeeded(userReferenceId: string): void { |
| 110 | + const action = this.currentAction(); |
| 111 | + const addons = this.currentAddonsState(); |
| 112 | + |
| 113 | + if (!addons?.length) { |
| 114 | + this.#store.dispatch(action); |
| 115 | + this.#store.dispatch(new GetAuthorizedStorageAddons(userReferenceId)); |
| 116 | + this.#store.dispatch(new GetAuthorizedCitationAddons(userReferenceId)); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments