Skip to content

Commit

Permalink
chore: update ulid.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ynwd committed Nov 2, 2024
1 parent bb00780 commit 4d34edd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 81 deletions.
55 changes: 55 additions & 0 deletions modules/index/index.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Context, HttpRequest } from "@app/mod.ts";
import { getSession } from "@app/utils/session.ts";

function init() {
const basePath = Deno.env.get("DENO_DEPLOYMENT_ID")
? `https://raw.githubusercontent.com/fastrodev/fastro/main/static`
: "http://localhost:8000/static";
const code =
`import init from "${basePath}/init.ts"; const name = Deno.args[0] ?? 'project'; await init(name);`;
return new Response(code, {
headers: {
"content-type": "application/typescript; charset=utf-8",
},
});
}

function denoRunCheck(req: HttpRequest) {
const regex = /^Deno\/(\d+\.\d+\.\d+)$/;
const string = req.headers.get("user-agent");
if (!string) return false;
const match = regex.exec(string);
if (!match) return false;
return true;
}

export const handler = async (req: HttpRequest, ctx: Context) => {
const res = denoRunCheck(req);
if (res) return init();
const ses = await getSession(req, ctx);
const title = ses?.isLogin
? "Under construction ~ Home"
: "Fast & Modular Web Framework";
const ws_url = Deno.env.get("DENO_DEPLOYMENT_ID")
? "wss://fastro.dev"
: "ws://localhost:8000";
return ctx.render({
title,
description:
"Enhance SSR web app maintainability through a flat modular architecture",
image: "https://fastro.dev/fastro.png",
start: Deno.env.get("ENV") === "DEVELOPMENT"
? "http://localhost:8000/docs/start"
: "https://fastro.dev/docs/start",
baseUrl: Deno.env.get("ENV") === "DEVELOPMENT"
? "http://localhost:8000"
: "https://fastro.dev",
new: "Using Queues to Avoid Race Conditions",
destination: "blog/queue",
isLogin: ses?.isLogin,
avatar_url: ses?.avatar_url,
html_url: ses?.html_url,
ws_url,
username: ses?.username,
});
};
6 changes: 3 additions & 3 deletions modules/index/index.message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export function Message(
class={`bg-gray-900 ps-3 pt-2 pe-3 pb-2 border border-gray-800 rounded-lg flex flex-col gap-1`}
>
<div
class={`flex items-center justify-between gap-3 text-gray-500`}
class={`flex items-center justify-between gap-3 text-gray-500 font-light text-xs`}
>
<div class={`inline-flex items-center gap-x-1`}>
<span class={"grow text-xs font-thin text-gray-200"}>
<span class={"grow"}>
{props.username}
</span>
<span class={"grow text-xs font-thin"}>
<span class={"grow"}>
{props.time}
</span>
</div>
Expand Down
58 changes: 3 additions & 55 deletions modules/index/mod.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,14 @@
import { Fastro, HttpRequest } from "@app/mod.ts";
import { Fastro } from "@app/mod.ts";
import indexApp from "@app/modules/index/index.page.tsx";
import index from "@app/modules/index/index.layout.tsx";
import { getSession } from "@app/utils/session.ts";

function init() {
const basePath = Deno.env.get("DENO_DEPLOYMENT_ID")
? `https://raw.githubusercontent.com/fastrodev/fastro/main/static`
: "http://localhost:8000/static";
const code =
`import init from "${basePath}/init.ts"; const name = Deno.args[0] ?? 'project'; await init(name);`;
return new Response(code, {
headers: {
"content-type": "application/typescript; charset=utf-8",
},
});
}

function denoRunCheck(req: HttpRequest) {
const regex = /^Deno\/(\d+\.\d+\.\d+)$/;
const string = req.headers.get("user-agent");
if (!string) return false;
const match = regex.exec(string);
if (!match) return false;
return true;
}
import { handler } from "@app/modules/index/index.handler.ts";

export default function (s: Fastro) {
const ws_url = Deno.env.get("DENO_DEPLOYMENT_ID")
? "wss://fastro.dev"
: "ws://localhost:8000";
/** setup SSR */
s.page("/", {
component: indexApp,
layout: index,
folder: "modules/index",
handler: async (req, ctx) => {
const res = denoRunCheck(req);
if (res) return init();
const ses = await getSession(req, ctx);
const title = ses?.isLogin
? "Under construction ~ Home"
: "Fast & Modular Web Framework";
return ctx.render({
title,
description:
"Enhance SSR web app maintainability through a flat modular architecture",
image: "https://fastro.dev/fastro.png",
start: Deno.env.get("ENV") === "DEVELOPMENT"
? "http://localhost:8000/docs/start"
: "https://fastro.dev/docs/start",
baseUrl: Deno.env.get("ENV") === "DEVELOPMENT"
? "http://localhost:8000"
: "https://fastro.dev",
new: "Using Queues to Avoid Race Conditions",
destination: "blog/queue",
isLogin: ses?.isLogin,
avatar_url: ses?.avatar_url,
html_url: ses?.html_url,
ws_url,
username: ses?.username,
});
},
handler,
});
return s;
}
51 changes: 28 additions & 23 deletions utils/ulid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dayjs from "npm:dayjs";

export function ulidToDate(ulid: string) {
// Base32 character set
const base32Chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
Expand All @@ -20,30 +22,33 @@ export function ulidToDate(ulid: string) {
}

export function formatTime(isoDateString: string): string {
const date = new Date(isoDateString);
const now = new Date();

// Get local date components
const localYear = date.getFullYear();
const localMonth = String(date.getMonth() + 1).padStart(2, "0"); // Months are 0-indexed
const localDay = String(date.getDate()).padStart(2, "0");
const localHours = date.getHours();
const localMinutes = String(date.getMinutes()).padStart(2, "0");

// Check if the date is now
if (
date.toDateString() === now.toDateString() &&
date.getTime() === now.getTime()
) {
const formattedHours = localHours % 12 || 12; // Convert to 12-hour format
const amPm = localHours < 12 ? "AM" : "PM";
return `— Now at ${formattedHours}:${localMinutes} ${amPm}`;
const date = dayjs(isoDateString);
const now = dayjs();

// Check if the time difference is less than 1 minute
if (now.diff(date, "minute") < 1) {
return "• now";
}

// Check if the time difference is less than 1 hour
if (now.diff(date, "hour") < 1) {
const minutesAgo = now.diff(date, "minute");
return `• ${minutesAgo} minute${minutesAgo !== 1 ? "s" : ""} ago`;
}

// Format hours for 12-hour clock
const formattedHours = localHours % 12 || 12; // Convert to 12-hour format
const amPm = localHours < 12 ? "AM" : "PM";
// Check if the time difference is less than 1 day
if (now.diff(date, "day") < 1) {
const hoursAgo = now.diff(date, "hour");
return `• ${hoursAgo} hour${hoursAgo !== 1 ? "s" : ""} ago`;
}

// Check if the time difference is less than 1 year
if (now.diff(date, "year") < 1) {
const monthsAgo = now.diff(date, "month");
return `• ${monthsAgo} month${monthsAgo !== 1 ? "s" : ""} ago`;
}

// Construct the desired format
return `— ${localMonth}/${localDay}/${localYear} ${formattedHours}:${localMinutes} ${amPm}`;
// Format for dates older than a day
const formattedDate = date.format("MM/DD/YYYY hh:mm A");
return `• ${formattedDate}`;
}

0 comments on commit 4d34edd

Please sign in to comment.