Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-18 19:13:33 +00:00
co-authored by renee-png
parent 1afb6d6c86
commit ec09bdc3f0
+17
View File
@@ -0,0 +1,17 @@
/**
* Prompts the user for a filename before download. Returns the chosen base
* name (without extension) sanitized for filesystem use, or null if cancelled.
*
* @param defaultName Suggested base name (no extension)
* @param ext Extension shown to the user, e.g. "pdf", "docx", "csv"
*/
export function promptFilename(defaultName: string, ext: string): string | null {
const sanitize = (s: string) => s.replace(/[\\/:*?"<>|]+/g, "_").trim();
const cleanDefault = sanitize(defaultName).replace(new RegExp(`\\.${ext}$`, "i"), "") || "Document";
const input = typeof window !== "undefined"
? window.prompt(`File name (.${ext})`, cleanDefault)
: cleanDefault;
if (input === null) return null; // user cancelled
const cleaned = sanitize(input).replace(new RegExp(`\\.${ext}$`, "i"), "");
return cleaned || cleanDefault;
}