Skip to content

High performance self-hosted photo and video management solution.

3.8 KB
Raw Blame
<script lang="ts">
  import { NotificationLevel, NotificationType, type NotificationDto } from '@immich/sdk';
  import { IconButton, Stack, Text } from '@immich/ui';
  import {
    mdiBackupRestore,
    mdiImageAlbum,
    mdiImagePlus,
    mdiInformationOutline,
    mdiMessageBadgeOutline,
    mdiSync,
  } from '@mdi/js';
  import { DateTime } from 'luxon';

  interface Props {
    notification: NotificationDto;
    onclick: (notification: NotificationDto) => void;
  }

  let { notification, onclick }: Props = $props();

  const getAlertColor = (level: NotificationLevel) => {
    switch (level) {
      case NotificationLevel.Error: {
        return 'danger';
      }
      case NotificationLevel.Warning: {
        return 'warning';
      }
      case NotificationLevel.Info: {
        return 'primary';
      }
      case NotificationLevel.Success: {
        return 'success';
      }
      default: {
        return 'primary';
      }
    }
  };

  const getIconBgColor = (level: NotificationLevel) => {
    switch (level) {
      case NotificationLevel.Error: {
        return 'bg-red-500 dark:bg-red-300 dark:hover:bg-red-200';
      }
      case NotificationLevel.Warning: {
        return 'bg-amber-500 dark:bg-amber-200 dark:hover:bg-amber-200';
      }
      case NotificationLevel.Info: {
        return 'bg-blue-500 dark:bg-blue-200 dark:hover:bg-blue-200';
      }
      case NotificationLevel.Success: {
        return 'bg-green-500 dark:bg-green-200 dark:hover:bg-green-200';
      }
    }
  };

  const getIconType = (type: NotificationType) => {
    switch (type) {
      case NotificationType.BackupFailed: {
        return mdiBackupRestore;
      }
      case NotificationType.JobFailed: {
        return mdiSync;
      }
      case NotificationType.SystemMessage: {
        return mdiMessageBadgeOutline;
      }
      case NotificationType.Custom: {
        return mdiInformationOutline;
      }

      case NotificationType.AlbumInvite: {
        return mdiImageAlbum;
      }

      case NotificationType.AlbumUpdate: {
        return mdiImagePlus;
      }

      default: {
        return mdiInformationOutline;
      }
    }
  };

  const formatRelativeTime = (dateString: string): string => {
    try {
      const date = DateTime.fromISO(dateString);
      if (!date.isValid) {
        return dateString; // Return original string if parsing fails
      }
      // Use Luxon's toRelative with the current locale
      return date.setLocale('en').toRelative() || dateString;
    } catch (error) {
      console.error('Error formatting relative time:', error);
      return dateString; // Fallback to original string on error
    }
  };
</script>

<button
  class="min-h-20 w-full border-b border-gray-200 p-2 py-3 hover:bg-immich-primary/10 dark:border-immich-dark-gray dark:hover:bg-immich-dark-primary/10"
  type="button"
  onclick={() => onclick(notification)}
  title={notification.createdAt}
>
  <div class="grid grid-cols-[56px_1fr_32px] items-center gap-2">
    <div class="flex place-content-center place-items-center">
      <IconButton
        icon={getIconType(notification.type)}
        color={getAlertColor(notification.level)}
        aria-label={notification.title}
        shape="round"
        class={getIconBgColor(notification.level)}
        size="small"
      ></IconButton>
    </div>

    <Stack class="text-left" gap={1}>
      <Text size="tiny" class="text-base text-black dark:text-white" fontWeight="semi-bold">{notification.title}</Text>
      {#if notification.description}
        <Text class="overflow-hidden text-gray-600 dark:text-gray-300">{notification.description}</Text>
      {/if}

      <Text size="tiny" color="muted">{formatRelativeTime(notification.createdAt)}</Text>
    </Stack>

    {#if !notification.readAt}
      <div class="size-2 justify-self-center rounded-full bg-primary text-right"></div>
    {/if}
  </div>
</button>