Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

9.5 KB
Raw Blame
import { KeyValuePipe } from '@angular/common'
import { HttpErrorResponse } from '@angular/common/http'
import { Component, ElementRef, inject, ViewChild } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { LucideArrowDown, LucideArrowUp, LucideDynamicIcon, LucideHardDriveUpload, LucidePencil, LucidePlus, LucideRotateCw } from '@lucide/angular'
import { ContextMenuComponent, ContextMenuModule } from '@perfectmemory/ngx-contextmenu'
import { L10N_LOCALE, L10nLocale, L10nTranslateDirective, L10nTranslatePipe } from 'angular-l10n'
import { BsModalRef } from 'ngx-bootstrap/modal'
import { TooltipModule } from 'ngx-bootstrap/tooltip'
import { take } from 'rxjs/operators'
import { BadgeMembersComponent } from '../../../common/components/badge-members.component'
import { StorageUsageComponent } from '../../../common/components/storage-usage.component'
import { VirtualScrollComponent } from '../../../common/components/virtual-scroll.component'
import { TapDirective } from '../../../common/directives/tap.directive'
import { TableHeaderConfig } from '../../../common/interfaces/table.interface'
import { SearchFilterPipe } from '../../../common/pipes/search.pipe'
import { originalOrderKeyValue } from '../../../common/utils/functions'
import { SortSettings, SortTable } from '../../../common/utils/sort-table'
import { LayoutService } from '../../../layout/layout.service'
import { NavbarSearchService } from '../../../layout/navbar/services/navbar-search.service'
import { SharedChildrenDialogComponent } from '../../shares/components/dialogs/shared-children-dialog.component'
import { SpaceDialogComponent } from '../../spaces/components/dialogs/space-dialog.component'
import type { SpaceModel } from '../../spaces/models/space.model'
import { SpacesService } from '../../spaces/services/spaces.service'
import { SPACES_ICON } from '../../spaces/spaces.constants'
import { UserAvatarStackComponent } from '../../users/components/utils/user-avatar-stack.component'
import { ADMIN_ICON, ADMIN_PATH, ADMIN_TITLE } from '../admin.constants'
import { AdminService } from '../admin.service'
import { ToBytesPipe } from '../../../common/pipes/to-bytes.pipe'

@Component({
  selector: 'app-admin-spaces',
  imports: [
    KeyValuePipe,
    L10nTranslateDirective,
    LucideDynamicIcon,
    UserAvatarStackComponent,
    VirtualScrollComponent,
    TooltipModule,
    L10nTranslatePipe,
    ContextMenuModule,
    SearchFilterPipe,
    TapDirective,
    BadgeMembersComponent,
    StorageUsageComponent,
    ToBytesPipe
  ],
  templateUrl: 'admin-spaces.component.html'
})
export class AdminSpacesComponent {
  @ViewChild(VirtualScrollComponent) scrollView: { element: ElementRef; viewPortItems: SpaceModel[]; scrollInto: (arg: SpaceModel | number) => void }
  @ViewChild('MainContextMenu', { static: true }) mainContextMenu: ContextMenuComponent<any>
  @ViewChild('TargetContextMenu', { static: true }) targetContextMenu: ContextMenuComponent<any>
  protected readonly locale = inject<L10nLocale>(L10N_LOCALE)
  protected readonly layout = inject(LayoutService)
  protected readonly navbarSearch = inject(NavbarSearchService)
  protected readonly originalOrderKeyValue = originalOrderKeyValue
  protected readonly icons = {
    SPACES: ADMIN_ICON.SPACES,
    SHARES: SPACES_ICON.SHARES,
    LucideArrowDown,
    LucideArrowUp,
    LucideRotateCw,
    LucideHardDriveUpload,
    LucidePlus,
    LucidePencil
  }
  // Sort
  protected tableHeaders: Record<'name' | 'managers' | 'storage' | 'members' | 'info' | 'modified', TableHeaderConfig> = {
    name: {
      label: 'Name',
      width: 30,
      textCenter: false,
      class: '',
      show: true,
      sortable: true
    },
    managers: {
      label: 'Managers',
      width: 12,
      class: 'd-none d-lg-table-cell',
      textCenter: true,
      show: true,
      sortable: true
    },
    storage: {
      label: 'Storage Space',
      width: 15,
      class: 'd-none d-md-table-cell',
      textCenter: true,
      show: true,
      sortable: true
    },
    members: {
      label: 'Members',
      width: 15,
      class: 'd-none d-lg-table-cell',
      textCenter: true,
      show: true
    },
    info: { label: 'Info', width: 8, textCenter: true, class: 'd-none d-lg-table-cell', show: true },
    modified: {
      label: 'Modified',
      width: 10,
      textCenter: true,
      class: 'd-none d-lg-table-cell',
      newly: 'newly',
      show: true,
      sortable: true
    }
  }
  protected loading = false
  protected spaces: SpaceModel[] = []
  protected selected: SpaceModel = null
  protected storageUsage = 0
  private readonly activatedRoute = inject(ActivatedRoute)
  private readonly spacesService = inject(SpacesService)
  private readonly adminService = inject(AdminService)
  private readonly sortSettings: SortSettings = {
    default: [{ prop: 'name', type: 'string' }],
    name: [{ prop: 'name', type: 'string' }],
    managers: [{ prop: 'managers', type: 'length' }],
    storage: [{ prop: 'storageUsage', type: 'number' }],
    modified: [{ prop: 'modifiedAt', type: 'date' }]
  }
  protected sortTable = new SortTable(this.constructor.name, this.sortSettings)
  // States
  private focusOnSelect: string

  constructor() {
    this.loadSpaces()
    this.layout.setBreadcrumbIcon(ADMIN_ICON.SPACES)
    this.layout.setBreadcrumbNav({
      url: `/${ADMIN_PATH.BASE}/${ADMIN_PATH.SPACES}/${ADMIN_TITLE.SPACES}`,
      splicing: 2,
      translating: true,
      sameLink: true
    })
    this.activatedRoute.queryParams.subscribe((params) => (this.focusOnSelect = params.select))
  }

  loadSpaces() {
    this.loading = true
    this.onSelect()
    this.adminService.listSpaces().subscribe({
      next: (spaces: SpaceModel[]) => {
        void this.calcUsageStorage(spaces)
        this.sortBy(this.sortTable.sortParam.column, false, spaces)
        this.loading = false
        if (this.focusOnSelect) {
          this.focusOn(this.focusOnSelect)
        } else {
          this.scrollView.scrollInto(-1)
        }
      },
      error: (e: HttpErrorResponse) => {
        this.spaces = []
        this.loading = false
        this.layout.sendNotification('error', 'Spaces', e.error.message)
      }
    })
  }

  onSelect(space: SpaceModel = null) {
    this.selected = space ?? null
  }

  sortBy(column: string, toUpdate = true, collection?: SpaceModel[]) {
    this.spaces = this.sortTable.sortBy(column, toUpdate, collection || this.spaces)
  }

  onContextMenu(ev: any) {
    ev.preventDefault()
    ev.stopPropagation()
    this.layout.openContextMenu(ev, this.mainContextMenu)
  }

  onTargetContextMenu(ev: any, space: SpaceModel) {
    ev.preventDefault()
    if (ev.type === 'contextmenu') {
      ev.stopPropagation()
    }
    this.onSelect(space)
    this.layout.openContextMenu(ev, this.targetContextMenu)
  }

  openSpaceDialog(add = false) {
    if (add) {
      const modalRef: BsModalRef<SpaceDialogComponent> = this.layout.openDialog(SpaceDialogComponent, 'xl', {
        initialState: { fromAdmin: true } as SpaceDialogComponent
      })
      modalRef.content.spaceChange.pipe(take(1)).subscribe((r: ['add' | string, SpaceModel]) => {
        const [action, s] = r
        if (action === 'add') {
          this.sortBy(this.sortTable.sortParam.column, false, this.spaces.concat(s))
          this.onSelect(s)
        }
      })
    } else if (this.selected) {
      this.spacesService.getSpace(this.selected.id).subscribe({
        next: (space: SpaceModel) => {
          const modalRef: BsModalRef<SpaceDialogComponent> = this.layout.openDialog(SpaceDialogComponent, 'xl', {
            initialState: { space: space, fromAdmin: true } as SpaceDialogComponent
          })
          modalRef.content.spaceChange.pipe(take(1)).subscribe((r: ['update' | 'delete' | string, SpaceModel]) => {
            const [action, s] = r
            if (action === 'update') {
              this.selected.name = s.name
              this.selected.alias = s.alias
              this.selected.description = s.description
              this.selected.storageUsage = s.storageUsage
              this.selected.storageQuota = s.storageQuota
              // hook to keep the shares count
              this.selected.counts = { ...s.counts, shares: this.selected.counts.shares }
              this.selected.modifiedAt = s.modifiedAt
              this.selected.enabled = s.enabled
              this.selected.managers = [...s.managers]
            } else if (action === 'delete') {
              this.onSelect()
              this.sortBy(
                this.sortTable.sortParam.column,
                false,
                this.spaces.filter((sp: SpaceModel) => sp.id !== s.id)
              )
            }
          })
        },
        error: (e: HttpErrorResponse) => this.layout.sendNotification('error', 'Edit space', this.selected.name, e)
      })
    }
  }

  openChildShareDialog(space?: SpaceModel) {
    if (space) this.onSelect(space)
    const modalRef: BsModalRef<SharedChildrenDialogComponent> = this.layout.openDialog(SharedChildrenDialogComponent, null, {
      initialState: { space: this.selected, fromAdmin: true } as SharedChildrenDialogComponent
    })
    modalRef.content.sharesCountEvent.subscribe((sharesCount: number) => (this.selected.counts.shares = sharesCount))
  }

  private focusOn(select: string) {
    const s = this.spaces.find((space) => space.name === select)
    if (s) {
      setTimeout(() => this.scrollView.scrollInto(s), 100)
      this.onSelect(s)
    }
  }

  private async calcUsageStorage(spaces: SpaceModel[]) {
    this.storageUsage = spaces.reduce((sum, space) => sum + (space.storageUsage ?? 0), 0)
  }
}