| 1 | "use client"; |
| 2 | |
| 3 | import { useQueryClient } from |
| 4 | import { Mail, MailOpen |
| 5 | import Link from "next/link |
| 6 | import { useAction } |
| 7 | import { toast } |
| 8 | |
| 9 | import { FromNowHoverCard } |
| 10 | import { setNotificationUnread } |
| 11 | import { |
| 12 | type Notification, |
| 13 | UNREAD_COUNT_KEY, |
| 14 | } from "@/components/notifications/common"; |
| 15 | import { Badge } |
| 16 | import { Button } |
| 17 | import { cn } |
| 18 | import { ThreadStateIcon } |
| 19 | |
| 20 | const REASONS: Record |
| 21 | assigned: "Assigned", |
| 22 | mentioned: "Mentioned", |
| 23 | team_mentioned: "Team mentioned", |
| 24 | author: "Author", |
| 25 | subscribed: "Subscribed", |
| 26 | watching: "Watching", |
| 27 | }; |
| 28 | |
| 29 | const VERBS: Record |
| 30 | "issue.opened": "opened this", |
| 31 | "issue.commented": "commented", |
| 32 | "issue.closed": "closed this", |
| 33 | "issue.reopened": "reopened this", |
| 34 | "issue.assigned": "assigned someone", |
| 35 | "pull_request.merged": "merged this", |
| 36 | "pull_request.reviewed": "reviewed this", |
| 37 | "pull_request.review_commented": "replied to a review", |
| 38 | }; |
| 39 | |
| 40 | export function NotificationItem({ |
| 41 | notification, |
| 42 | }: { |
| 43 | notification: Notification |
| 44 | }) { |
| 45 | const queryClient = |
| 46 | const { execute, |
| 47 | onSuccess: () => |
| 48 | queryClient.invalidateQueries({ |
| 49 | onError: ({ error |
| 50 | toast.error(error |
| 51 | }); |
| 52 | |
| 53 | const { repository, |
| 54 | const href = |
| 55 | const ToggleIcon = |
| 56 | |
| 57 | return ( |
| 58 | <li |
| 59 | className={cn( |
| 60 | "flex items-center gap-3 px-4 py-3 text-sm", |
| 61 | !unread && "text-muted-foreground |
| 62 | )} |
| 63 | > |
| 64 | <span |
| 65 | aria-hidden |
| 66 | className={cn( |
| 67 | "size-2 shrink-0 rounded-full", |
| 68 | unread ? "bg-primary" |
| 69 | )} |
| 70 | /> |
| 71 | <ThreadStateIcon |
| 72 | isPullRequest={thread.isPullRequest |
| 73 | state={thread.state |
| 74 | /> |
| 75 | <div className="min-w-0 flex-1 |
| 76 | <p className="truncate text-xs text-muted-foreground |
| 77 | {repository.owner}/ |
| 78 | </p> |
| 79 | <Link |
| 80 | href={href} |
| 81 | onClick={() => |
| 82 | unread && execute( |
| 83 | } |
| 84 | className={cn( |
| 85 | "block truncate hover:underline", |
| 86 | unread && "font-medium" |
| 87 | )} |
| 88 | > |
| 89 | {thread.title} |
| 90 | </Link> |
| 91 | <p className="truncate text-xs text-muted-foreground |
| 92 | {notification.actorUsername ?? |
| 93 | {VERBS[notification.eventType |
| 94 | <FromNowHoverCard date={ |
| 95 | </p> |
| 96 | </div> |
| 97 | <Badge variant=" |
| 98 | {REASONS[notification.reason |
| 99 | </Badge> |
| 100 | <Button |
| 101 | variant="ghost" |
| 102 | size="icon" |
| 103 | disabled={isExecuting} |
| 104 | aria-label={unread ? |
| 105 | title={unread ? |
| 106 | onClick={() => |
| 107 | > |
| 108 | <ToggleIcon /> |
| 109 | </Button> |
| 110 | </li> |
| 111 | ); |
| 112 | } |