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