Files
FitOS/app/src/components/FeedItemRow.tsx

126 lines
4.3 KiB
TypeScript

import {
CalendarX,
Clock,
Building2,
Dumbbell,
Megaphone,
MessageSquare,
Share2,
type LucideIcon,
} from 'lucide-react';
import { useQueryClient } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import i18n from '../i18n';
import { markFeedItemRead } from '../api/queries';
import type { FeedItem } from '../api/types';
import { timeAgo } from '../lib/format';
import { useToast } from './toast';
/** Deeplink wpisu feedu; null = wpis bez przejścia. */
export function feedDeeplink(item: FeedItem): string | null {
if (item.source === 'notification') {
if (item.type === 'PLAN_READY' || item.type === 'PLAN_SHARED') {
const planId = item.payload.planId;
return typeof planId === 'string' ? `/plans/${planId}` : null;
}
if (item.type === 'TRAINER_COMMENT') {
const sessionId = item.payload.sessionId;
return typeof sessionId === 'string'
? `/profile/history?session=${sessionId}`
: '/profile/history';
}
return null;
}
return `/announcements/${item.id}`;
}
/** Ikona + podpis źródła wpisu (wspólna dla karty dashboardu i strony aktualności). */
export function feedItemIcon(item: FeedItem): { Icon: LucideIcon; label: string } {
if (item.source === 'notification') {
switch (item.type) {
case 'PLAN_READY':
return { Icon: Dumbbell, label: i18n.t('feed:itemType.planFromTrainer') };
case 'PLAN_SHARED':
return { Icon: Share2, label: i18n.t('feed:itemType.sharedPlan') };
case 'GYM_DELETED':
return { Icon: Building2, label: i18n.t('feed:itemType.gymDeleted') };
case 'TRAINER_COMMENT':
return { Icon: MessageSquare, label: i18n.t('feed:itemType.trainerComment') };
}
}
switch (item.kind) {
case 'MANUAL':
return { Icon: Megaphone, label: item.gymName ?? i18n.t('feed:itemType.announcement') };
case 'HOURS_CHANGE':
return { Icon: Clock, label: item.gymName ?? i18n.t('feed:itemType.hoursChange') };
case 'EXCEPTION':
return { Icon: CalendarX, label: item.gymName ?? i18n.t('feed:itemType.exception') };
}
}
/**
* Wiersz wpisu „Aktualności": klik = oznaczenie przeczytania + deeplink.
* `compact` (karta dashboardu) pomija treść ogłoszenia.
*/
export function FeedItemRow({ item, compact = false }: { item: FeedItem; compact?: boolean }) {
const navigate = useNavigate();
const queryClient = useQueryClient();
const { push } = useToast();
const { Icon, label } = feedItemIcon(item);
const to = feedDeeplink(item);
const attribution =
item.source === 'announcement' && item.authorName && item.authorRole
? `${item.gymName ?? i18n.t('feed:platform')} · ${item.authorName} (${i18n.t('feed:authorRole.' + item.authorRole)})`
: label;
const open = async () => {
if (!item.read) {
try {
await markFeedItemRead(item);
await queryClient.invalidateQueries({ queryKey: ['feed'] });
} catch (err) {
push((err as Error).message, 'error');
return;
}
}
if (to) navigate(to);
};
return (
<button
type="button"
onClick={() => void open()}
className={`flex w-full items-start gap-3 rounded-[10px] px-2 py-2.5 text-left transition-colors hover:bg-line/20 ${
to ? 'cursor-pointer' : 'cursor-default'
}`}
>
<span
aria-hidden="true"
className={`mt-0.5 grid h-8 w-8 shrink-0 place-items-center rounded-[10px] ${
item.source === 'notification' && item.type === 'GYM_DELETED'
? 'bg-danger/10 text-danger'
: 'bg-accent/12 text-accent-text'
}`}
>
<Icon className="h-4 w-4" />
</span>
<span className="min-w-0 flex-1">
<span
className={`block truncate text-[0.9rem] ${item.read ? 'font-semibold' : 'font-extrabold text-ink'}`}
>
{item.title}
</span>
<span className="mt-0.5 block truncate text-dim text-[0.78rem]">
{attribution} · {timeAgo(item.createdAt)}
</span>
{!compact && item.source === 'announcement' && (
<span className="mt-1 block line-clamp-3 text-dim text-[0.8125rem]">{item.body}</span>
)}
</span>
{!item.read && (
<span aria-hidden="true" className="mt-2.5 h-2 w-2 shrink-0 rounded-full bg-accent" />
)}
</button>
);
}