|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { |
| 3 | + link, |
| 4 | + card, |
| 5 | + content, |
| 6 | + iconContainer, |
| 7 | + icon, |
| 8 | + title, |
| 9 | + updatedText, |
| 10 | +} from './styles.css'; |
| 11 | + |
| 12 | +export interface LinkPreviewProps { |
| 13 | + url: string; |
| 14 | +} |
| 15 | + |
| 16 | +interface RepoData { |
| 17 | + name: string; |
| 18 | + full_name: string; |
| 19 | + owner: { |
| 20 | + avatar_url: string; |
| 21 | + }; |
| 22 | + updated_at: string; |
| 23 | +} |
| 24 | + |
| 25 | +// GitHub 레포지토리 데이터를 가져오는 함수 |
| 26 | +const fetchGitHubRepoData = async ( |
| 27 | + repoPath: string |
| 28 | +): Promise<RepoData | null> => { |
| 29 | + try { |
| 30 | + const apiUrl = `https://api.github.com/repos/${repoPath}`; |
| 31 | + const response = await fetch(apiUrl); |
| 32 | + |
| 33 | + if (!response.ok) { |
| 34 | + throw new Error('Failed to fetch GitHub repo data'); |
| 35 | + } |
| 36 | + |
| 37 | + const data = await response.json(); |
| 38 | + return data; |
| 39 | + } catch (error) { |
| 40 | + console.error('Error fetching GitHub repo data:', error); |
| 41 | + return null; |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +// GitHub URL에서 레포지토리 경로 추출 |
| 46 | +const extractRepoPathFromUrl = (url: string): string | null => { |
| 47 | + try { |
| 48 | + const parsedUrl = new URL(url); |
| 49 | + if (parsedUrl.hostname === 'github.com') { |
| 50 | + // URL 경로에서 첫 번째 '/'를 제거하고 나머지 경로 반환 |
| 51 | + const path = parsedUrl.pathname.substring(1); |
| 52 | + // 레포지토리 경로는 일반적으로 'username/repo-name' 형식 |
| 53 | + const pathParts = path.split('/'); |
| 54 | + if (pathParts.length >= 2) { |
| 55 | + return `${pathParts[0]}/${pathParts[1]}`; |
| 56 | + } |
| 57 | + } |
| 58 | + return null; |
| 59 | + } catch (error) { |
| 60 | + console.error('Error parsing URL:', error); |
| 61 | + return null; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +// 날짜 포맷팅 함수 |
| 66 | +const formatUpdatedTime = (dateString: string): string => { |
| 67 | + const date = new Date(dateString); |
| 68 | + const now = new Date(); |
| 69 | + const diffInHours = Math.floor( |
| 70 | + (now.getTime() - date.getTime()) / (1000 * 60 * 60) |
| 71 | + ); |
| 72 | + |
| 73 | + if (diffInHours < 24) { |
| 74 | + return `Updated ${diffInHours} hours ago`; |
| 75 | + } else { |
| 76 | + const diffInDays = Math.floor(diffInHours / 24); |
| 77 | + if (diffInDays === 1) { |
| 78 | + return 'Updated yesterday'; |
| 79 | + } else if (diffInDays < 30) { |
| 80 | + return `Updated ${diffInDays} days ago`; |
| 81 | + } else { |
| 82 | + const months = [ |
| 83 | + 'Jan', |
| 84 | + 'Feb', |
| 85 | + 'Mar', |
| 86 | + 'Apr', |
| 87 | + 'May', |
| 88 | + 'Jun', |
| 89 | + 'Jul', |
| 90 | + 'Aug', |
| 91 | + 'Sep', |
| 92 | + 'Oct', |
| 93 | + 'Nov', |
| 94 | + 'Dec', |
| 95 | + ]; |
| 96 | + return `Updated on ${months[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`; |
| 97 | + } |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +const LinkPreview: React.FC<LinkPreviewProps> = ({ url }) => { |
| 102 | + const [repoData, setRepoData] = useState<RepoData | null>(null); |
| 103 | + const [loading, setLoading] = useState(true); |
| 104 | + |
| 105 | + useEffect(() => { |
| 106 | + const loadRepoData = async () => { |
| 107 | + setLoading(true); |
| 108 | + const repoPath = extractRepoPathFromUrl(url); |
| 109 | + |
| 110 | + if (repoPath) { |
| 111 | + const data = await fetchGitHubRepoData(repoPath); |
| 112 | + setRepoData(data); |
| 113 | + } |
| 114 | + |
| 115 | + setLoading(false); |
| 116 | + }; |
| 117 | + |
| 118 | + loadRepoData(); |
| 119 | + }, [url]); |
| 120 | + |
| 121 | + // 레포지토리 이름 추출 (full_name에서 organization/repo 형식) |
| 122 | + const repoName = |
| 123 | + repoData?.name || |
| 124 | + extractRepoPathFromUrl(url)?.split('/')[1] || |
| 125 | + 'Repository'; |
| 126 | + |
| 127 | + // 업데이트 시간 포맷팅 |
| 128 | + const updatedTimeText = repoData?.updated_at |
| 129 | + ? formatUpdatedTime(repoData.updated_at) |
| 130 | + : ''; |
| 131 | + |
| 132 | + return ( |
| 133 | + <a href={url} target="_blank" rel="noopener noreferrer" className={link}> |
| 134 | + <div className={card}> |
| 135 | + <div className={iconContainer}> |
| 136 | + <img |
| 137 | + src={ |
| 138 | + repoData?.owner?.avatar_url || |
| 139 | + 'https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png' |
| 140 | + } |
| 141 | + alt="Repository icon" |
| 142 | + className={icon} |
| 143 | + /> |
| 144 | + </div> |
| 145 | + <div className={content}> |
| 146 | + <div className={title}>{repoName}</div> |
| 147 | + <div className={updatedText}> |
| 148 | + {loading ? 'Loading...' : `NotionX • ${updatedTimeText}`} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + </a> |
| 153 | + ); |
| 154 | +}; |
| 155 | + |
| 156 | +export default LinkPreview; |
0 commit comments