Skip to content

High performance self-hosted photo and video management solution.

4.4 KB
Raw Blame
<script lang="ts">
  import { Route } from '#lib/route.ts';
  import { locale } from '#lib/stores/preferences.store.ts';
  import { uploadAssetsStore } from '#lib/stores/upload.ts';
  import type { UploadAsset } from '#lib/types.ts';
  import { UploadState } from '#lib/types.ts';
  import { getByteUnitString } from '#lib/utils/byte-units.ts';
  import { fileUploadHandler } from '#lib/utils/file-uploader.ts';
  import { Icon } from '@immich/ui';
  import {
    mdiAlertCircle,
    mdiCheckCircle,
    mdiCircleOutline,
    mdiClose,
    mdiLoading,
    mdiOpenInNew,
    mdiRestart,
    mdiTrashCan,
  } from '@mdi/js';
  import { t } from 'svelte-i18n';
  import { fade } from 'svelte/transition';

  interface Props {
    uploadAsset: UploadAsset;
  }

  let { uploadAsset }: Props = $props();

  const handleDismiss = () => {
    uploadAssetsStore.removeItem(uploadAsset.id);
  };

  const handleRetry = async () => {
    uploadAssetsStore.removeItem(uploadAsset.id);
    await fileUploadHandler({ files: [uploadAsset.file], albumId: uploadAsset.albumId });
  };
</script>

<div
  in:fade={{ duration: 250 }}
  out:fade={{ duration: 100 }}
  class="flex flex-col gap-1 rounded-xl border border-gray-300 bg-primary/10 p-2 text-xs dark:border-subtle"
>
  <div class="flex items-center gap-2">
    <div class="flex items-center justify-center">
      {#if uploadAsset.state === UploadState.PENDING}
        <Icon icon={mdiCircleOutline} size="24" class="text-primary" title={$t('pending')} />
      {:else if uploadAsset.state === UploadState.STARTED}
        <Icon icon={mdiLoading} size="24" spin class="text-primary" title={$t('asset_skipped')} />
      {:else if uploadAsset.state === UploadState.ERROR}
        <Icon icon={mdiAlertCircle} size="24" class="text-danger" title={$t('error')} />
      {:else if uploadAsset.state === UploadState.DUPLICATED}
        {#if uploadAsset.isTrashed}
          <Icon icon={mdiTrashCan} size="24" class="text-gray-500" title={$t('asset_skipped_in_trash')} />
        {:else}
          <Icon icon={mdiAlertCircle} size="24" class="text-warning" title={$t('asset_skipped')} />
        {/if}
      {:else if uploadAsset.state === UploadState.DONE}
        <Icon icon={mdiCheckCircle} size="24" class="text-success" title={$t('asset_uploaded')} />
      {/if}
    </div>
    <!-- <span>[{getByteUnitString(uploadAsset.file.size, $locale)}]</span> -->
    <span class="grow break-all">{uploadAsset.file.name}</span>

    {#if uploadAsset.state === UploadState.DUPLICATED && uploadAsset.assetId}
      <div class="flex items-center justify-between gap-1">
        <a
          href={uploadAsset.isTrashed
            ? Route.viewTrashedAsset({ id: uploadAsset.assetId })
            : Route.viewAsset({ id: uploadAsset.assetId })}
          target="_blank"
          rel="noopener noreferrer"
          class=""
          aria-hidden="true"
          tabindex={-1}
        >
          <Icon icon={mdiOpenInNew} size="20" />
        </a>
        <button type="button" onclick={() => handleDismiss()} class="" aria-hidden="true" tabindex={-1}>
          <Icon icon={mdiClose} size="20" />
        </button>
      </div>
    {:else if uploadAsset.state === UploadState.ERROR}
      <div class="flex items-center justify-between gap-1">
        <button type="button" onclick={() => handleRetry()} class="" aria-hidden="true" tabindex={-1}>
          <Icon icon={mdiRestart} size="20" />
        </button>
        <button type="button" onclick={() => handleDismiss()} class="" aria-hidden="true" tabindex={-1}>
          <Icon icon={mdiClose} size="20" />
        </button>
      </div>
    {/if}
  </div>

  {#if uploadAsset.state === UploadState.STARTED}
    <div class="relative mt-[5px] h-4.5 w-full rounded-md bg-gray-300 text-black dark:bg-gray-700">
      <div class="h-4.5 rounded-md bg-immich-primary transition-all" style={`width: ${uploadAsset.progress}%`}></div>
      <p class="absolute top-0.5 size-full text-center text-[10px] text-white">
        {#if uploadAsset.message === $t('asset_hashing')}
          {uploadAsset.message}
        {:else}
          {uploadAsset.message}
          {uploadAsset.progress}% - {getByteUnitString(uploadAsset.speed || 0, $locale)}/s - {uploadAsset.eta}s
        {/if}
      </p>
    </div>
  {/if}

  {#if uploadAsset.state === UploadState.ERROR}
    <div class="flex flex-row justify-between">
      <p class="w-full rounded-md text-justify text-danger">
        {uploadAsset.error}
      </p>
    </div>
  {/if}
</div>