mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add unified useRepos hook
This commit is contained in:
1 parent
a1eb111244
commit
5465989c6d
5 files changed
+451
-360
No files matched your search
@@ -1,246 +0,0 @@
|
||||
import { useState } from 'react';
|
||||
import { useNostrRepos, type NostrRepo } from '@/hooks/useNostrRepos';
|
||||
import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { GitBranch, Download, ExternalLink, Users, Globe } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import git from 'isomorphic-git';
|
||||
import http from 'isomorphic-git/http/web';
|
||||
|
||||
interface NostrReposProps {
|
||||
className?: string;
|
||||
excludeProjectIds?: string[]; // Project IDs that are already "nostr enabled"
|
||||
onProjectCloned?: () => void; // Callback when a project is successfully cloned
|
||||
}
|
||||
|
||||
export function NostrRepos({ className, excludeProjectIds = [], onProjectCloned }: NostrReposProps) {
|
||||
const { data: repos, isLoading, error } = useNostrRepos();
|
||||
const projectsManager = useProjectsManager();
|
||||
const { toast } = useToast();
|
||||
const [cloningRepos, setCloningRepos] = useState<Set<string>>(new Set());
|
||||
|
||||
// Filter out repos that are already represented as projects
|
||||
const availableRepos = repos?.filter(repo => !excludeProjectIds.includes(repo.id)) || [];
|
||||
|
||||
const handleCloneRepo = async (repo: NostrRepo) => {
|
||||
if (!repo.cloneUrls.length) {
|
||||
toast({
|
||||
title: "No clone URL",
|
||||
description: "This repository doesn't have any clone URLs configured.",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setCloningRepos(prev => new Set(prev).add(repo.id));
|
||||
|
||||
try {
|
||||
await projectsManager.init();
|
||||
|
||||
// Create project directory
|
||||
const projectPath = `/projects/${repo.id}`;
|
||||
|
||||
// Clone the repository
|
||||
const cloneUrl = repo.cloneUrls[0]; // Use the first clone URL
|
||||
await git.clone({
|
||||
fs: projectsManager.fs,
|
||||
http,
|
||||
dir: projectPath,
|
||||
url: cloneUrl,
|
||||
singleBranch: true,
|
||||
depth: 1,
|
||||
});
|
||||
|
||||
// Set git config to mark this as a nostr-enabled project
|
||||
const naddr = nip19.naddrEncode({
|
||||
identifier: repo.id,
|
||||
pubkey: repo.event.pubkey,
|
||||
kind: 30617,
|
||||
relays: repo.relays.length > 0 ? repo.relays : undefined,
|
||||
});
|
||||
|
||||
await git.setConfig({
|
||||
fs: projectsManager.fs,
|
||||
dir: projectPath,
|
||||
path: 'nostr.repo',
|
||||
value: naddr,
|
||||
});
|
||||
|
||||
// No need to create project metadata - using filesystem stats and basename
|
||||
|
||||
toast({
|
||||
title: "Repository cloned",
|
||||
description: `${repo.name} has been cloned to your projects.`,
|
||||
});
|
||||
|
||||
// Call the callback to refresh the projects list, or fall back to page reload
|
||||
if (onProjectCloned) {
|
||||
onProjectCloned();
|
||||
} else {
|
||||
window.location.reload();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to clone repository:', error);
|
||||
toast({
|
||||
title: "Clone failed",
|
||||
description: `Failed to clone ${repo.name}. Please try again.`,
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
setCloningRepos(prev => {
|
||||
const newSet = new Set(prev);
|
||||
newSet.delete(repo.id);
|
||||
return newSet;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className={cn("space-y-4", className)}>
|
||||
<h2 className="text-xl md:text-2xl font-semibold">Your Nostr Repositories</h2>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{[...Array(3)].map((_, i) => (
|
||||
<Card key={i}>
|
||||
<CardHeader className="pb-2">
|
||||
<Skeleton className="h-5 w-3/4" />
|
||||
<Skeleton className="h-4 w-1/2" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Skeleton className="h-4 w-full mb-2" />
|
||||
<Skeleton className="h-4 w-2/3" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className={cn("space-y-4", className)}>
|
||||
<h2 className="text-xl md:text-2xl font-semibold">Your Nostr Repositories</h2>
|
||||
<Card className="border-destructive/20">
|
||||
<CardContent className="py-6 text-center">
|
||||
<p className="text-destructive">Failed to load repositories</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!availableRepos.length) {
|
||||
return null; // Don't show the section if there are no repos to display
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("space-y-4", className)}>
|
||||
<h2 className="text-xl md:text-2xl font-semibold">Your Nostr Repositories</h2>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
Clone your NIP-34 repositories to start working on them locally.
|
||||
</p>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{availableRepos.map((repo) => {
|
||||
const isCloning = cloningRepos.has(repo.id);
|
||||
|
||||
return (
|
||||
<Card
|
||||
key={repo.id}
|
||||
className="bg-gradient-to-br from-card to-card/80 border-primary/10 hover:border-primary/20 transition-all duration-300"
|
||||
>
|
||||
<CardHeader className="pb-2">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="flex-1 min-w-0">
|
||||
<CardTitle className="text-base md:text-lg flex items-center gap-2 truncate">
|
||||
<GitBranch className="h-4 w-4 text-primary flex-shrink-0" />
|
||||
<span className="truncate">{repo.name}</span>
|
||||
</CardTitle>
|
||||
{repo.description && (
|
||||
<CardDescription className="text-sm mt-1 line-clamp-2">
|
||||
{repo.description}
|
||||
</CardDescription>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="space-y-3">
|
||||
{/* Tags */}
|
||||
{repo.tags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{repo.tags.slice(0, 3).map((tag) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
{repo.tags.length > 3 && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
+{repo.tags.length - 3}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Repository info */}
|
||||
<div className="flex items-center gap-4 text-xs text-muted-foreground">
|
||||
{repo.maintainers.length > 0 && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Users className="h-3 w-3" />
|
||||
<span>{repo.maintainers.length} maintainer{repo.maintainers.length !== 1 ? 's' : ''}</span>
|
||||
</div>
|
||||
)}
|
||||
{repo.webUrls.length > 0 && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Globe className="h-3 w-3" />
|
||||
<span>Web</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={() => handleCloneRepo(repo)}
|
||||
disabled={isCloning || !repo.cloneUrls.length}
|
||||
className="flex-1"
|
||||
size="sm"
|
||||
>
|
||||
{isCloning ? (
|
||||
<>
|
||||
<div className="animate-spin rounded-full h-3 w-3 border-b-2 border-white mr-2"></div>
|
||||
Cloning...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Download className="h-3 w-3 mr-2" />
|
||||
Clone
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
|
||||
{repo.webUrls.length > 0 && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(repo.webUrls[0], '_blank')}
|
||||
className="px-3"
|
||||
>
|
||||
<ExternalLink className="h-3 w-3" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import {
|
||||
Folder,
|
||||
GitBranch,
|
||||
Download,
|
||||
ExternalLink,
|
||||
Users,
|
||||
Globe,
|
||||
Cloud,
|
||||
HardDrive,
|
||||
RefreshCw,
|
||||
Upload
|
||||
} from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import git from 'isomorphic-git';
|
||||
import http from 'isomorphic-git/http/web';
|
||||
import type { UnifiedRepo } from '@/hooks/useRepos';
|
||||
|
||||
interface RepoCardProps {
|
||||
repo: UnifiedRepo;
|
||||
onRepoCloned?: () => void;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function RepoCard({ repo, onRepoCloned, className }: RepoCardProps) {
|
||||
const navigate = useNavigate();
|
||||
const projectsManager = useProjectsManager();
|
||||
const { toast } = useToast();
|
||||
const [isCloning, setIsCloning] = useState(false);
|
||||
|
||||
const getStatusIcon = () => {
|
||||
switch (repo.status) {
|
||||
case 'local-only':
|
||||
return <HardDrive className="h-4 w-4 text-blue-500" />;
|
||||
case 'published':
|
||||
return <Upload className="h-4 w-4 text-green-500" />;
|
||||
case 'remote-only':
|
||||
return <Cloud className="h-4 w-4 text-orange-500" />;
|
||||
case 'synced':
|
||||
return <RefreshCw className="h-4 w-4 text-emerald-500" />;
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusText = () => {
|
||||
switch (repo.status) {
|
||||
case 'local-only':
|
||||
return 'Local only';
|
||||
case 'published':
|
||||
return 'Published';
|
||||
case 'remote-only':
|
||||
return 'Remote only';
|
||||
case 'synced':
|
||||
return 'Synced';
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusColor = () => {
|
||||
switch (repo.status) {
|
||||
case 'local-only':
|
||||
return 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200';
|
||||
case 'published':
|
||||
return 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200';
|
||||
case 'remote-only':
|
||||
return 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200';
|
||||
case 'synced':
|
||||
return 'bg-emerald-100 text-emerald-800 dark:bg-emerald-900 dark:text-emerald-200';
|
||||
}
|
||||
};
|
||||
|
||||
const handleCardClick = () => {
|
||||
if (repo.localProject) {
|
||||
navigate(`/project/${repo.localProject.id}`);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCloneRepo = async () => {
|
||||
if (!repo.nostrRepo || !repo.cloneUrls.length) {
|
||||
toast({
|
||||
title: "No clone URL",
|
||||
description: "This repository doesn't have any clone URLs configured.",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setIsCloning(true);
|
||||
|
||||
try {
|
||||
await projectsManager.init();
|
||||
|
||||
// Create project directory
|
||||
const projectPath = `/projects/${repo.id}`;
|
||||
|
||||
// Clone the repository
|
||||
const cloneUrl = repo.cloneUrls[0]; // Use the first clone URL
|
||||
await git.clone({
|
||||
fs: projectsManager.fs,
|
||||
http,
|
||||
dir: projectPath,
|
||||
url: cloneUrl,
|
||||
singleBranch: true,
|
||||
depth: 1,
|
||||
});
|
||||
|
||||
// Set git config to mark this as a nostr-enabled project
|
||||
const naddr = nip19.naddrEncode({
|
||||
identifier: repo.id,
|
||||
pubkey: repo.nostrRepo.event.pubkey,
|
||||
kind: 30617,
|
||||
relays: repo.relays.length > 0 ? repo.relays : undefined,
|
||||
});
|
||||
|
||||
await git.setConfig({
|
||||
fs: projectsManager.fs,
|
||||
dir: projectPath,
|
||||
path: 'nostr.repo',
|
||||
value: naddr,
|
||||
});
|
||||
|
||||
toast({
|
||||
title: "Repository cloned",
|
||||
description: `${repo.name} has been cloned to your projects.`,
|
||||
});
|
||||
|
||||
// Call the callback to refresh the repos list
|
||||
if (onRepoCloned) {
|
||||
onRepoCloned();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to clone repository:', error);
|
||||
toast({
|
||||
title: "Clone failed",
|
||||
description: `Failed to clone ${repo.name}. Please try again.`,
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
setIsCloning(false);
|
||||
}
|
||||
};
|
||||
|
||||
const canOpen = repo.status === 'local-only' || repo.status === 'published' || repo.status === 'synced';
|
||||
const canClone = repo.status === 'remote-only' && repo.cloneUrls.length > 0;
|
||||
|
||||
return (
|
||||
<Card
|
||||
className={cn(
|
||||
"transition-all duration-300 bg-gradient-to-br from-card to-card/80 border-primary/10 hover:border-primary/20",
|
||||
canOpen && "cursor-pointer hover:shadow-xl hover:shadow-primary/10 hover:scale-[1.02]",
|
||||
className
|
||||
)}
|
||||
onClick={canOpen ? handleCardClick : undefined}
|
||||
tabIndex={canOpen ? 0 : undefined}
|
||||
onKeyDown={canOpen ? (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault();
|
||||
handleCardClick();
|
||||
}
|
||||
} : undefined}
|
||||
>
|
||||
<CardHeader className="pb-2">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="flex-1 min-w-0">
|
||||
<CardTitle className="text-base md:text-lg flex items-center gap-2 truncate">
|
||||
{repo.status === 'remote-only' ? (
|
||||
<GitBranch className="h-4 w-4 text-primary flex-shrink-0" />
|
||||
) : (
|
||||
<Folder className="h-4 w-4 text-primary flex-shrink-0" />
|
||||
)}
|
||||
<span className="truncate">{repo.name}</span>
|
||||
</CardTitle>
|
||||
{repo.description && (
|
||||
<CardDescription className="text-sm mt-1 line-clamp-2">
|
||||
{repo.description}
|
||||
</CardDescription>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 ml-2">
|
||||
<Badge variant="outline" className={cn("text-xs", getStatusColor())}>
|
||||
<span className="flex items-center gap-1">
|
||||
{getStatusIcon()}
|
||||
{getStatusText()}
|
||||
</span>
|
||||
</Badge>
|
||||
</div>
|
||||
</div>
|
||||
<CardDescription className="text-sm">
|
||||
Modified {repo.lastModified.toLocaleDateString()}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="space-y-3">
|
||||
{/* Tags */}
|
||||
{repo.tags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{repo.tags.slice(0, 3).map((tag) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
{repo.tags.length > 3 && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
+{repo.tags.length - 3}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Repository info */}
|
||||
{(repo.maintainers.length > 0 || repo.webUrls.length > 0) && (
|
||||
<div className="flex items-center gap-4 text-xs text-muted-foreground">
|
||||
{repo.maintainers.length > 0 && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Users className="h-3 w-3" />
|
||||
<span>{repo.maintainers.length} maintainer{repo.maintainers.length !== 1 ? 's' : ''}</span>
|
||||
</div>
|
||||
)}
|
||||
{repo.webUrls.length > 0 && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Globe className="h-3 w-3" />
|
||||
<span>Web</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Actions */}
|
||||
{(canClone || repo.webUrls.length > 0) && (
|
||||
<div className="flex gap-2" onClick={(e) => e.stopPropagation()}>
|
||||
{canClone && (
|
||||
<Button
|
||||
onClick={handleCloneRepo}
|
||||
disabled={isCloning}
|
||||
className="flex-1"
|
||||
size="sm"
|
||||
>
|
||||
{isCloning ? (
|
||||
<>
|
||||
<div className="animate-spin rounded-full h-3 w-3 border-b-2 border-white mr-2"></div>
|
||||
Cloning...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Download className="h-3 w-3 mr-2" />
|
||||
Clone
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
|
||||
{repo.webUrls.length > 0 && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(repo.webUrls[0], '_blank')}
|
||||
className="px-3"
|
||||
>
|
||||
<ExternalLink className="h-3 w-3" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useProjectsManager } from './useProjectsManager';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
|
||||
export function useNostrEnabledProjects() {
|
||||
const projectsManager = useProjectsManager();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['nostr-enabled-projects'],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
await projectsManager.init();
|
||||
const projects = await projectsManager.getProjects();
|
||||
|
||||
const nostrEnabledProjects: string[] = [];
|
||||
|
||||
for (const project of projects) {
|
||||
try {
|
||||
const naddr = await projectsManager.getNostrRepoAddress(project.id);
|
||||
if (naddr) {
|
||||
// Extract the d tag (identifier) from the naddr
|
||||
const decoded = nip19.decode(naddr);
|
||||
if (decoded.type === 'naddr') {
|
||||
nostrEnabledProjects.push(decoded.data.identifier);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Skip projects that can't be checked
|
||||
}
|
||||
}
|
||||
|
||||
return nostrEnabledProjects;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
staleTime: 30 * 1000, // 30 seconds
|
||||
gcTime: 60 * 1000, // 1 minute
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useProjectsManager } from './useProjectsManager';
|
||||
import { useNostrRepos, type NostrRepo } from './useNostrRepos';
|
||||
import { useCurrentUser } from './useCurrentUser';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import type { Project } from '@/lib/ProjectsManager';
|
||||
|
||||
export type RepoStatus =
|
||||
| 'local-only' // Exists only locally, not published to Nostr
|
||||
| 'published' // Local project that's also published to Nostr
|
||||
| 'remote-only' // Exists on Nostr but not cloned locally
|
||||
| 'synced'; // Local and remote are in sync
|
||||
|
||||
export interface UnifiedRepo {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
status: RepoStatus;
|
||||
lastModified: Date;
|
||||
|
||||
// Local project data (if exists locally)
|
||||
localProject?: Project;
|
||||
|
||||
// Nostr repo data (if exists on Nostr)
|
||||
nostrRepo?: NostrRepo;
|
||||
|
||||
// Additional metadata
|
||||
tags: string[];
|
||||
webUrls: string[];
|
||||
cloneUrls: string[];
|
||||
maintainers: string[];
|
||||
relays: string[];
|
||||
}
|
||||
|
||||
export function useRepos() {
|
||||
const projectsManager = useProjectsManager();
|
||||
const { user } = useCurrentUser();
|
||||
const { data: nostrRepos = [] } = useNostrRepos();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['unified-repos', user?.pubkey],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
await projectsManager.init();
|
||||
const localProjects = await projectsManager.getProjects();
|
||||
|
||||
// Create a map to track repos by their identifier
|
||||
const repoMap = new Map<string, UnifiedRepo>();
|
||||
|
||||
// First, add all local projects
|
||||
for (const project of localProjects) {
|
||||
const naddr = await projectsManager.getNostrRepoAddress(project.id);
|
||||
let repoId = project.id;
|
||||
let status: RepoStatus = 'local-only';
|
||||
|
||||
// If this project has a Nostr address, extract the repo ID
|
||||
if (naddr) {
|
||||
try {
|
||||
const decoded = nip19.decode(naddr);
|
||||
if (decoded.type === 'naddr') {
|
||||
repoId = decoded.data.identifier;
|
||||
status = 'published';
|
||||
}
|
||||
} catch {
|
||||
// If we can't decode the naddr, treat as local-only
|
||||
}
|
||||
}
|
||||
|
||||
repoMap.set(repoId, {
|
||||
id: repoId,
|
||||
name: project.name,
|
||||
description: undefined,
|
||||
status,
|
||||
lastModified: project.lastModified,
|
||||
localProject: project,
|
||||
tags: [],
|
||||
webUrls: [],
|
||||
cloneUrls: [],
|
||||
maintainers: [],
|
||||
relays: [],
|
||||
});
|
||||
}
|
||||
|
||||
// Then, add or update with Nostr repos
|
||||
for (const nostrRepo of nostrRepos) {
|
||||
const existingRepo = repoMap.get(nostrRepo.id);
|
||||
|
||||
if (existingRepo) {
|
||||
// Update existing repo with Nostr data
|
||||
existingRepo.nostrRepo = nostrRepo;
|
||||
existingRepo.description = nostrRepo.description;
|
||||
existingRepo.tags = nostrRepo.tags;
|
||||
existingRepo.webUrls = nostrRepo.webUrls;
|
||||
existingRepo.cloneUrls = nostrRepo.cloneUrls;
|
||||
existingRepo.maintainers = nostrRepo.maintainers;
|
||||
existingRepo.relays = nostrRepo.relays;
|
||||
|
||||
// Update status based on local presence
|
||||
if (existingRepo.localProject) {
|
||||
existingRepo.status = 'synced';
|
||||
}
|
||||
} else {
|
||||
// Add new remote-only repo
|
||||
repoMap.set(nostrRepo.id, {
|
||||
id: nostrRepo.id,
|
||||
name: nostrRepo.name,
|
||||
description: nostrRepo.description,
|
||||
status: 'remote-only',
|
||||
lastModified: new Date(nostrRepo.event.created_at * 1000),
|
||||
nostrRepo,
|
||||
tags: nostrRepo.tags,
|
||||
webUrls: nostrRepo.webUrls,
|
||||
cloneUrls: nostrRepo.cloneUrls,
|
||||
maintainers: nostrRepo.maintainers,
|
||||
relays: nostrRepo.relays,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Convert map to array and sort by last modified
|
||||
const repos = Array.from(repoMap.values()).sort((a, b) =>
|
||||
b.lastModified.getTime() - a.lastModified.getTime()
|
||||
);
|
||||
|
||||
return repos;
|
||||
} catch (error) {
|
||||
console.error('Failed to load unified repos:', error);
|
||||
return [];
|
||||
}
|
||||
},
|
||||
enabled: !!user?.pubkey,
|
||||
staleTime: 30 * 1000, // 30 seconds
|
||||
gcTime: 60 * 1000, // 1 minute
|
||||
});
|
||||
}
|
||||
+44
-74
@@ -5,10 +5,10 @@ import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useOnboarding } from '@/hooks/useOnboarding';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useNostrEnabledProjects } from '@/hooks/useNostrEnabledProjects';
|
||||
import { useRepos } from '@/hooks/useRepos';
|
||||
import { ProjectSidebar } from '@/components/ProjectSidebar';
|
||||
import { OnboardingDialog } from '@/components/onboarding';
|
||||
import { NostrRepos } from '@/components/NostrRepos';
|
||||
import { RepoCard } from '@/components/RepoCard';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
@@ -22,14 +22,13 @@ import { cn } from '@/lib/utils';
|
||||
export default function Index() {
|
||||
const [prompt, setPrompt] = useState('');
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isCreating, setIsCreating] = useState(false);
|
||||
const [storedPrompt, setStoredPrompt] = useLocalStorage('shakespeare-draft-prompt', '');
|
||||
const navigate = useNavigate();
|
||||
const projectsManager = useProjectsManager();
|
||||
const isMobile = useIsMobile();
|
||||
const { user } = useCurrentUser();
|
||||
const { data: nostrEnabledProjectIds = [] } = useNostrEnabledProjects();
|
||||
const { user: _user } = useCurrentUser();
|
||||
const { data: repos = [], isLoading, error, refetch } = useRepos();
|
||||
const {
|
||||
hasCompletedOnboarding,
|
||||
currentStep,
|
||||
@@ -60,8 +59,6 @@ export default function Index() {
|
||||
setProjects(projectList);
|
||||
} catch (error) {
|
||||
console.error('Failed to load projects:', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [projectsManager]);
|
||||
|
||||
@@ -92,6 +89,8 @@ export default function Index() {
|
||||
setIsCreating(true);
|
||||
try {
|
||||
const project = await projectsManager.createProject(prompt.trim());
|
||||
// Refresh repos after creating a new project
|
||||
refetch();
|
||||
navigate(`/project/${project.id}`);
|
||||
} catch (error) {
|
||||
console.error('Failed to create project:', error);
|
||||
@@ -100,8 +99,9 @@ export default function Index() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleProjectClick = (projectId: string) => {
|
||||
navigate(`/project/${projectId}`);
|
||||
const handleRepoRefresh = () => {
|
||||
refetch();
|
||||
loadProjects();
|
||||
};
|
||||
|
||||
const handleProjectSelect = (project: Project | null) => {
|
||||
@@ -133,7 +133,7 @@ export default function Index() {
|
||||
<div className="w-8" /> {/* Spacer */}
|
||||
</header>
|
||||
|
||||
{/* Mobile Sidebar Overlay - only show if user has projects */}
|
||||
{/* Mobile Sidebar Overlay - only show if user has local projects */}
|
||||
{projects.length > 0 && isSidebarVisible && (
|
||||
<div className="fixed inset-0 z-50 bg-background/80 backdrop-blur-sm">
|
||||
{/* Backdrop - only covers the area not occupied by the sidebar */}
|
||||
@@ -228,7 +228,7 @@ export default function Index() {
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h2 className="text-xl md:text-2xl font-semibold mb-4 md:mb-6">Your Projects</h2>
|
||||
<h2 className="text-xl md:text-2xl font-semibold mb-4 md:mb-6">Your Repositories</h2>
|
||||
|
||||
{isLoading ? (
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
@@ -241,49 +241,33 @@ export default function Index() {
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
) : projects.length === 0 ? (
|
||||
) : error ? (
|
||||
<Card className="border-destructive/20">
|
||||
<CardContent className="py-6 text-center">
|
||||
<p className="text-destructive">Failed to load repositories</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : repos.length === 0 ? (
|
||||
<Card className="bg-gradient-to-br from-muted/50 to-muted/20 border-dashed border-2 border-muted-foreground/20">
|
||||
<CardContent className="text-center py-8 md:py-12">
|
||||
<div className="flex items-center justify-center gap-2 mb-4">
|
||||
<Folder className="h-10 w-10 md:h-12 md:w-12 text-muted-foreground" />
|
||||
</div>
|
||||
<p className="text-muted-foreground text-sm md:text-base">No projects yet. Create your first project above!</p>
|
||||
<p className="text-muted-foreground text-sm md:text-base">No repositories yet. Create your first project above!</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{projects.map((project) => (
|
||||
<Card
|
||||
key={project.id}
|
||||
className="cursor-pointer hover:shadow-xl hover:shadow-primary/10 transition-all duration-300 focus-ring bg-gradient-to-br from-card to-card/80 border-primary/10 hover:border-primary/20 hover:scale-[1.02]"
|
||||
onClick={() => handleProjectClick(project.id)}
|
||||
tabIndex={0}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault();
|
||||
handleProjectClick(project.id);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base md:text-lg">{project.name}</CardTitle>
|
||||
<CardDescription className="text-sm">
|
||||
Modified {project.lastModified.toLocaleDateString()}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
|
||||
</Card>
|
||||
{repos.map((repo) => (
|
||||
<RepoCard
|
||||
key={repo.id}
|
||||
repo={repo}
|
||||
onRepoCloned={handleRepoRefresh}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Nostr Repositories Section - Mobile */}
|
||||
{user && (
|
||||
<div className="mt-8">
|
||||
<NostrRepos excludeProjectIds={nostrEnabledProjectIds} onProjectCloned={loadProjects} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -298,6 +282,7 @@ export default function Index() {
|
||||
onProjectCreated={(projectId) => {
|
||||
console.log('Project created during onboarding:', projectId);
|
||||
setStoredPrompt('');
|
||||
handleRepoRefresh();
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
@@ -318,10 +303,11 @@ export default function Index() {
|
||||
// Handle project creation during onboarding
|
||||
console.log('Project created during onboarding:', projectId);
|
||||
setStoredPrompt(''); // Clear stored prompt after project creation
|
||||
handleRepoRefresh();
|
||||
}}
|
||||
/>
|
||||
<div className="min-h-screen flex bg-background">
|
||||
{/* Fixed Sidebar - only show if user has created projects */}
|
||||
{/* Fixed Sidebar - only show if user has created local projects */}
|
||||
{projects.length > 0 && (
|
||||
<div className={cn("w-80 border-r bg-sidebar transition-all duration-300", isSidebarVisible ? "block" : "hidden")}>
|
||||
<ProjectSidebar
|
||||
@@ -335,7 +321,7 @@ export default function Index() {
|
||||
{/* Main Content Area */}
|
||||
<div className="flex-1 flex flex-col bg-background">
|
||||
<div className="min-h-screen bg-gradient-to-br from-primary/5 to-accent/5">
|
||||
{/* Header - only show toggle button if user has projects */}
|
||||
{/* Header - only show toggle button if user has local projects */}
|
||||
{!isSidebarVisible && projects.length > 0 && (
|
||||
<header className="border-b bg-gradient-to-r from-primary/10 via-accent/5 to-primary/10 backdrop-blur px-4 py-3 flex items-center">
|
||||
<Button
|
||||
@@ -416,7 +402,7 @@ export default function Index() {
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h2 className="text-xl md:text-2xl font-semibold mb-4 md:mb-6">Your Projects</h2>
|
||||
<h2 className="text-xl md:text-2xl font-semibold mb-4 md:mb-6">Your Repositories</h2>
|
||||
|
||||
{isLoading ? (
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
@@ -429,49 +415,33 @@ export default function Index() {
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
) : projects.length === 0 ? (
|
||||
) : error ? (
|
||||
<Card className="border-destructive/20">
|
||||
<CardContent className="py-6 text-center">
|
||||
<p className="text-destructive">Failed to load repositories</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : repos.length === 0 ? (
|
||||
<Card className="bg-gradient-to-br from-muted/50 to-muted/20 border-dashed border-2 border-muted-foreground/20">
|
||||
<CardContent className="text-center py-8 md:py-12">
|
||||
<div className="flex items-center justify-center gap-2 mb-4">
|
||||
<Folder className="h-10 w-10 md:h-12 md:w-12 text-muted-foreground" />
|
||||
</div>
|
||||
<p className="text-muted-foreground text-sm md:text-base">No projects yet. Create your first project above!</p>
|
||||
<p className="text-muted-foreground text-sm md:text-base">No repositories yet. Create your first project above!</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{projects.map((project) => (
|
||||
<Card
|
||||
key={project.id}
|
||||
className="cursor-pointer hover:shadow-xl hover:shadow-primary/10 transition-all duration-300 focus-ring bg-gradient-to-br from-card to-card/80 border-primary/10 hover:border-primary/20 hover:scale-[1.02]"
|
||||
onClick={() => handleProjectClick(project.id)}
|
||||
tabIndex={0}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault();
|
||||
handleProjectClick(project.id);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base md:text-lg">{project.name}</CardTitle>
|
||||
<CardDescription className="text-sm">
|
||||
Modified {project.lastModified.toLocaleDateString()}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
|
||||
</Card>
|
||||
{repos.map((repo) => (
|
||||
<RepoCard
|
||||
key={repo.id}
|
||||
repo={repo}
|
||||
onRepoCloned={handleRepoRefresh}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Nostr Repositories Section - Desktop */}
|
||||
{user && (
|
||||
<div className="mt-8">
|
||||
<NostrRepos excludeProjectIds={nostrEnabledProjectIds} onProjectCloned={loadProjects} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user