Skip to content

High performance self-hosted photo and video management solution.

6.5 KB
Raw Blame
<script lang="ts">
  import { cleanClass } from '#lib';
  import QueueCardBadge from './QueueCardBadge.svelte';
  import QueueCardButton from './QueueCardButton.svelte';
  import { Route } from '#lib/route.ts';
  import { asQueueItem } from '#lib/services/queue.service.ts';
  import { locale } from '#lib/stores/preferences.store.ts';
  import { transformToTitleCase } from '#lib/utils.ts';
  import { QueueCommand, type QueueCommandDto, type QueueResponseDto } from '@immich/sdk';
  import { Badge, Icon, IconButton, Link } from '@immich/ui';
  import {
    mdiAlertCircle,
    mdiAllInclusive,
    mdiChartLine,
    mdiClose,
    mdiFastForward,
    mdiImageRefreshOutline,
    mdiPause,
    mdiPlay,
    mdiSelectionSearch,
  } from '@mdi/js';
  import { type Component } from 'svelte';
  import { t } from 'svelte-i18n';

  interface Props {
    queue: QueueResponseDto;
    description?: Component;
    disabled?: boolean;
    allText?: string;
    refreshText?: string;
    missingText: string;
    onCommand: (command: QueueCommandDto) => void;
  }

  let { queue, description, disabled = false, allText, refreshText, missingText, onCommand }: Props = $props();

  const { icon, title, subtitle } = $derived(asQueueItem($t, queue));
  const { statistics } = $derived(queue);
  let waitingCount = $derived(statistics.waiting + statistics.delayed);
  let isIdle = $derived(statistics.active + statistics.waiting === 0 && !queue.isPaused);
  let multipleButtons = $derived(allText || refreshText);

  const commonClasses = 'flex place-items-center justify-between w-full py-2 sm:py-4 pe-4 ps-6';
</script>

<div class="flex flex-col rounded-2xl bg-gray-100 sm:flex-row dark:bg-immich-dark-gray">
  <div class="flex w-full flex-col">
    {#if queue.isPaused}
      <QueueCardBadge color="warning">{$t('paused')}</QueueCardBadge>
    {:else if statistics.active > 0}
      <QueueCardBadge color="success">{$t('active')}</QueueCardBadge>
    {/if}
    <div class="flex flex-col gap-2 p-5 sm:p-7 md:p-9">
      <div class="flex items-center gap-2 text-xl font-semibold text-primary">
        <Link class="flex items-center gap-2 hover:underline" href={Route.viewQueue(queue)} underline={false}>
          <Icon {icon} size="1.25em" class="hidden shrink-0 sm:block" />
          <span>{transformToTitleCase(title)}</span>
        </Link>
        <IconButton
          color="primary"
          icon={mdiChartLine}
          aria-label={$t('view_details')}
          size="small"
          variant="ghost"
          href={Route.viewQueue(queue)}
        />
        <div class="flex gap-2">
          {#if statistics.failed > 0}
            <Badge
              onClose={() => onCommand({ command: QueueCommand.ClearFailed, force: false })}
              translations={{ close: $t('clear_message') }}
            >
              {$t('admin.jobs_failed', { values: { jobCount: statistics.failed.toLocaleString($locale) } })}
            </Badge>
          {/if}
          {#if statistics.delayed > 0}
            <Badge>
              {$t('admin.jobs_delayed', { values: { jobCount: statistics.delayed.toLocaleString($locale) } })}
            </Badge>
          {/if}
        </div>
      </div>

      {#if subtitle}
        <div class="text-sm whitespace-pre-line dark:text-white">{subtitle}</div>
      {/if}

      {#if description}
        <div class="text-sm dark:text-white">
          {const SvelteComponent = $derived(description)}
          <SvelteComponent />
        </div>
      {/if}

      <div class="mt-2 flex w-full max-w-md flex-col sm:flex-row">
        <div
          class={cleanClass(
            commonClasses,
            'rounded-t-lg bg-immich-primary text-white dark:bg-immich-dark-primary dark:text-immich-dark-gray sm:rounded-s-lg sm:rounded-e-none',
          )}
        >
          <p>{$t('active')}</p>
          <p class="text-2xl">
            {statistics.active.toLocaleString($locale)}
          </p>
        </div>

        <div
          class={cleanClass(
            commonClasses,
            'flex-row-reverse rounded-b-lg bg-gray-200 text-immich-dark-bg dark:bg-gray-700 dark:text-immich-gray sm:rounded-s-none sm:rounded-e-lg',
          )}
        >
          <p class="text-2xl">
            {waitingCount.toLocaleString($locale)}
          </p>
          <p>{$t('waiting')}</p>
        </div>
      </div>
    </div>
  </div>
  <div class="flex w-full flex-row sm:w-32 sm:flex-col">
    {#if disabled}
      <QueueCardButton
        disabled={true}
        color="light-gray"
        onClick={() => onCommand({ command: QueueCommand.Start, force: false })}
      >
        <Icon icon={mdiAlertCircle} size="36" />
        <span>{$t('disabled')}</span>
      </QueueCardButton>
    {/if}

    {#if !disabled && !isIdle}
      {#if waitingCount > 0}
        <QueueCardButton color="gray" onClick={() => onCommand({ command: QueueCommand.Empty, force: false })}>
          <Icon icon={mdiClose} size="24" />
          <span>{$t('clear')}</span>
        </QueueCardButton>
      {/if}
      {#if queue.isPaused}
        <QueueCardButton color="light-gray" onClick={() => onCommand({ command: QueueCommand.Resume, force: false })}>
          <Icon icon={mdiFastForward} size={waitingCount > 0 ? '24' : '48'} />
          <span>{$t('resume')}</span>
        </QueueCardButton>
      {:else}
        <QueueCardButton color="light-gray" onClick={() => onCommand({ command: QueueCommand.Pause, force: false })}>
          <Icon icon={mdiPause} size="24" />
          <span>{$t('pause')}</span>
        </QueueCardButton>
      {/if}
    {/if}

    {#if !disabled && multipleButtons && isIdle}
      {#if allText}
        <QueueCardButton color="dark-gray" onClick={() => onCommand({ command: QueueCommand.Start, force: true })}>
          <Icon icon={mdiAllInclusive} size="24" />
          <span>{allText}</span>
        </QueueCardButton>
      {/if}
      {#if refreshText}
        <QueueCardButton color="gray" onClick={() => onCommand({ command: QueueCommand.Start, force: undefined })}>
          <Icon icon={mdiImageRefreshOutline} size="24" />
          <span>{refreshText}</span>
        </QueueCardButton>
      {/if}
      <QueueCardButton color="light-gray" onClick={() => onCommand({ command: QueueCommand.Start, force: false })}>
        <Icon icon={mdiSelectionSearch} size="24" />
        <span>{missingText}</span>
      </QueueCardButton>
    {/if}

    {#if !disabled && !multipleButtons && isIdle}
      <QueueCardButton color="light-gray" onClick={() => onCommand({ command: QueueCommand.Start, force: false })}>
        <Icon icon={mdiPlay} size="48" />
        <span>{missingText}</span>
      </QueueCardButton>
    {/if}
  </div>
</div>