ngOnInit()

in web/frontend/src/app/pages/streams/components/streams-list/streams-list.component.ts [117:256]


  ngOnInit() {
    this.isWriter$ = this.permissionsService.isWriter();
    this.appStore
      .pipe(select(getActiveOrFirstTab), takeUntil(this.destroy$))
      .subscribe((activeTab) => {
        this.activeTabType = activeTab?.type || null;
      });


    this.streamsService.streamRemoved
      .pipe(takeUntil(this.destroy$))
      .subscribe((streamId) => this.onItemDeleted([streamId]));

    this.streamsService.streamNameUpdated
      .pipe(takeUntil(this.destroy$))
      .subscribe(({ streamId }) => {
        this.onItemDeleted([streamId]);
        this.onItemAdded(streamId);
    })

    this.emptyListTitle$ = this.leftSidebarStorageService.watchStorage().pipe(
      switchMap(storage => this.translate.get(`streamList.empty.${storage.search ? 'hasSearch' : 'noSearch'}.${storage.treeView}`, {search: storage.search})),
    );
    
    const showSpaces$ = this.globalFiltersService.getFilters().pipe(
      map((f) => f?.showSpaces),
      distinctUntilChanged(),
    );
    
    showSpaces$.pipe(takeUntil(this.destroy$)).subscribe((showSpaces) => {
      this.toggleSpaces(showSpaces);
    });
    
    const freshMenu$ = this.leftSidebarStorageService.watchStorage().pipe(
      map(storage => ({
        search: storage.search,
        treeView: storage.treeView,
        searchOptions: storage.search ? storage.searchOptions : null,
      })),
      filter(storage => storage.treeView !== undefined && storage.search !== undefined),
      distinctUntilChanged(equal),
    );
    
    freshMenu$.pipe(
      takeUntil(this.destroy$),
      switchMap(() => this.leftSidebarStorageService.getStorage()),
      switchMap(({search, treeView}) => {
        this.menuLoaded = false;
        this.treeViewId = treeView;
        this.cdRef.detectChanges();
        return this.freshMenu(false);
      }),
      catchError(() => {
        return this.leftSidebarStorageService.updatePath(() => []).pipe(switchMap(() => this.freshMenu(false)));
      }),
      delay(0),
    ).subscribe(() => {
      this.menuLoaded = true;
      this.cdRef.detectChanges();
      this.streamsNavigationScrollService.scrollToActiveMenu();
    });
    
    this.streamsNavigationScrollService
      .onScrollToActiveMenu()
      .pipe(takeUntil(this.destroy$), delay(0))
      .subscribe(() => this.scrollToActiveMenu());
    
    this.leftSidebarStorageService.watchStorage().pipe(map(storage => storage.treeView), distinctUntilChanged()).pipe(
      switchMap((treeView) => this.structureUpdatesService.onUpdates().pipe(map(updates => ({treeView, updates})))),
      map(({updates, treeView}) => updates.filter(update =>
        (update.type === StructureUpdateType.stream && treeView === 'streams') || (update.type === StructureUpdateType.view && treeView === 'views')),
      ),
      takeUntil(this.destroy$),
    ).subscribe((events) => {
      const updatedStreams = [];
      events.forEach(event => {
        switch (event.action) {
          case StructureUpdateAction.rename:
            this.onStreamRenamed(event.id, event.target);
            break;
          case StructureUpdateAction.update:
            updatedStreams.push(event.viewMd?.stream || event.id);
            break;
          case StructureUpdateAction.add:
            this.onItemAdded(event.viewMd?.stream || event.id);
            break;
          case StructureUpdateAction.remove:
            this.onItemDeleted([event.viewMd?.stream || event.id]);
            break;
        }
      });
      
      if (updatedStreams.length) {
        this.onStreamChanged(updatedStreams);
      }
      
      this.menuItemsService.clearCache();
    });
    
    this.spaceRenameService
      .onSpaceRenamed()
      .pipe(
        takeUntil(this.destroy$),
        switchMap(({streamId, oldName, newName}) => {
          let spacePath = null;
          const oldNameEncoded = encodeURIComponent(oldName);
          const newNameEncoded = encodeURIComponent(newName);
          
          this.recursiveMenu((item, path) => {
            if (
              item.meta.stream?.id === streamId &&
              item.type === MenuItemType.space &&
              item.id === oldName
            ) {
              item.id = item.name = newName;
              spacePath = path;
            }
          });
          
          this.addMeta();
          this.makeFlatMenu();
          
          const newPath =
            spacePath.substr(0, spacePath.length - oldNameEncoded.length) + newNameEncoded;
          return this.leftSidebarStorageService.updatePath((paths) =>
            paths.map((path) =>
              path.startsWith(spacePath) ? newPath + path.substr(spacePath.length) : path,
            ),
          );
        }),
      )
      .subscribe(() => this.cdRef.detectChanges());

    this.importFromTextFileService.streamIdSubject
      .pipe(takeUntil(this.destroy$))
      .subscribe(streamId => {
        this.streamSelectedForImport = streamId;
        this.cdRef.detectChanges();
      })
  }