diff --git a/src/components/RepositoryCard.tsx b/src/components/RepositoryCard.tsx index 81ea8c7b..e5c1ab18 100644 --- a/src/components/RepositoryCard.tsx +++ b/src/components/RepositoryCard.tsx @@ -2,13 +2,20 @@ import React, { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; -import { GitBranch, Loader2, EllipsisVertical, Edit } from 'lucide-react'; +import { GitBranch, Loader2, EllipsisVertical, Edit, Code, ExternalLink } from 'lucide-react'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, + DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, +} from '@/components/ui/dialog'; import type { Repository } from '@/hooks/useUserRepositories'; import { useAuthor } from '@/hooks/useAuthor'; import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar'; @@ -23,6 +30,7 @@ import { useGitSettings } from '@/hooks/useGitSettings'; import { detectFork } from '@/lib/detectFork'; import { NostrRepoEditDialog } from '@/components/NostrRepoEditDialog'; import { useQueryClient } from '@tanstack/react-query'; +import { useOGImage } from '@/hooks/useOGImage'; interface RepositoryCardProps { repo: Repository; @@ -39,11 +47,19 @@ export function RepositoryCard({ repo }: RepositoryCardProps) { const queryClient = useQueryClient(); const [isCloning, setIsCloning] = useState(false); const [editRepoOpen, setEditRepoOpen] = useState(false); + const [imageError, setImageError] = useState(false); + const [eventViewerOpen, setEventViewerOpen] = useState(false); const displayName = authorData?.metadata?.name || genUserName(repo.pubkey); const profileImage = authorData?.metadata?.picture; const isOwnRepo = user?.pubkey === repo.pubkey; + // Get OG metadata from first web URL + const firstWebUrl = repo.webUrls?.[0]; + const { data: ogMetadata } = useOGImage(firstWebUrl); + const ogImageUrl = ogMetadata?.image ?? null; + const ogDescription = ogMetadata?.description ?? null; + // Construct Nostr clone URL const nostrURI = new NostrURI({ pubkey: repo.pubkey, @@ -109,8 +125,31 @@ export function RepositoryCard({ repo }: RepositoryCardProps) { queryClient.invalidateQueries({ queryKey: ['nostr', 'user-repositories', user?.pubkey] }); }; + // Check if we have a valid OG image to show + const hasValidImage = ogImageUrl && !imageError; + return ( + {/* Preview Image or Placeholder */} + {hasValidImage && firstWebUrl ? ( + + {`${repo.name} setImageError(true)} + /> + + ) : ( +
+ +
+ )} {/* Header with name and menu */}
@@ -119,42 +158,57 @@ export function RepositoryCard({ repo }: RepositoryCardProps) { {repo.name}
- {isOwnRepo && ( - - - - - - setEditRepoOpen(true)} className="gap-2"> - - Edit Repository + + + + + + {firstWebUrl && ( + + + + Open Website + - - - )} + )} + setEventViewerOpen(true)} className="gap-2"> + + View Event + + {isOwnRepo && ( + <> + + setEditRepoOpen(true)} className="gap-2"> + + Edit Repository + + + )} + + {/* Author */}
-
- +
+ {profileImage && ( )} - + {displayName.slice(0, 2).toUpperCase()} - {displayName} + {displayName}
{/* Description */} - {repo.description && ( + {(repo.description || ogDescription) && (

- {repo.description} + {repo.description || ogDescription}

)} @@ -212,6 +266,28 @@ export function RepositoryCard({ repo }: RepositoryCardProps) { repoIdentifier={repo.repoId} onSuccess={handleRepoEditSuccess} /> + + {/* Event Viewer Dialog */} + + + + Repository Event + +
+
+              {JSON.stringify({
+                id: repo.id,
+                pubkey: repo.pubkey,
+                created_at: repo.created_at,
+                kind: repo.kind,
+                tags: repo.tags,
+                content: repo.content,
+                sig: repo.sig,
+              }, null, 2)}
+            
+
+
+
); } diff --git a/src/hooks/useFollowedRepositories.ts b/src/hooks/useFollowedRepositories.ts index 5964d08b..452fdbb5 100644 --- a/src/hooks/useFollowedRepositories.ts +++ b/src/hooks/useFollowedRepositories.ts @@ -50,10 +50,19 @@ export function useFollowedRepositories(followedPubkeys: string[] = []) { const name = event.tags.find(tag => tag[0] === 'name')?.[1] || dTag; const description = event.tags.find(tag => tag[0] === 'description')?.[1] || ''; - const webUrls = event.tags.filter(tag => tag[0] === 'web').map(tag => tag[1]).filter(Boolean); - const cloneUrls = event.tags.filter(tag => tag[0] === 'clone').map(tag => tag[1]).filter(Boolean); - const relays = event.tags.filter(tag => tag[0] === 'relays').map(tag => tag[1]).filter(Boolean); - const maintainers = event.tags.filter(tag => tag[0] === 'maintainers').map(tag => tag[1]).filter(Boolean); + + // Extract multi-value tags correctly (all values are in a single tag per NIP-34) + const webTag = event.tags.find(tag => tag[0] === 'web'); + const webUrls = webTag ? webTag.slice(1).filter(Boolean) : []; + + const cloneTag = event.tags.find(tag => tag[0] === 'clone'); + const cloneUrls = cloneTag ? cloneTag.slice(1).filter(Boolean) : []; + + const relaysTag = event.tags.find(tag => tag[0] === 'relays'); + const relays = relaysTag ? relaysTag.slice(1).filter(Boolean) : []; + + const maintainersTag = event.tags.find(tag => tag[0] === 'maintainers'); + const maintainers = maintainersTag ? maintainersTag.slice(1).filter(Boolean) : []; const repoTags = event.tags.filter(tag => tag[0] === 't' && tag[1] !== 'personal-fork').map(tag => tag[1]); const earliestUniqueCommit = event.tags.find(tag => tag[0] === 'r' && tag[2] === 'euc')?.[1]; const isPersonalFork = event.tags.some(tag => tag[0] === 't' && tag[1] === 'personal-fork'); diff --git a/src/hooks/useOGImage.ts b/src/hooks/useOGImage.ts new file mode 100644 index 00000000..173379d6 --- /dev/null +++ b/src/hooks/useOGImage.ts @@ -0,0 +1,91 @@ +import { useQuery } from '@tanstack/react-query'; +import { useAppContext } from './useAppContext'; +import { proxyUrl } from '@/lib/proxyUrl'; + +export interface OGMetadata { + image: string | null; + description: string | null; +} + +/** + * Hook to fetch Open Graph metadata from a web URL using CORS proxy + * @param webUrl - The web URL to fetch OG metadata from + * @returns Query result with OG image URL and description + */ +export function useOGImage(webUrl?: string) { + const { config } = useAppContext(); + + return useQuery({ + queryKey: ['og-metadata', webUrl], + queryFn: async (): Promise => { + if (!webUrl) { + return { image: null, description: null }; + } + + try { + // Use CORS proxy to fetch the HTML + const proxiedUrl = proxyUrl(config.corsProxy, webUrl); + const response = await fetch(proxiedUrl, { + signal: AbortSignal.timeout(5000), + }); + + if (!response.ok) { + return { image: null, description: null }; + } + + const html = await response.text(); + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + + // Extract OG image + let ogImage: string | null = null; + const ogImageMeta = doc.querySelector('meta[property="og:image"]')?.getAttribute('content'); + if (ogImageMeta) { + try { + ogImage = new URL(ogImageMeta, webUrl).href; + } catch { + ogImage = ogImageMeta.startsWith('http') ? ogImageMeta : null; + } + } + + // Fallback to twitter:image if og:image not found + if (!ogImage) { + const twitterImage = doc.querySelector('meta[name="twitter:image"]')?.getAttribute('content'); + if (twitterImage) { + try { + ogImage = new URL(twitterImage, webUrl).href; + } catch { + ogImage = twitterImage.startsWith('http') ? twitterImage : null; + } + } + } + + // Extract OG description + let ogDescription: string | null = null; + const ogDescriptionMeta = doc.querySelector('meta[property="og:description"]')?.getAttribute('content'); + if (ogDescriptionMeta) { + ogDescription = ogDescriptionMeta.trim(); + } + + // Fallback to twitter:description if og:description not found + if (!ogDescription) { + const twitterDescription = doc.querySelector('meta[name="twitter:description"]')?.getAttribute('content'); + if (twitterDescription) { + ogDescription = twitterDescription.trim(); + } + } + + return { + image: ogImage, + description: ogDescription || null, + }; + } catch (error) { + console.warn('Failed to fetch OG metadata:', error); + return { image: null, description: null }; + } + }, + enabled: !!webUrl, + staleTime: 1000 * 60 * 60, // 1 hour + retry: 1, + }); +} diff --git a/src/hooks/useUserRepositories.ts b/src/hooks/useUserRepositories.ts index 195039b6..1479fe97 100644 --- a/src/hooks/useUserRepositories.ts +++ b/src/hooks/useUserRepositories.ts @@ -55,10 +55,19 @@ export function useUserRepositories(userPubkey?: string) { try { const name = event.tags.find(tag => tag[0] === 'name')?.[1] || dTag; const description = event.tags.find(tag => tag[0] === 'description')?.[1] || ''; - const webUrls = event.tags.filter(tag => tag[0] === 'web').map(tag => tag[1]).filter(Boolean); - const cloneUrls = event.tags.filter(tag => tag[0] === 'clone').map(tag => tag[1]).filter(Boolean); - const relays = event.tags.filter(tag => tag[0] === 'relays').map(tag => tag[1]).filter(Boolean); - const maintainers = event.tags.filter(tag => tag[0] === 'maintainers').map(tag => tag[1]).filter(Boolean); + + // Extract multi-value tags correctly (all values are in a single tag per NIP-34) + const webTag = event.tags.find(tag => tag[0] === 'web'); + const webUrls = webTag ? webTag.slice(1).filter(Boolean) : []; + + const cloneTag = event.tags.find(tag => tag[0] === 'clone'); + const cloneUrls = cloneTag ? cloneTag.slice(1).filter(Boolean) : []; + + const relaysTag = event.tags.find(tag => tag[0] === 'relays'); + const relays = relaysTag ? relaysTag.slice(1).filter(Boolean) : []; + + const maintainersTag = event.tags.find(tag => tag[0] === 'maintainers'); + const maintainers = maintainersTag ? maintainersTag.slice(1).filter(Boolean) : []; const repoTags = event.tags.filter(tag => tag[0] === 't' && tag[1] !== 'personal-fork').map(tag => tag[1]); const earliestUniqueCommit = event.tags.find(tag => tag[0] === 'r' && tag[2] === 'euc')?.[1]; const isPersonalFork = event.tags.some(tag => tag[0] === 't' && tag[1] === 'personal-fork');