From 353d1cf69950ae52d2fca6e2f7c75da09a76a964 Mon Sep 17 00:00:00 2001 From: Gcat101 Date: Tue, 30 Jan 2024 00:11:12 +0300 Subject: [PATCH] orgs part 2 (lord forgive me for I have sinned) --- src/lib/project/Organizations.svelte | 51 +++++++++-------------- src/lib/project/PackageList.svelte | 5 ++- src/lib/utils.ts | 18 +++++++- src/routes/+layout.svelte | 2 +- src/routes/s/+page.svelte | 61 +++++++++++++++++++++++----- 5 files changed, 91 insertions(+), 46 deletions(-) diff --git a/src/lib/project/Organizations.svelte b/src/lib/project/Organizations.svelte index 2ffccdf..39c82a2 100644 --- a/src/lib/project/Organizations.svelte +++ b/src/lib/project/Organizations.svelte @@ -1,36 +1,25 @@ -{#await getGhInfo(`users/${author}/orgs`, $ghApiKeyStore) then orgs} - - {#if orgs.length>0} -
-
Organizations
- {#each orgs as org} - {@const c = filterPkgsByAuthor(org.login).length} - - {#if c>0} - - organization's profile avatar -
-
{org.login}
-
- owns {c} - {c == 1 ? 'package' : 'packages'} -
-
-
- {/if} - {/each} -
- {/if} -{/await} \ No newline at end of file +
+
Organizations
+ {#each orgs as org} + + organization's profile avatar +
+
{org.login}
+
+ owns {org.pkg_count} + {org.pkg_count == 1 ? 'package' : 'packages'} +
+
+
+ {/each} +
\ No newline at end of file diff --git a/src/lib/project/PackageList.svelte b/src/lib/project/PackageList.svelte index 5889cee..f4ae781 100644 --- a/src/lib/project/PackageList.svelte +++ b/src/lib/project/PackageList.svelte @@ -14,10 +14,12 @@ /** name and author are sorted alphabetically. */ export let sortBy: '' | 'name' | 'author' | 'downloads' | 'views' = ''; export let maxCount = Infinity; + export let startFrom = 0; export let showAvatar = true; export let showDetails = true; export let showName = true; export let compact = false; + export let customHeight : number | null = null; $: sortedP = (() => { const alphabetic = [...p].sort((_a, _b) => { @@ -52,7 +54,7 @@ })(); -{#each [...sortedP].slice(0, maxCount) as [name, locator], i (name)} +{#each [...sortedP].slice(startFrom, maxCount) as [name, locator], i (name)} {@const locatorInfo = locator.match(consts.LOCATOR_REGEX) ?? [null, null, null, null, null]} {@const author = locatorInfo[1]} {@const repo = locatorInfo[2]} @@ -70,6 +72,7 @@ class:!variant-filled-primary={$page.url.searchParams.get('id') == name} animate:flip={{ duration: 500 }} transition:fade={{ duration: 300 }} + style={customHeight!=null ? `height: ${customHeight}rem` : ''} > {#if showAvatar && !compact} { + let orgsWithPackageCount : any[] = []; + let orgs : [any] = await getGhInfo(`users/${a}/orgs`, key); + if (Object.keys(orgs).length==0) return []; + + orgs.forEach(org => { + org.pkg_count = filterPkgsByAuthor(org.login).length; + if (org.pkg_count>0) orgsWithPackageCount.push(org); + // for (let i=0; i<4; i++) {if (org.pkg_count>0) orgsWithPackageCount.push(org);} + }); + return orgsWithPackageCount; +} export function capitalizeFirstLetter(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1); @@ -109,9 +121,11 @@ export function removeBase(target: string, base: string): string { } export async function getGhInfo(path : string, key: string | null) : Promise { - return await (await fetch(`${consts.GH_API_URL}/${path}`, {headers: { + let r : Response = await fetch(`${consts.GH_API_URL}/${path}`, {headers: { 'Accept': 'application/vnd.github+json', 'Authorization': (key!=null ? 'Bearer '+key : ''), 'X-GitHub-Api-Version': '2022-11-28' - }})).json(); + }}); + if (r.status!=200) return {}; + return await r.json(); } \ No newline at end of file diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 1c86a63..35c6eb9 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -72,7 +72,7 @@ -
+
{#key data.href} {/key} diff --git a/src/routes/s/+page.svelte b/src/routes/s/+page.svelte index 65af287..5d662ec 100644 --- a/src/routes/s/+page.svelte +++ b/src/routes/s/+page.svelte @@ -7,6 +7,7 @@ import { contextMenu } from '$lib/overlays/contextMenu'; import { currentSearchStore, + ghApiKeyStore, ghApiLoginStore, packageListStore, packageStatusStore, @@ -16,6 +17,7 @@ filterObjectByKey, filterPkgsByAuthor, generateInputString, + getNonEmptyOrgsWithPackageCount, initPackageList, parseInputString } from '$lib/utils'; @@ -30,6 +32,8 @@ }); $: queryParams = parseInputString($currentSearchStore); + let largeScreen = matchMedia('(min-width: 1024px)').matches; + window.onresize = () => {largeScreen = matchMedia('(min-width: 1024px)').matches} $: $packageStatusStore.search.d = Object.entries( (!queryParams.ROOT @@ -202,10 +206,32 @@
{#if queryParams.author && (filteredAuthor.length > 0 || queryParams.author==$ghApiLoginStore)} -
+ + {#await getNonEmptyOrgsWithPackageCount(queryParams.author, $ghApiKeyStore)} - -
+ {:then orgs} +
+
+ + {#if largeScreen} + + {/if} +
+ {#if orgs.length>0} + + {/if} +
+ {/await} {/if} {#if state == 'loading'} @@ -239,14 +265,27 @@ class:md:grid-cols-2={$userPreferencesStore.compact} class:lg:grid-cols-3={$userPreferencesStore.compact} > - + {#await getNonEmptyOrgsWithPackageCount('Gcat101', $ghApiKeyStore)} + + {:then orgs} + 0 ? orgs.length-1 : 0} + /> + {/await} {:else if state == 'fail'}

Something went wrong