Add RepoCard component

This commit is contained in:
Rūdolfs Ošiņš
2024-09-05 11:17:58 +02:00
parent 7f67759d4b
commit 3edfcf9192
13 changed files with 431 additions and 9 deletions
+8
View File
@@ -0,0 +1,8 @@
import { object, string, z } from "zod";
export type Author = z.infer<typeof authorSchema>;
export const authorSchema = object({
id: string(),
alias: string().optional(),
});
+36
View File
@@ -0,0 +1,36 @@
{
"rid": "rad:z3trNYnLWS11cJWC6BbxDs5niGo82",
"payloads": {
"xyz.radicle.project": {
"data": {
"name": "rips",
"description": "Radicle Improvement Proposals (RIPs)",
"defaultBranch": "master"
},
"meta": {
"head": "329dee9a4b65169ea3889a7da239892b705d0d68",
"patches": {
"open": 0,
"draft": 2,
"archived": 0,
"merged": 0
},
"issues": {
"open": 1,
"closed": 0
}
}
}
},
"delegates": [
{
"id": "did:key:z6MksFqXN3Yhqk8pTJdUGLwATkRfQvwZXPqR2qMEhbS9wzpT",
"alias": "cloudhead"
}
],
"threshold": 1,
"visibility": {
"type": "private"
},
"seeding": 29
}
+48
View File
@@ -0,0 +1,48 @@
import {
array,
literal,
number,
object,
optional,
string,
union,
z,
} from "zod";
import { authorSchema } from "./author";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const repoSchema = object({
rid: string(),
payloads: object({
"xyz.radicle.project": object({
data: object({
name: string(),
description: string(),
defaultBranch: string(),
}),
meta: object({
head: string(),
patches: object({
open: number(),
draft: number(),
archived: number(),
merged: number(),
}),
issues: object({
open: number(),
closed: number(),
}),
}),
}),
}),
delegates: array(authorSchema),
threshold: number(),
visibility: union([
object({ type: literal("public") }),
object({ type: literal("private"), allow: optional(array(string())) }),
]),
seeding: number(),
});
export type Repo = z.infer<typeof repoSchema>;
+72
View File
@@ -1,3 +1,75 @@
import bs58 from "bs58";
export const unreachable = (value: never): never => {
throw new Error(`Unreachable code: ${value}`);
};
export function formatRepositoryId(id: string): string {
const parsedId = parseRepositoryId(id);
if (parsedId) {
return `${parsedId.prefix}${truncateId(parsedId.pubkey)}`;
}
return id;
}
export function parseRepositoryId(
rid: string,
): { prefix: string; pubkey: string } | undefined {
const match = /^(rad:)?(z[a-zA-Z0-9]+)$/.exec(rid);
if (match) {
const hex = bs58.decode(match[2].substring(1));
if (hex.byteLength !== 20) {
return undefined;
}
return { prefix: match[1] || "rad:", pubkey: match[2] };
}
return undefined;
}
export function truncateId(pubkey: string): string {
return `${pubkey.substring(0, 6)}…${pubkey.slice(-6)}`;
}
export const formatTimestamp = (
timestamp: number,
current = new Date().getTime(),
): string => {
const units: Record<string, number> = {
year: 24 * 60 * 60 * 1000 * 365,
month: (24 * 60 * 60 * 1000 * 365) / 12,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000,
};
// Multiplying timestamp with 1000 to convert from seconds to milliseconds
timestamp = timestamp * 1000;
const rtf = new Intl.RelativeTimeFormat("en", {
numeric: "auto",
style: "long",
});
const elapsed = current - timestamp;
if (elapsed > units["year"]) {
return "more than a year ago";
} else if (elapsed < 0) {
return "now"; // If elapsed is a negative number we are dealing with an item from the future, and we return "now"
}
for (const u in units) {
if (elapsed > units[u] || u === "second") {
// We convert the division result to a negative number to get "XX [unit] ago"
return rtf.format(
Math.round(elapsed / units[u]) * -1,
u as Intl.RelativeTimeFormatUnit,
);
}
}
return new Date(timestamp).toUTCString();
};