From d396c8673a8e452660583326b05f844a3c5aa507 Mon Sep 17 00:00:00 2001 From: neil Date: Thu, 9 Feb 2023 20:08:41 +0800 Subject: [PATCH] #183 render contributors --- modules/desktop/src/libs/api/electron.ts | 1 - modules/desktop/src/libs/github.ts | 19 ++++- modules/desktop/src/libs/stores/pkgs.ts | 70 +++++++++---------- modules/desktop/src/libs/types.ts | 1 + .../src/routes/packages/[slug]/+page.svelte | 3 +- .../ui/src/PackageMetas/PackageMetas.svelte | 43 +++++------- modules/ui/src/types.ts | 2 +- 7 files changed, 71 insertions(+), 68 deletions(-) diff --git a/modules/desktop/src/libs/api/electron.ts b/modules/desktop/src/libs/api/electron.ts index 7971f82..2018eac 100644 --- a/modules/desktop/src/libs/api/electron.ts +++ b/modules/desktop/src/libs/api/electron.ts @@ -100,7 +100,6 @@ export async function getPackageBottles(packageName: string): Promise } export async function getPackage(packageName: string): Promise> { - console.log('getting package:', packageName); const pkg: Partial = await apiGet>( `packages/${packageName.replaceAll('/', ':')}` ); diff --git a/modules/desktop/src/libs/github.ts b/modules/desktop/src/libs/github.ts index bad471f..1bb57c4 100644 --- a/modules/desktop/src/libs/github.ts +++ b/modules/desktop/src/libs/github.ts @@ -1,11 +1,10 @@ import axios from 'axios'; +import type { Contributor } from '@tea/ui/types'; const yaml = window.require('yaml'); export async function getGithubOwnerRepo( pkgYamlUrl: string ): Promise<{ owner: string; repo: string }> { - // https://github.com/teaxyz/pantry.core/blob/main/projects/sqlite.org/package.yml - // https://raw.githubusercontent.com/teaxyz/pantry.core/main/projects/sqlite.org/package.yml let owner = ''; let repo = ''; @@ -33,3 +32,19 @@ export async function getReadme(owner: string, repo: string): Promise { } return readme; } + +export async function getContributors(owner: string, repo: string): Promise { + // maintainer/repo + let contributors: Contributor[] = []; + const req = await axios.get(`https://api.github.com/repos/${owner}/${repo}/contributors`); + if (req.data) { + contributors = req.data.map((c: Contributor & { id: number }) => ({ + login: c.login, + avatar_url: c.avatar_url, + name: c.name || '', + github_id: c.id, + contributions: c.contributions + })); + } + return contributors; +} diff --git a/modules/desktop/src/libs/stores/pkgs.ts b/modules/desktop/src/libs/stores/pkgs.ts index 5b0ac8d..9e1dcda 100644 --- a/modules/desktop/src/libs/stores/pkgs.ts +++ b/modules/desktop/src/libs/stores/pkgs.ts @@ -4,7 +4,7 @@ import { getPackages } from '@api'; import Fuse from 'fuse.js'; import { getPackage } from '@api'; -import { getGithubOwnerRepo, getReadme } from '$libs/github'; +import { getReadme, getContributors } from '$libs/github'; export default function initPackagesStore() { let initialized = false; @@ -37,13 +37,40 @@ export default function initPackagesStore() { }); }; + const syncPackageData = async (guiPkg: Partial) => { + if (guiPkg.synced) return; + + const pkg = await getPackage(guiPkg.full_name!); // ATM: pkg only bottles and github:string + const readmeMd = `# ${guiPkg.full_name} # +To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}). + `; + + const updatedPackage: Partial = { + ...pkg, + readme_md: readmeMd, + synced: true + }; + if (pkg.github) { + const [owner, repo] = pkg.github.split('/'); + const [readme, contributors] = await Promise.all([ + getReadme(owner, repo), + getContributors(owner, repo) + ]); + if (readme) { + updatedPackage.readme_md = readme; + } + updatedPackage.contributors = contributors; + } + + updatePackageProp(guiPkg.full_name!, updatedPackage); + }; + return { packages, subscribe, search: async (term: string, limit = 5): Promise => { if (!term || !packagesIndex) return []; // TODO: if online, use algolia else use Fuse - const res = packagesIndex.search(term, { limit }); const matchingPackages: GUIPackage[] = res.map((v) => v.item); return matchingPackages; @@ -51,44 +78,11 @@ export default function initPackagesStore() { subscribeToPackage: (slug: string, cb: (pkg: GUIPackage) => void) => { subscribe((pkgs) => { const foundPackage = pkgs.find((p) => p.slug === slug) as GUIPackage; - if (foundPackage) cb(foundPackage); - // get readme - // get contributors - // get github last modified - // subscribe((pkgs) => cb(pkgs[pkg])); - - // console.log('f:', foundPackage); - // getReadmeRaw(''); - - if (!foundPackage.bottles) { - getPackage(foundPackage.full_name).then((pkg) => { - updatePackageProp(foundPackage.full_name, pkg); - }); - } - - if (!foundPackage.readme_md && foundPackage.package_yml_url) { - getGithubOwnerRepo(foundPackage.package_yml_url).then(async ({ owner, repo }) => { - const defaultReadme = `# ${foundPackage.full_name} # -To read more about this package go to [${foundPackage.homepage}](${foundPackage.homepage}). - `; - if (owner && repo) { - const readme = await getReadme(owner, repo); - updatePackageProp(foundPackage.full_name, { readme_md: readme || defaultReadme }); - } else { - updatePackageProp(foundPackage.full_name, { readme_md: defaultReadme }); - } - }); + if (foundPackage) { + cb(foundPackage); + syncPackageData(foundPackage); } }); } }; } - -async function getReadmeRaw(owner: string, repo: string): Promise { - // const rep = await getRepo('oven-sh', 'bun'); - // const repo = await octokit.request('GET /repos/{owner}/{repo}', { - // owner: '', - // repo: '', - // }); - return ''; -} diff --git a/modules/desktop/src/libs/types.ts b/modules/desktop/src/libs/types.ts index fc0b31a..6ebf033 100644 --- a/modules/desktop/src/libs/types.ts +++ b/modules/desktop/src/libs/types.ts @@ -15,6 +15,7 @@ export enum PackageStates { export type GUIPackage = Package & { state: PackageStates; installed_version?: string; + synced?: boolean; }; export type Course = { diff --git a/modules/desktop/src/routes/packages/[slug]/+page.svelte b/modules/desktop/src/routes/packages/[slug]/+page.svelte index 05ce657..4f9f0e3 100644 --- a/modules/desktop/src/routes/packages/[slug]/+page.svelte +++ b/modules/desktop/src/routes/packages/[slug]/+page.svelte @@ -32,6 +32,7 @@ packagesStore.subscribeToPackage(data?.slug, (p) => { pkg = p; + if (!bottles.length && pkg.bottles) { const newVersion = pkg.bottles.map((b) => b.version); versions = [...new Set(newVersion)]; @@ -74,7 +75,7 @@
- +
SNIPPETS diff --git a/modules/ui/src/PackageMetas/PackageMetas.svelte b/modules/ui/src/PackageMetas/PackageMetas.svelte index 13019bb..0614238 100644 --- a/modules/ui/src/PackageMetas/PackageMetas.svelte +++ b/modules/ui/src/PackageMetas/PackageMetas.svelte @@ -1,4 +1,7 @@