Compare commits

...

5 Commits

Author SHA1 Message Date
Eshan Roy
f36187ec73 Merge PR #6 from XlebyllleK/master
feat: Filter out unused repositories
2025-01-11 23:02:18 +05:30
XlebyllleK
b79bb58829 Refactor: Update and remove comments 2025-01-11 19:29:28 +02:00
XlebyllleK
1f652f2bb9 feat: Filter out unused repositories 2025-01-11 19:16:14 +02:00
Eshan Roy
c8fa507ef8 Merge PR #5 from XlebyllleK/master
chore: update api.ts
2025-01-11 22:27:57 +05:30
XlebyllleK
0fad592599 Update api.ts 2025-01-11 18:55:43 +02:00
2 changed files with 92 additions and 64 deletions

View File

@@ -9,35 +9,52 @@ import {
Repository
} from '../types';
import {
MIRRORS
} from '../services/api';
interface HeaderProps {
onRepositoryChange: (repo: Repository) => void;
onRepositoryChange: (repo: Repository) => void;
}
export function Header({ onRepositoryChange }: HeaderProps): JSX.Element {
return (
<header className="bg-gradient-to-r from-nord-9 to-nord-8 via-nord-10 text-nord-6 shadow-lg">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
<div className="flex items-center justify-between">
{/* Logo Section */}
<div className="flex items-center gap-4">
<Logo />
</div>
export function Header({
onRepositoryChange
}: HeaderProps): JSX.Element {
const usedRepositories = new Set(Object.values(MIRRORS).map(mirror => mirror.repository));
const filteredRepository = Object.keys(Repository).reduce((acc, key) => {
// This code is flawed because it explicitly checks for 'ALL', reducing flexibility.
// A more generic approach should be used to filter unused repositories while preserving 'ALL'.
if ((key === 'ALL') || usedRepositories.has(Repository[key as keyof typeof Repository])) acc[key] = Repository[key as keyof typeof Repository];
return acc;
}, {} as Record<string, string>);
{/* Repository Filter Dropdown */}
<div>
<select onChange={
(e) => onRepositoryChange(e.target.value as Repository)
} defaultValue="all" className="bg-nord-5 dark:bg-nord-1 text-black dark:text-white border-2 border-nord-4 dark:border-nord-2 rounded-lg py-2 px-4 focus:ring-2 focus:ring-nord-8">
{Object.values(Repository).map((repository) => (
<option key={repository} value={repository}>{((repository === Repository.ALL) ? 'All Repositories' : repository.charAt(0).toUpperCase() + repository.slice(1))}</option>
))}
</select>
</div>
return (
<header className="bg-gradient-to-r from-nord-9 to-nord-8 via-nord-10 text-nord-6 shadow-lg">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
<div className="flex items-center justify-between">
{/* Logo Section */}
{/* Theme Toggle */}
<ThemeToggle />
</div>
</div>
</header>
);
<div className="flex items-center gap-4">
<Logo />
</div>
{/* Repository Filter Dropdown */}
<div>
<select onChange={
(e) => onRepositoryChange(e.target.value as Repository)
} defaultValue="all" className="bg-nord-5 dark:bg-nord-1 text-black dark:text-white border-2 border-nord-4 dark:border-nord-2 rounded-lg py-2 px-4 focus:ring-2 focus:ring-nord-8">
{Object.values(filteredRepository).map((repository) => (
<option key={repository} value={repository}>{((repository === Repository.ALL) ? 'All Repositories' : repository.charAt(0).toUpperCase() + repository.slice(1))}</option>
))}
</select>
</div>
{/* Theme Toggle */}
<ThemeToggle />
</div>
</div>
</header>
);
}

View File

@@ -1,53 +1,64 @@
import { Package } from '../types';
import {
Package,
Repository
} from '../types';
// Define the mirrors from which packages will be fetched
const MIRRORS = [
'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-core/refs/heads/master/packages.txt',
'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-extra/refs/heads/master/packages.txt'
];
export const MIRRORS: Record<string, {
url: string;
repository: Repository;
}> = {
'core': {
url: 'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-core/refs/heads/master/packages.txt',
repository: ('core' as Repository)
},
'extra': {
url: 'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-extra/refs/heads/master/packages.txt',
repository: ('extra' as Repository)
}
}
// Fetch data from a single mirror (Core or Extra repository)
async function fetchFromMirror(url: string): Promise<Package[]> {
const response = await fetch(url);
const text = await response.text();
async function fetchFromMirror(url: string, repository: Repository): Promise<Package[]> {
const response = await fetch(url);
const text = await response.text();
// Determine the repository name (core or extra) from the URL
const repository = url.includes('snigdhaos-core') ? 'core' : 'extra';
// Parse the text file content and return a list of packages
return text.split('\n').filter(Boolean).map((line) => {
const [
name,
version,
...description
] = line.split(' ');
// Parse the text file content and return a list of packages
return text
.split('\n') // Split by line
.filter(Boolean) // Remove empty lines
.map((line) => {
const [name, version, ...descParts] = line.split(' ');
return {
name,
version,
description: descParts.join(' '),
repository, // Attach the repository name to each package
};
return {
name,
version,
description: description.join(' '),
repository
};
});
}
// Fetch packages from all mirrors and combine them
export async function fetchPackages(): Promise<Package[]> {
let packages: Package[] = [];
let packages: Package[] = [];
// Try fetching from each mirror and accumulate the results
for (const mirror of MIRRORS) {
try {
const result = await fetchFromMirror(mirror);
packages = packages.concat(result); // Append the packages from this mirror
} catch (error) {
console.warn(`Failed to fetch from mirror ${mirror}:`, error);
continue; // Continue to the next mirror if this one fails
// Try fetching from each mirror and accumulate the results
for (const key in MIRRORS) {
const mirror = MIRRORS[key];
try {
const result = await fetchFromMirror(mirror.url, mirror.repository);
packages = packages.concat(result); // Append the packages from this mirror
} catch (error) {
console.warn(`Failed to fetch from mirror ${mirror.url}:`, error);
continue; // Continue to the next mirror if this one fails
}
}
}
// If no successful fetch, throw an error
if (packages.length === 0) {
throw new Error('All mirrors failed to respond');
}
return packages;
// If no successful fetch, throw an error
if (packages.length === 0) throw new Error('All mirrors failed to respond');
return packages;
}