Skip to content

High performance self-hosted photo and video management solution.

5.1 KB
Raw Blame
<script lang="ts">
  import { goto } from '$app/navigation';
  import type { Action } from '#lib/components/asset-viewer/actions/action.ts';
  import DownloadAction from '#lib/components/timeline/actions/DownloadAction.svelte';
  import RemoveFromSharedLink from '#lib/components/timeline/actions/RemoveFromSharedLinkAction.svelte';
  import AssetSelectControlBar from '#lib/components/timeline/AssetSelectControlBar.svelte';
  import { AssetAction } from '#lib/constants.ts';
  import { assetMultiSelectManager } from '#lib/managers/asset-multi-select-manager.svelte.ts';
  import { authManager } from '#lib/managers/auth-manager.svelte.ts';
  import type { Viewport } from '#lib/managers/timeline-manager/types.ts';
  import { Route } from '#lib/route.ts';
  import { dragAndDropFilesStore } from '#lib/stores/drag-and-drop-files.store.ts';
  import { mediaQueryManager } from '#lib/stores/media-query-manager.svelte.ts';
  import { handlePromiseError } from '#lib/utils.ts';
  import { downloadArchive } from '#lib/utils/asset-utils.ts';
  import { fileUploadHandler, openFileUploadDialog } from '#lib/utils/file-uploader.ts';
  import { handleError } from '#lib/utils/handle-error.ts';
  import { toTimelineAsset } from '#lib/utils/timeline-util.ts';
  import { getAssetInfo, type SharedLinkResponseDto } from '@immich/sdk';
  import { IconButton, Logo, toastManager } from '@immich/ui';
  import { mdiDownload, mdiFileImagePlusOutline, mdiSelectAll } from '@mdi/js';
  import { t } from 'svelte-i18n';
  import ControlAppBar from '../shared-components/ControlAppBar.svelte';
  import GalleryViewer from '../shared-components/gallery-viewer/GalleryViewer.svelte';

  interface Props {
    sharedLink: SharedLinkResponseDto;
    isOwned: boolean;
  }

  let { sharedLink = $bindable(), isOwned }: Props = $props();

  const viewport: Viewport = $state({ width: 0, height: 0 });

  let assets = $derived(sharedLink.assets);

  dragAndDropFilesStore.subscribe((value) => {
    if (!(value.isDragging && value.files.length > 0)) {
      return;
    }

    handlePromiseError(handleUploadAssets(value.files));
    dragAndDropFilesStore.set({ isDragging: false, files: [] });
  });

  const downloadAssets = async () => {
    await downloadArchive(`immich-shared`, { assetIds: assets.map((asset) => asset.id) });
  };

  const handleUploadAssets = async (files: File[] = []) => {
    try {
      await (!files || files.length === 0 || !Array.isArray(files)
        ? openFileUploadDialog()
        : fileUploadHandler({ files }));

      toastManager.primary();
    } catch (error) {
      handleError(error, $t('errors.unable_to_add_assets_to_shared_link'));
    }
  };

  const handleSelectAll = () => {
    assetMultiSelectManager.selectAssets(assets.map((asset) => toTimelineAsset(asset)));
  };

  const handleAction = async (action: Action) => {
    switch (action.type) {
      case AssetAction.ARCHIVE:
      case AssetAction.DELETE:
      case AssetAction.TRASH: {
        await goto(Route.photos());
        break;
      }
      // no default
    }
  };
</script>

{#if sharedLink?.allowUpload || assets.length > 1}
  <main
    class="isolate mx-4 mt-24 mb-40 max-h-screen"
    bind:clientHeight={viewport.height}
    bind:clientWidth={viewport.width}
  >
    <GalleryViewer {assets} assetInteraction={assetMultiSelectManager} {viewport} allowDeletion={false} />
  </main>

  <header class="fixed inset-s-0 top-0 w-full">
    {#if assetMultiSelectManager.selectionActive}
      <AssetSelectControlBar>
        <IconButton
          shape="round"
          color="secondary"
          variant="ghost"
          aria-label={$t('select_all')}
          icon={mdiSelectAll}
          onclick={handleSelectAll}
        />
        {#if sharedLink?.allowDownload}
          <DownloadAction filename="immich-shared" />
        {/if}
        {#if isOwned}
          <RemoveFromSharedLink bind:sharedLink />
        {/if}
      </AssetSelectControlBar>
    {:else}
      <ControlAppBar>
        {#snippet leading()}
          <a data-sveltekit-preload-data="hover" class="ms-4" href="/">
            <Logo variant={mediaQueryManager.maxMd ? 'icon' : 'inline'} class="min-w-10" />
          </a>
        {/snippet}

        {#snippet trailing()}
          {#if sharedLink?.allowUpload}
            <IconButton
              shape="round"
              color="secondary"
              variant="ghost"
              aria-label={$t('add_photos')}
              onclick={() => handleUploadAssets()}
              icon={mdiFileImagePlusOutline}
            />
          {/if}

          {#if sharedLink?.allowDownload}
            <IconButton
              shape="round"
              color="secondary"
              variant="ghost"
              aria-label={$t('download')}
              onclick={downloadAssets}
              icon={mdiDownload}
            />
          {/if}
        {/snippet}
      </ControlAppBar>
    {/if}
  </header>
{:else if assets.length === 1}
  {#await getAssetInfo({ ...authManager.params, id: assets[0].id }) then asset}
    {#await import('#lib/components/asset-viewer/AssetViewer.svelte') then { default: AssetViewer }}
      <AssetViewer cursor={{ current: asset }} onAction={handleAction} />
    {/await}
  {/await}
{/if}