From 080f8e9642860b96cfbce80e31cf3dd56a2a20ed Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sat, 14 Sep 2024 17:59:12 -0500 Subject: [PATCH] fix(#1): add recent packages to homepage --- config.nims | 2 +- src/app.nim | 2 +- src/components/package.nim | 35 +++++++++++++++++------------------ src/components/tag.nim | 2 +- src/context.nim | 19 +++++++++++++++---- src/packages.nim | 1 + src/pages/index.nim | 9 ++++++--- 7 files changed, 42 insertions(+), 28 deletions(-) diff --git a/config.nims b/config.nims index 8471699..18b699e 100644 --- a/config.nims +++ b/config.nims @@ -8,7 +8,7 @@ task watch, "rebuild on change": exec ( "watchexec " & "--project-origin . -w src " & - "nim js -d:packagesHash:master -o:site/app.js src/app.nim" + "nim js -d:debug -d:packagesHash:master -o:site/app.js src/app.nim" ) # begin Nimble config (version 2) diff --git a/src/app.nim b/src/app.nim index 901ff8c..5d41bfa 100644 --- a/src/app.nim +++ b/src/app.nim @@ -5,7 +5,7 @@ import pages/pages import context, utils proc render(data: RouterData): VNode = - console.log ctx + when defined(debug): console.log ctx var uri = currentUri() result = buildHtml(tdiv( class = "lg:w-3/4 max-w-[90%] mx-auto md:text-lg text-sm min-h-screen flex flex-col") diff --git a/src/components/package.nim b/src/components/package.nim index b63c184..2377c91 100644 --- a/src/components/package.nim +++ b/src/components/package.nim @@ -1,4 +1,4 @@ -import std/[algorithm, strutils, sequtils, jsconsole, uri, random] +import std/[strutils, sequtils, uri, random] import karax/[kbase, karax, karaxdsl, vdom, jstrutils, ] @@ -7,6 +7,7 @@ import ../components/tag import ../utils randomize() + proc authorRepo(uri: Uri, hostname = false): kstring = var name = if hostname: uri.hostname & uri.path.replace(".git") @@ -58,29 +59,27 @@ proc card*(pkg: NimPackage): VNode = class = "link"): t.renderTag -proc getRecentReleases(ctx: Context): seq[NimPackage] = - var pkgs: seq[NimPackage] - for pkg in ctx.nimpkgs.packages.values(): - if pkg.versions.len > 0: - pkgs.add pkg - - pkgs.sort(sortVersion, order = Descending) - return pkgs[0..20] - -proc recentPackageVersionList*(ctx: Context): VNode = - let pkgs = ctx.getRecentReleases +proc recentAddedPackagesList*(): VNode = + let pkgs = recentPackagesList() result = buildHtml(tdiv(class = "flex flex-wrap")): for pkg in pkgs: - a(class = borderStyle & "p-2 m-1 space-x-1 no-underline text-ctp-text", + a(class = borderStyle & "group p-2 m-1 space-x-1 no-underline text-ctp-text)", href = "/#/pkg/" & pkg.name): - span(class = textStyle & "font-bold font-mono-casual"): text pkg.name - span(class = "italic"): text pkg.versions[0].tag - span: - text " (" & (getTime() - pkg.versions[0].time).inDays.jss & " days ago)" + span(class = textStyle & "group-hover:text-ctp-mauve font-bold font-mono-casual"): text pkg.name + +proc recentPackageVersionsList*(): VNode = + let pkgs = getRecentReleases() + result = buildHtml(tdiv(class = "flex flex-wrap")): + for pkg in pkgs: + a(class = borderStyle & "group p-2 m-1 space-x-1 no-underline text-ctp-text)", + href = "/#/pkg/" & pkg.name): + span(class = textStyle & "group-hover:text-ctp-mauve font-bold font-mono-casual"): text pkg.name + span(class = "group-hover:text-ctp-mauve"): text pkg.versions[0].tag + # span: + # text " (" & (getTime() - pkg.versions[0].time).inDays.jss & " days ago)" proc randomPackage*(ctx: Context): VNode = let pkgName = ctx.nimpkgs.packages.keys().toSeq().sample() - console.log pkgName.jss result = buildHtml(tdiv(class = borderStyle & "my-2 m-1 p-2")): a(href = "/#/pkg/" & pkgName.jss, class = "flex items-center text-ctp-text no-underline"): tdiv(class = "i-mdi-dice-6") diff --git a/src/components/tag.nim b/src/components/tag.nim index efd850b..90e71c4 100644 --- a/src/components/tag.nim +++ b/src/components/tag.nim @@ -36,7 +36,7 @@ proc selectRandomTags*(ctx: Context): seq[kstring] = if tag notin result: result.add tag -proc randomTags*(ctx: Context): VNode = +proc randomTags*(): VNode = let tags = ctx.selectRandomTags() buildHtml(tdiv): tags.renderTags diff --git a/src/context.nim b/src/context.nim index 2a88bfe..9d16355 100644 --- a/src/context.nim +++ b/src/context.nim @@ -1,10 +1,7 @@ import std/[ asyncjs, jsconsole, jsfetch, sequtils, sugar, tables ] - -import karax/[kbase, karax] -import jsony - +import karax/[kbase, karax], jsony import packages, utils export tables @@ -36,3 +33,17 @@ discard ctx.fetchPackages proc nimpkgsList*(): seq[NimPackage] {.inline.} = ctx.nimpkgs.packages.values.toSeq() + +proc recentPackagesList*(): seq[NimPackage] {.inline.} = + ctx.nimpkgs.recent.mapIt(ctx.nimpkgs.packages[$it]) + +proc getRecentReleases*(): seq[NimPackage] = + var pkgs: seq[NimPackage] + for pkg in ctx.nimpkgs.packages.values(): + if pkg.versions.len > 0: + pkgs.add pkg + + pkgs.sort(sortVersion, order = Descending) + return pkgs[0..10] + + diff --git a/src/packages.nim b/src/packages.nim index 09cd9cf..41edd97 100644 --- a/src/packages.nim +++ b/src/packages.nim @@ -29,6 +29,7 @@ type NimPkgs* = object updated*: Time + recent*: seq[kstring] packagesHash*: kstring packages*: OrderedTable[string, NimPackage] diff --git a/src/pages/index.nim b/src/pages/index.nim index 63b40b2..0481396 100644 --- a/src/pages/index.nim +++ b/src/pages/index.nim @@ -1,7 +1,6 @@ import karax/[karaxdsl, vdom] import ../components/[search, tag, package] -import ../context proc render*(): VNode = result = buildHtml(tdiv(class = "justify-center")): @@ -14,9 +13,13 @@ proc render*(): VNode = tdiv(): tdiv(): text "explore tags:" - ctx.randomTags() + randomTags() + tdiv(): + tdiv(): + text "recently added packages" + recentAddedPackagesList() tdiv(): tdiv(): text "recently released versions:" - ctx.recentPackageVersionList + recentPackageVersionsList()