Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Say just now instead of 1h ago two seconds after saving

Bittia49d96072d65
diff --git a/src/App.test.tsx b/src/App.test.tsx
index ee862ea..6d7d116 100644
--- a/src/App.test.tsx
+++ b/src/App.test.tsx
@@ -3,8 +3,37 @@
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import App from "./App";
+import { ProjectCard, relativeDate } from "./components/ProjectCard";
import { createProjectConfig } from "./lib/project";
+describe("how long ago a project was edited", () => {
+ afterEach(cleanup);
+ const secondsAgo = (seconds: number) => new Date(Date.now() - seconds * 1000).toISOString();
+
+ it("says what is true, including inside the first hour", () => {
+ // N8: this used to floor at an hour, so a project saved two seconds ago was
+ // labelled "Edited 1h ago" — the card's one claim about the project's life,
+ // wrong for the whole first hour of it.
+ expect(relativeDate(secondsAgo(2))).toBe("just now");
+ expect(relativeDate(secondsAgo(59))).toBe("just now");
+ expect(relativeDate(secondsAgo(90))).toBe("1 min ago");
+ expect(relativeDate(secondsAgo(59 * 60))).toBe("59 min ago");
+ expect(relativeDate(secondsAgo(60 * 60))).toBe("1h ago");
+ // Elapsed time, not the nearest label: 1h59m is not two hours yet.
+ expect(relativeDate(secondsAgo(119 * 60))).toBe("1h ago");
+ expect(relativeDate(secondsAgo(26 * 3_600))).toBe("1d ago");
+ // A clock that has moved backwards since the save is not a project edited
+ // in the future.
+ expect(relativeDate(secondsAgo(-30))).toBe("just now");
+ });
+
+ it("prints a just-saved project as just saved", () => {
+ const config = createProjectConfig({ name: "Ceramic lamp", prompt: "A quiet product film", aspectRatio: "16:9", resolution: "1080p", targetDurationSeconds: 30 });
+ const { container } = render(<ProjectCard project={{ folderPath: "C:\\Ceramic Lamp", config }} index={0} onOpen={() => undefined} />);
+ expect(container.querySelector(".project-card__meta")!.textContent).toContain("Edited just now");
+ });
+});
+
describe("project library controls", () => {
beforeEach(() => localStorage.clear());
afterEach(cleanup);
diff --git a/src/components/ProjectCard.tsx b/src/components/ProjectCard.tsx
index 1effcea..70b27e0 100644
--- a/src/components/ProjectCard.tsx
+++ b/src/components/ProjectCard.tsx
@@ -10,10 +10,20 @@ interface ProjectCardProps {
const artwork = ["aurora", "paper", "chrome", "ember"];
-function relativeDate(iso: string) {
- const hours = Math.max(1, Math.round((Date.now() - new Date(iso).getTime()) / 3_600_000));
+/* How long ago, said honestly. This floored everything at an hour, so a project
+ saved two seconds ago was labelled "Edited 1h ago" — the card's only claim
+ about the project's life, and it was false for the whole first hour. Rounding
+ is out for the same reason: 1h59m is not two hours yet. Everything below is
+ how much time has definitely passed. A clock that has moved backwards since
+ the save (a machine resyncing, a file from another timezone) leaves a
+ negative age, which is no age at all and reads as just now. */
+export function relativeDate(iso: string, now = Date.now()) {
+ const minutes = Math.floor((now - new Date(iso).getTime()) / 60_000);
+ if (minutes < 1) return "just now";
+ if (minutes < 60) return `${minutes} min ago`;
+ const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h ago`;
- const days = Math.round(hours / 24);
+ const days = Math.floor(hours / 24);
if (days < 7) return `${days}d ago`;
return new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric" }).format(new Date(iso));
}
diff --git a/src/components/ProjectWorkspace.test.ts b/src/components/ProjectWorkspace.test.ts
index 09cd165..2ee44ed 100644
--- a/src/components/ProjectWorkspace.test.ts
+++ b/src/components/ProjectWorkspace.test.ts
@@ -4,7 +4,7 @@ import { act, cleanup, createEvent, fireEvent, render, screen, waitFor, within }
import { createElement, useEffect, useState } from "react";
import { afterEach, describe, expect, it, vi } from "vitest";
import completeFixture from "../../fixtures/project-v1-complete.json";
-import { compileMiniMaxH3Prompt, createProjectConfig, parseProjectConfig, STORY_TRACK_ID, type ProjectAsset, type ProjectConfig, type TimelineClip } from "../lib/project";
+import { compileMiniMaxH3Prompt, createProjectConfig, parseProjectConfig, STORY_TRACK_ID, type ProjectAsset, type ProjectConfig, type ProjectRecord, type TimelineClip } from "../lib/project";
import { formatDurationTimecode } from "./ProjectWorkspace";
import { ProjectWorkspace } from "./ProjectWorkspace";
import { GeneratorView } from "./workspace/GeneratorView";
@@ -508,7 +508,7 @@ describe("project workspace timecode", () => {
nobody had touched — "Save your changes to this project folder" over a
folder that had none. A measurement is still worth keeping, so it stays
in the project and rides along with the next real save. */
- const onSave = vi.fn(async () => undefined);
+ const onSave = vi.fn(async (_record: ProjectRecord) => undefined);
const project = { folderPath: "C:\\Looked At", config: projectWithMedia() };
await withFakeDecoder({ seconds: 40, width: 1920, height: 1080 }, async () => {
render(createElement(ProjectWorkspace, { project, onBack: () => undefined, onSave }));
@@ -529,7 +529,7 @@ describe("project workspace timecode", () => {
fireEvent.click(save());
await waitFor(() => expect(onSave).toHaveBeenCalled());
});
- const saved = onSave.mock.calls[0][0] as unknown as { config: ProjectConfig };
+ const saved = onSave.mock.calls[0][0];
expect(saved.config.assets.map((asset) => asset.durationMs)).toEqual([40_000, 40_000]);
expect(parseProjectConfig(saved.config)).toBeTruthy();
}, 20_000);