Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

5.2 KB
Raw Blame
import type { HTMLAttributes } from 'react'
import { ArrowRight, Bell, Check, Mail, Trash2, X } from 'lucide-react'
import { useTranslation } from '../../../i18n'
import type { InAppNotification } from '../../../store/inAppNotificationStore'
import { compactTime, useNotificationItemActions } from '../../../components/Notifications/useNotificationItemActions'
import { localizedParams } from '../../../components/Notifications/notificationModel'
import MChip from '../../components/MChip'

interface MNotificationRowProps {
  notification: InAppNotification
}

/**
 * One notification as a widget-panel row (avatar tile, title + Geist subtext,
 * --m-rowbr divider). Tapping the row marks it read; boolean/navigate actions
 * render as chips below the text, per-row delete sits in the trailing column.
 */
export default function MNotificationRow({ notification }: MNotificationRowProps) {
  const { t, locale } = useTranslation()
  const { handleRespond, handleNavigate, markRead, markUnread, deleteNotification } = useNotificationItemActions(notification)

  const title = t(notification.title_key, notification.title_params)
  const body = t(notification.text_key, localizedParams(notification.text_params, locale))

  // Tapping an unread row marks it read; a row that is already read carries no
  // action, so it neither takes focus nor claims a role. It stays a div because
  // the chips and the delete button inside it are buttons of their own.
  const markReadOnRow: HTMLAttributes<HTMLDivElement> = notification.is_read
    ? {}
    : {
        role: 'button',
        tabIndex: 0,
        onClick: () => markRead(notification.id),
        onKeyDown: e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); markRead(notification.id) } },
      }

  return (
    <div
      className="flex items-start gap-[11px] border-b border-[color:var(--m-rowbr)] py-3 last:border-b-0"
      {...markReadOnRow}
    >
      {notification.sender_avatar ? (
        <img src={notification.sender_avatar} alt="" className="h-8 w-8 flex-none rounded-full object-cover" />
      ) : (
        <div className="flex h-8 w-8 flex-none items-center justify-center rounded-full bg-[color:var(--m-ic)] text-[0.75rem] font-bold text-m-ink">
          {notification.sender_username
            ? notification.sender_username.charAt(0).toUpperCase()
            : <Bell size={14} strokeWidth={2} className="text-m-muted" />}
        </div>
      )}

      <div className="min-w-0 flex-1">
        <p className="text-[0.8125rem] font-semibold leading-snug text-m-ink">{title}</p>
        <p className="mt-[2px] font-geist text-[0.65625rem] leading-relaxed text-m-muted">{body}</p>

        {notification.type === 'boolean' && notification.positive_text_key && notification.negative_text_key && (
          <div className="mt-2 flex gap-[6px]" role="presentation" onClick={e => e.stopPropagation()}>
            <MChip
              active={notification.response === 'positive'}
              onClick={() => handleRespond('positive')}
              className={notification.response === 'negative' ? 'opacity-50' : ''}
            >
              <Check size={12} strokeWidth={2.2} />
              {t(notification.positive_text_key)}
            </MChip>
            <MChip
              active={notification.response === 'negative'}
              onClick={() => handleRespond('negative')}
              className={notification.response === 'positive' ? 'opacity-50' : ''}
            >
              <X size={12} strokeWidth={2.2} />
              {t(notification.negative_text_key)}
            </MChip>
          </div>
        )}

        {notification.type === 'navigate' && notification.navigate_text_key && notification.navigate_target && (
          <div className="mt-2" role="presentation" onClick={e => e.stopPropagation()}>
            <MChip onClick={handleNavigate}>
              <ArrowRight size={12} strokeWidth={2.2} />
              {t(notification.navigate_text_key)}
            </MChip>
          </div>
        )}
      </div>

      <div className="flex flex-none flex-col items-end gap-[6px]">
        <div className="flex items-center gap-[6px]">
          {!notification.is_read && (
            <span aria-hidden className="h-[7px] w-[7px] flex-none rounded-full bg-m-ink" />
          )}
          <span className="font-geist text-[0.625rem] font-semibold text-m-faint">
            {compactTime(notification.created_at, t('common.justNow'))}
          </span>
        </div>
        {notification.is_read && (
          <button
            type="button"
            aria-label={t('notifications.markUnread')}
            onClick={e => { e.stopPropagation(); markUnread(notification.id) }}
            className="flex h-7 w-7 items-center justify-center rounded-full text-m-faint active:bg-[color:var(--m-ic)]"
          >
            <Mail size={14} strokeWidth={2} />
          </button>
        )}
        <button
          type="button"
          aria-label={t('notifications.delete')}
          onClick={e => { e.stopPropagation(); deleteNotification(notification.id) }}
          className="flex h-7 w-7 items-center justify-center rounded-full text-m-faint active:bg-[color:var(--m-ic)]"
        >
          <Trash2 size={14} strokeWidth={2} />
        </button>
      </div>
    </div>
  )
}