Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search bar update #18

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 152 additions & 21 deletions frontend/app/packages/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,170 @@ import Link from 'next/link';
interface Package {
id: string;
name: string;
author: string;
version: string;
description: string;
version: string;
author: string;
metrics: {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResponsiveMaintainer and NetScore should be added to metrics. This is an minor thing that could be added in the future

busFactor: number;
correctness: number;
rampUp: number;
license: string;
};
}

export default function PackageDirectory() {
const [packages, setPackages] = useState<Package[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [sortBy, setSortBy] = useState<'name' | 'author'>('name');

useEffect(() => {
async function fetchPackages() {
const fakePackages: Package[] = [
{ id: '1', name: 'package-one', author: 'Author One', version: '1.0.0', description: 'This is the first package' },
{ id: '2', name: 'package-two', author: 'Author Two', version: '2.0.0', description: 'This is the second package' },
{ id: '3', name: 'package-three', author: 'Author Three', version: '3.0.0', description: 'This is the third package' },
];
setPackages(fakePackages);
}
const fetchPackages = async () => {
setIsLoading(true);
setError(null);

try {
// TODO: Replace with actual API call
await new Promise(resolve => setTimeout(resolve, 1000));

// Dummy data for demonstration
const dummyPackages: Package[] = [
{
id: '1',
name: 'express',
description: 'Fast, unopinionated, minimalist web framework for Node.js',
version: '4.18.2',
author: 'TJ Holowaychuk',
metrics: {
busFactor: 0.9,
correctness: 0.95,
rampUp: 0.8,
license: 'MIT',
},
},
{
id: '2',
name: 'lodash',
description: 'A modern JavaScript utility library delivering modularity, performance & extras',
version: '4.17.21',
author: 'John-David Dalton',
metrics: {
busFactor: 0.85,
correctness: 0.9,
rampUp: 0.75,
license: 'MIT',
},
},
{
id: '3',
name: 'react',
description: 'A JavaScript library for building user interfaces',
version: '18.2.0',
author: 'Meta Open Source',
metrics: {
busFactor: 0.95,
correctness: 0.98,
rampUp: 0.85,
license: 'MIT',
},
},
];

setPackages(dummyPackages);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Failed to fetch packages';
setError(errorMessage);
} finally {
setIsLoading(false);
}
};

fetchPackages();
}, []);

const sortedPackages = [...packages].sort((a, b) => {
if (sortBy === 'name') {
return a.name.localeCompare(b.name);
}
return a.author.localeCompare(b.author);
});

return (
<div className="max-w-4xl mx-auto p-8">
<h1 className="text-3xl font-bold mb-6">Package Directory</h1>
<ul className="space-y-4">
{packages.map((pkg) => (
<li key={pkg.id}>
<Link href={`/packages/${pkg.name}`} className="text-blue-500 hover:underline">
{pkg.name}
</Link>
</li>
))}
</ul>
<div className="max-w-6xl mx-auto p-8">
<header className="mb-8">
<div className="bg-gray-100 p-6 rounded-lg mb-6">
<h1 className="text-3xl font-bold">Package Directory</h1>
<p className="text-gray-600 mt-2">Browse all available packages in our registry</p>
</div>

{isLoading ? (
<div role="status" className="flex justify-center items-center min-h-[200px]">
<span className="sr-only">Loading packages...</span>
<div className="animate-spin h-8 w-8 border-4 border-blue-500 border-t-transparent rounded-full"></div>
</div>
) : error ? (
<div role="alert" className="text-red-600 text-center p-4 bg-red-50 rounded-md">
{error}
</div>
) : (
<div>
<div className="flex justify-between items-center mb-6">
<p className="text-gray-600">{packages.length} packages available</p>
<div className="flex items-center gap-2">
<label htmlFor="sort-select" className="text-sm text-gray-600">
Sort by:
</label>
<select
id="sort-select"
value={sortBy}
onChange={(e) => setSortBy(e.target.value as 'name' | 'author')}
className="border border-gray-300 rounded-md px-2 py-1"
>
<option value="name">Name</option>
<option value="author">Author</option>
</select>
</div>
</div>

<ul className="space-y-6">
{sortedPackages.map((pkg) => (
<li key={pkg.id} className="border border-gray-200 rounded-lg p-6 hover:bg-gray-50 transition-colors">
<Link href={`/packages/${pkg.name}`} className="block">
<div className="flex justify-between items-start mb-2">
<h2 className="text-xl font-semibold text-blue-600 hover:underline">
{pkg.name}
</h2>
<span className="text-sm text-gray-500">v{pkg.version}</span>
</div>
<p className="text-gray-600 mb-4">{pkg.description}</p>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 text-sm">
<div>
<span className="text-gray-500">Bus Factor:</span>
<span className="ml-2">{pkg.metrics.busFactor.toFixed(2)}</span>
</div>
<div>
<span className="text-gray-500">Correctness:</span>
<span className="ml-2">{pkg.metrics.correctness.toFixed(2)}</span>
</div>
<div>
<span className="text-gray-500">Ramp Up:</span>
<span className="ml-2">{pkg.metrics.rampUp.toFixed(2)}</span>
</div>
<div>
<span className="text-gray-500">License:</span>
<span className="ml-2">{pkg.metrics.license}</span>
</div>
</div>
<div className="mt-4 text-sm text-gray-500">
<span className="font-medium">Author:</span> {pkg.author}
</div>
</Link>
</li>
))}
</ul>
</div>
)}
</header>
</div>
);
}
170 changes: 170 additions & 0 deletions frontend/app/packages/search/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
'use client';

import { useEffect, useState, Suspense } from 'react';
import { useSearchParams } from 'next/navigation';
import Link from 'next/link';

interface Package {
id: string;
name: string;
description: string;
version: string;
author: string;
metrics: {
busFactor: number;
correctness: number;
rampUp: number;
license: string;
};
score: number;
}

function SearchResultsContent() {
const searchParams = useSearchParams();
const query = searchParams.get('q');
const searchType = searchParams.get('type');

const [packages, setPackages] = useState<Package[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [sortBy, setSortBy] = useState<'score' | 'name'>('score');

useEffect(() => {
const fetchSearchResults = async () => {
setIsLoading(true);
setError(null);

try {
await new Promise(resolve => setTimeout(resolve, 1000));

const dummyResults: Package[] = [
{
id: '1',
name: 'express',
description: 'Fast, unopinionated, minimalist web framework for Node.js',
version: '4.18.2',
author: 'TJ Holowaychuk',
metrics: {
busFactor: 0.9,
correctness: 0.95,
rampUp: 0.8,
license: 'MIT',
},
score: 0.95,
},
];

setPackages(dummyResults);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Failed to fetch search results';
setError(errorMessage);
} finally {
setIsLoading(false);
}
};

fetchSearchResults();
}, [query, searchType]);

const sortedPackages = [...packages].sort((a, b) => {
if (sortBy === 'score') {
return b.score - a.score;
}
return a.name.localeCompare(b.name);
});

return (
<div className="max-w-6xl mx-auto p-8">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of html. Is it possible to break it up into React components? I'll leave this up to you, but I think it would improve readability, especially if you can reuse components.

<header className="mb-8">
<div className="bg-gray-100 p-6 rounded-lg mb-6">
<h1 className="text-3xl font-bold mb-4">Search Results</h1>
<div className="flex flex-col gap-2">
<p className="text-lg">
<span className="font-medium">Search Query:</span> &ldquo;{query}&rdquo;
</p>
<p className="text-gray-600">
<span className="font-medium">Search Type:</span> {searchType === 'smart' ? 'Smart Search' : 'Regular Search'}
</p>
</div>
</div>

{isLoading ? (
<div role="status" className="flex justify-center items-center min-h-[200px]">
<span className="sr-only">Loading search results...</span>
<div className="animate-spin h-8 w-8 border-4 border-blue-500 border-t-transparent rounded-full"></div>
</div>
) : error ? (
<div role="alert" className="text-red-600 text-center p-4 bg-red-50 rounded-md">
{error}
</div>
) : (
<div>
<div className="flex justify-between items-center mb-6">
<p className="text-gray-600">{packages.length} packages found</p>
<div className="flex items-center gap-2">
<label htmlFor="sort-select" className="text-sm text-gray-600">
Sort by:
</label>
<select
id="sort-select"
value={sortBy}
onChange={(e) => setSortBy(e.target.value as 'score' | 'name')}
className="border border-gray-300 rounded-md px-2 py-1"
>
<option value="score">Match Score</option>
<option value="name">Name</option>
</select>
</div>
</div>

<ul className="space-y-6">
{sortedPackages.map((pkg) => (
<li key={pkg.id} className="border border-gray-200 rounded-lg p-6 hover:bg-gray-50">
<Link href={`/packages/${pkg.name}`} className="block">
<div className="flex justify-between items-start mb-2">
<h2 className="text-xl font-semibold text-blue-600 hover:underline">
{pkg.name}
</h2>
<span className="text-sm text-gray-500">v{pkg.version}</span>
</div>
<p className="text-gray-600 mb-4">{pkg.description}</p>
<div className="grid grid-cols-2 sm:grid-cols-4 gap-4 text-sm">
<div>
<span className="text-gray-500">Bus Factor:</span>
<span className="ml-2">{pkg.metrics.busFactor.toFixed(2)}</span>
</div>
<div>
<span className="text-gray-500">Correctness:</span>
<span className="ml-2">{pkg.metrics.correctness.toFixed(2)}</span>
</div>
<div>
<span className="text-gray-500">Ramp Up:</span>
<span className="ml-2">{pkg.metrics.rampUp.toFixed(2)}</span>
</div>
<div>
<span className="text-gray-500">License:</span>
<span className="ml-2">{pkg.metrics.license}</span>
</div>
</div>
</Link>
</li>
))}
</ul>
</div>
)}
</header>
</div>
);
}

export default function SearchResults() {
return (
<Suspense fallback={
<div className="flex justify-center items-center min-h-screen">
<div className="animate-spin h-8 w-8 border-4 border-blue-500 border-t-transparent rounded-full"></div>
</div>
}>
<SearchResultsContent />
</Suspense>
);
}
Loading
Loading