Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.
TypeScript fintechinvoiceinvoice-generatorinvoice-pdfinvoice-templateinvoicesnextjsopen-sourcequotesreactreact-pdf-rendererself-hostedshadcn-uitailwindcsstypescript
import { readdir } from "node:fs/promises";
import { join } from "node:path";
import { z } from "zod";
import { GITHUB_URL } from "@/config";
export interface ChangelogSummary {
slug: string;
title: string;
description: string;
version?: string;
date: string;
/** A few words about the release for the "What's new" popup, see `popupSummary` below. */
popupSummary?: string;
/** YouTube video for the release, see `youtubeVideoId` below. */
youtubeVideoId?: string;
}
/** Maps metadata `version` (e.g. "1.0.1") to the GitHub release tag name. */
const changelogVersionToReleaseTag: Record<string, string> = {
"1.0.0": "EasyInvoicePDF-v1.0.0",
"1.0.1": "EasyInvoicePDF-1.0.1",
"1.0.2": "v1.0.2",
"1.0.3": "v1.0.3",
"1.0.4": "v1.0.4",
};
/**
* GitHub release URL for changelog entries. Unknown versions default to `v{version}`.
*/
export function getChangelogReleaseNotesUrl(version: string) {
const tag = changelogVersionToReleaseTag[version];
if (!tag) {
return null;
}
return `${GITHUB_URL}/releases/tag/${tag}`;
}
interface ChangelogMetadata {
title: string;
description: string;
date: string;
version?: string;
type?: "major" | "minor" | "patch";
slug?: string;
/**
* A few words about the release, shown in the "What's new" popup in place of its generic
* line. The popup is small and shares its space with the mascot, so keep it to a short
* sentence; leave it out and the popup falls back to the generic line.
*/
popupSummary?: string;
/**
* ID of the release's YouTube video (the `v=` part of its URL). The "What's new" popup
* shows its thumbnail, linking to the video; leave it out and the popup has no preview.
* Upload at 720p or higher: the popup uses YouTube's `maxresdefault` thumbnail on 2x
* screens, which YouTube only makes for HD videos.
*/
youtubeVideoId?: string;
}
export interface ChangelogEntry {
slug: string;
metadata: ChangelogMetadata;
Component: React.ComponentType;
}
// Validate metadata with Zod schema
const changelogEntryMetadataSchema = z.object({
title: z.string().min(1, "Title is required"),
description: z.string().min(1, "Description is required"),
date: z.iso.date(),
version: z.string().optional(),
type: z.enum(["major", "minor", "patch"]).optional(),
popupSummary: z.string().min(1).max(80).optional(),
youtubeVideoId: z
.string()
.regex(/^[\w-]{11}$/, "Expected an 11-character YouTube video ID")
.optional(),
});
/**
* Returns the newest changelog entry metadata without the MDX component.
*/
export async function getLatestChangelogSummary(): Promise<ChangelogSummary | null> {
const [latest] = await getChangelogEntries();
if (!latest) {
return null;
}
const { slug, metadata } = latest;
return {
slug,
title: metadata.title,
description: metadata.description,
version: metadata.version,
date: metadata.date,
popupSummary: metadata.popupSummary,
youtubeVideoId: metadata.youtubeVideoId,
};
}
/**
* Gets all MDX files from the content/changelog directory
*/
async function getChangelogFiles(): Promise<string[]> {
try {
const changelogDir = join(
process.cwd(),
"src",
"app",
"(main)",
"changelog",
"content",
);
const files = await readdir(changelogDir);
return files.filter((file) => {
return file.endsWith(".mdx");
});
} catch (error) {
console.error("Failed to read changelog directory:", error);
return [];
}
}
/**
* Converts filename to slug (removes .mdx extension)
*/
function filenameToSlug(filename: string): string {
return filename.replace(".mdx", "");
}
/**
* Dynamically imports a changelog MDX file
*/
async function importChangelogFile(filename: string) {
const slug = filenameToSlug(filename);
try {
const dynamicModule = (await import(`./content/${filename}`)) as {
metadata: ChangelogMetadata;
default: React.ComponentType;
};
// Validate metadata and throw error if invalid
try {
changelogEntryMetadataSchema.parse(dynamicModule.metadata);
} catch (error) {
if (error instanceof z.ZodError) {
console.error(
`Invalid metadata in changelog file ${filename}:`,
error.issues,
);
return null;
}
throw error;
}
return {
slug,
module: {
metadata: dynamicModule.metadata,
default: dynamicModule.default,
},
};
} catch (error) {
console.error(`Failed to import changelog file ${filename}:`, error);
return null;
}
}
/**
* Loads all changelog entries and returns them sorted by date (newest first)
*/
export async function getChangelogEntries(): Promise<ChangelogEntry[]> {
const files = await getChangelogFiles();
const entries: ChangelogEntry[] = [];
for (const filename of files) {
const imported = await importChangelogFile(filename);
if (!imported) {
throw new Error(
`\n\n __________ ERROR: Failed to import changelog file ${filename} __________\n\n`,
);
}
const { slug, module } = imported;
const { metadata, default: Component } = module;
entries.push({
slug,
metadata: {
...metadata,
slug, // Ensure slug is always available in metadata
},
Component,
});
}
// Sort by date (newest first)
return entries.sort((a, b) => {
return (
new Date(b.metadata.date).getTime() - new Date(a.metadata.date).getTime()
);
});
}
/**
* Gets a specific changelog entry by slug
*/
export async function getChangelogEntry(
slug: string,
): Promise<ChangelogEntry | null> {
const files = await getChangelogFiles();
const filename = files.find((file) => {
return filenameToSlug(file) === slug;
});
if (!filename) {
return null;
}
const imported = await importChangelogFile(filename);
if (!imported) {
return null;
}
const { module } = imported;
const { metadata, default: Component } = module;
return {
slug,
metadata: {
...metadata,
slug, // Ensure slug is always available in metadata
},
Component,
};
}
/**
* Formats a date for display
*/
export function formatChangelogDate(date: string): string {
return new Date(date).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
}
/**
* Gets the next changelog entry after the current one (based on date order)
*/
export async function getNextChangelogEntry(
currentSlug: string,
): Promise<ChangelogEntry | null> {
const allEntries = await getChangelogEntries();
const currentIndex = allEntries.findIndex((entry) => {
return entry.slug === currentSlug;
});
// If current entry is not found or is the first one (newest), return null
if (currentIndex === -1 || currentIndex === 0) {
return null;
}
// Return the previous entry (newer entry since they're sorted newest first)
return allEntries[currentIndex - 1];
}
/**
* Gets the previous changelog entry before the current one (based on date order)
*/
export async function getPreviousChangelogEntry(
currentSlug: string,
): Promise<ChangelogEntry | null> {
const allEntries = await getChangelogEntries();
const currentIndex = allEntries.findIndex((entry) => {
return entry.slug === currentSlug;
});
// If current entry is not found or is the last one (oldest), return null
if (currentIndex === -1 || currentIndex === allEntries.length - 1) {
return null;
}
// Return the next entry (older entry since they're sorted newest first)
return allEntries[currentIndex + 1];
}