|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | +import { api } from '~/trpc/react'; |
| 5 | +import { Button } from './ui/button'; |
| 6 | +import { Badge } from './ui/badge'; |
| 7 | +import { X, ExternalLink, Calendar, Tag, Loader2 } from 'lucide-react'; |
| 8 | + |
| 9 | +interface ReleaseNotesModalProps { |
| 10 | + isOpen: boolean; |
| 11 | + onClose: () => void; |
| 12 | + highlightVersion?: string; |
| 13 | +} |
| 14 | + |
| 15 | +interface Release { |
| 16 | + tagName: string; |
| 17 | + name: string; |
| 18 | + publishedAt: string; |
| 19 | + htmlUrl: string; |
| 20 | + body: string; |
| 21 | +} |
| 22 | + |
| 23 | +// Helper functions for localStorage |
| 24 | +const getLastSeenVersion = (): string | null => { |
| 25 | + if (typeof window === 'undefined') return null; |
| 26 | + return localStorage.getItem('LAST_SEEN_RELEASE_VERSION'); |
| 27 | +}; |
| 28 | + |
| 29 | +const markVersionAsSeen = (version: string): void => { |
| 30 | + if (typeof window === 'undefined') return; |
| 31 | + localStorage.setItem('LAST_SEEN_RELEASE_VERSION', version); |
| 32 | +}; |
| 33 | + |
| 34 | +export function ReleaseNotesModal({ isOpen, onClose, highlightVersion }: ReleaseNotesModalProps) { |
| 35 | + const [currentVersion, setCurrentVersion] = useState<string | null>(null); |
| 36 | + const { data: releasesData, isLoading, error } = api.version.getAllReleases.useQuery(undefined, { |
| 37 | + enabled: isOpen |
| 38 | + }); |
| 39 | + const { data: versionData } = api.version.getCurrentVersion.useQuery(undefined, { |
| 40 | + enabled: isOpen |
| 41 | + }); |
| 42 | + |
| 43 | + // Get current version when modal opens |
| 44 | + useEffect(() => { |
| 45 | + if (isOpen && versionData?.success && versionData.version) { |
| 46 | + setCurrentVersion(versionData.version); |
| 47 | + } |
| 48 | + }, [isOpen, versionData]); |
| 49 | + |
| 50 | + // Mark version as seen when modal closes |
| 51 | + const handleClose = () => { |
| 52 | + if (currentVersion) { |
| 53 | + markVersionAsSeen(currentVersion); |
| 54 | + } |
| 55 | + onClose(); |
| 56 | + }; |
| 57 | + |
| 58 | + if (!isOpen) return null; |
| 59 | + |
| 60 | + const releases: Release[] = releasesData?.success ? releasesData.releases : []; |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className="fixed inset-0 backdrop-blur-sm bg-black/50 flex items-center justify-center z-50 p-4"> |
| 64 | + <div className="bg-card rounded-lg shadow-xl max-w-4xl w-full max-h-[90vh] flex flex-col border border-border"> |
| 65 | + {/* Header */} |
| 66 | + <div className="flex items-center justify-between p-6 border-b border-border"> |
| 67 | + <div className="flex items-center gap-3"> |
| 68 | + <Tag className="h-6 w-6 text-blue-600" /> |
| 69 | + <h2 className="text-2xl font-bold text-card-foreground">Release Notes</h2> |
| 70 | + </div> |
| 71 | + <Button |
| 72 | + variant="ghost" |
| 73 | + size="sm" |
| 74 | + onClick={handleClose} |
| 75 | + className="h-8 w-8 p-0" |
| 76 | + > |
| 77 | + <X className="h-4 w-4" /> |
| 78 | + </Button> |
| 79 | + </div> |
| 80 | + |
| 81 | + {/* Content */} |
| 82 | + <div className="flex-1 overflow-hidden flex flex-col"> |
| 83 | + {isLoading ? ( |
| 84 | + <div className="flex items-center justify-center p-8"> |
| 85 | + <div className="flex items-center gap-3"> |
| 86 | + <Loader2 className="h-6 w-6 animate-spin text-primary" /> |
| 87 | + <span className="text-muted-foreground">Loading release notes...</span> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ) : error || !releasesData?.success ? ( |
| 91 | + <div className="flex items-center justify-center p-8"> |
| 92 | + <div className="text-center"> |
| 93 | + <p className="text-destructive mb-2">Failed to load release notes</p> |
| 94 | + <p className="text-sm text-muted-foreground"> |
| 95 | + {releasesData?.error || 'Please try again later'} |
| 96 | + </p> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + ) : releases.length === 0 ? ( |
| 100 | + <div className="flex items-center justify-center p-8"> |
| 101 | + <p className="text-muted-foreground">No releases found</p> |
| 102 | + </div> |
| 103 | + ) : ( |
| 104 | + <div className="flex-1 overflow-y-auto p-6 space-y-6"> |
| 105 | + {releases.map((release, index) => { |
| 106 | + const isHighlighted = highlightVersion && release.tagName.replace('v', '') === highlightVersion; |
| 107 | + const isLatest = index === 0; |
| 108 | + |
| 109 | + return ( |
| 110 | + <div |
| 111 | + key={release.tagName} |
| 112 | + className={`border rounded-lg p-6 ${ |
| 113 | + isHighlighted |
| 114 | + ? 'border-blue-500 bg-blue-50/10 dark:bg-blue-950/10' |
| 115 | + : 'border-border bg-card' |
| 116 | + } ${isLatest ? 'ring-2 ring-primary/20' : ''}`} |
| 117 | + > |
| 118 | + {/* Release Header */} |
| 119 | + <div className="flex items-start justify-between mb-4"> |
| 120 | + <div className="flex-1"> |
| 121 | + <div className="flex items-center gap-3 mb-2"> |
| 122 | + <h3 className="text-xl font-semibold text-card-foreground"> |
| 123 | + {release.name || release.tagName} |
| 124 | + </h3> |
| 125 | + {isLatest && ( |
| 126 | + <Badge variant="default" className="text-xs"> |
| 127 | + Latest |
| 128 | + </Badge> |
| 129 | + )} |
| 130 | + {isHighlighted && ( |
| 131 | + <Badge variant="secondary" className="text-xs bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200"> |
| 132 | + New |
| 133 | + </Badge> |
| 134 | + )} |
| 135 | + </div> |
| 136 | + <div className="flex items-center gap-4 text-sm text-muted-foreground"> |
| 137 | + <div className="flex items-center gap-1"> |
| 138 | + <Tag className="h-4 w-4" /> |
| 139 | + <span>{release.tagName}</span> |
| 140 | + </div> |
| 141 | + <div className="flex items-center gap-1"> |
| 142 | + <Calendar className="h-4 w-4" /> |
| 143 | + <span> |
| 144 | + {new Date(release.publishedAt).toLocaleDateString('en-US', { |
| 145 | + year: 'numeric', |
| 146 | + month: 'long', |
| 147 | + day: 'numeric' |
| 148 | + })} |
| 149 | + </span> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + <Button |
| 154 | + variant="ghost" |
| 155 | + size="sm" |
| 156 | + asChild |
| 157 | + className="h-8 w-8 p-0" |
| 158 | + > |
| 159 | + <a |
| 160 | + href={release.htmlUrl} |
| 161 | + target="_blank" |
| 162 | + rel="noopener noreferrer" |
| 163 | + title="View on GitHub" |
| 164 | + > |
| 165 | + <ExternalLink className="h-4 w-4" /> |
| 166 | + </a> |
| 167 | + </Button> |
| 168 | + </div> |
| 169 | + |
| 170 | + {/* Release Body */} |
| 171 | + {release.body && ( |
| 172 | + <div className="prose prose-sm max-w-none dark:prose-invert"> |
| 173 | + <div className="whitespace-pre-wrap text-sm text-card-foreground leading-relaxed"> |
| 174 | + {release.body} |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + )} |
| 178 | + </div> |
| 179 | + ); |
| 180 | + })} |
| 181 | + </div> |
| 182 | + )} |
| 183 | + </div> |
| 184 | + |
| 185 | + {/* Footer */} |
| 186 | + <div className="flex items-center justify-between p-6 border-t border-border bg-muted/30"> |
| 187 | + <div className="text-sm text-muted-foreground"> |
| 188 | + {currentVersion && ( |
| 189 | + <span>Current version: <span className="font-medium text-card-foreground">v{currentVersion}</span></span> |
| 190 | + )} |
| 191 | + </div> |
| 192 | + <Button onClick={handleClose} variant="default"> |
| 193 | + Close |
| 194 | + </Button> |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + ); |
| 199 | +} |
| 200 | + |
| 201 | +// Export helper functions for use in other components |
| 202 | +export { getLastSeenVersion, markVersionAsSeen }; |
0 commit comments