refactor: consolidate shared application helpers

This commit is contained in:
2026-07-29 12:28:51 +07:00 Unverified
parent 1fb154b465
commit 6b0440602b
14 changed files with 45 additions and 45 deletions
-16
View File
@@ -1,16 +0,0 @@
"use client";
type FlushSave = () => Promise<void>;
const pendingSaves = new Set<FlushSave>();
export function registerPendingSave(flush: FlushSave) {
pendingSaves.add(flush);
return () => {
pendingSaves.delete(flush);
};
}
export async function flushPendingSaves() {
await Promise.all([...pendingSaves].map((flush) => flush()));
}
+1 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { cdnImageResponse } from "@/lib/cdn-response";
import {
cdnImageResponse,
MAX_FORM_ANSWER_IMAGE_BYTES,
validateImageUploadFile,
} from "@/lib/cdn-images";
+17
View File
@@ -27,3 +27,20 @@ export function validateImageUploadFile(file: Pick<File, "name" | "type" | "size
export function cdnUrl(id: string | null | undefined) {
return id ? `/cdn/${id}` : "";
}
export type CdnResponseRecord = {
data: Buffer;
type: string;
size: number;
};
export function cdnImageResponse(record: CdnResponseRecord) {
return new Response(new Uint8Array(record.data), {
headers: {
"Content-Type": record.type,
"Content-Length": String(record.size),
"Cache-Control": "public, max-age=31536000, immutable",
"X-Content-Type-Options": "nosniff",
},
});
}
-16
View File
@@ -1,16 +0,0 @@
export type CdnResponseRecord = {
data: Buffer;
type: string;
size: number;
};
export function cdnImageResponse(record: CdnResponseRecord) {
return new Response(new Uint8Array(record.data), {
headers: {
"Content-Type": record.type,
"Content-Length": String(record.size),
"Cache-Control": "public, max-age=31536000, immutable",
"X-Content-Type-Options": "nosniff",
},
});
}
-3
View File
@@ -1,3 +0,0 @@
export function getErrorMessage(error: unknown, fallback: string) {
return error instanceof Error ? error.message : fallback;
}
+5 -1
View File
@@ -5,6 +5,10 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function getErrorMessage(error: unknown, fallback: string) {
return error instanceof Error ? error.message : fallback;
}
export const stripHtml = (html: string) => {
return (html || "").replace(/<[^>]*>?/gm, '').trim();
};
@@ -17,4 +21,4 @@ export const copyToClipboard = async (text: string): Promise<boolean> => {
console.error("Copy error:", error);
return false;
}
};
};