- Hooks: per-instance timers in useReady; cleanup for useIntersectionObserver; useWebSocket no longer reconnects after unmount/refresh, drains the queue and dispatches non-JSON messages - Modal/Popover/Dropdown: onClose only on close, Escape to close, no stray DOM props, hooks before early return, scrollbar-aware positioning, top-left/top-right dropdown positions - Elements: Toast/Search/StarRating per-instance timers, Pagination labels and empty state, Breadcrumbs titles, LinkList dividers/props, segment-wise active-link matching (new utils/link-matches-path), Table object cells - Form: Select with a default option is changeable, Input clear notifies controlled parents and copy works on textareas, Checkbox random label, NumberInputButtons runaway interval, upload prop/spread fixes - Layout: Button disabled while loading, Stack gap and Span truncate via inline styles, Img fallbackImageSrc, className/JSDoc fixes - Editors/MDX/AI: TinyMCE id/cleanup/stale handler/duplicate scripts, AceEditor debounce, markdown heights and frontmatter regex, last assistant message, AIPromptBlock prompt ref - Utils: fetchApi headers/FormData/fetchOptions/no mutation, base64 file names and read errors, slugify underscore trim, serializeQuery zero values - Tests for fetchApi, link matching, slugify and serializeQuery - AGENTS.md updated for the package layout Co-Authored-By: Claude Opus 5.5 <[email protected]>
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
export type FileInputToBase64FunctionReturn = {
|
|
fileBase64?: string;
|
|
fileBase64Full?: string;
|
|
fileName?: string;
|
|
fileSize?: number;
|
|
fileType?: string;
|
|
file?: File;
|
|
};
|
|
|
|
export type FileInputToBase64FunctioParam = {
|
|
inputFile: File;
|
|
allowedRegex?: RegExp;
|
|
};
|
|
|
|
export default async function fileInputToBase64({
|
|
inputFile,
|
|
allowedRegex,
|
|
}: FileInputToBase64FunctioParam): Promise<FileInputToBase64FunctionReturn> {
|
|
const allowedTypesRegex = allowedRegex ? allowedRegex : undefined;
|
|
|
|
if (allowedTypesRegex && !inputFile?.type?.match(allowedTypesRegex)) {
|
|
window.alert(`We currently don't support ${inputFile.type} file type.`);
|
|
return { fileName: inputFile.name };
|
|
}
|
|
|
|
let fileName = inputFile.name?.replace(/\.[^.]*$/, "");
|
|
const file = inputFile;
|
|
|
|
try {
|
|
const fileData: string | undefined = await new Promise(
|
|
(resolve, reject) => {
|
|
var reader = new FileReader();
|
|
reader.readAsDataURL(inputFile);
|
|
reader.onload = function () {
|
|
resolve(reader.result?.toString());
|
|
};
|
|
reader.onerror = function () {
|
|
reject(reader.error);
|
|
};
|
|
}
|
|
);
|
|
|
|
return {
|
|
fileBase64: fileData?.replace(/.*?base64,/, ""),
|
|
fileBase64Full: fileData,
|
|
fileName: fileName,
|
|
fileSize: inputFile.size,
|
|
fileType: inputFile.type,
|
|
file,
|
|
};
|
|
} catch (error: any) {
|
|
console.log("File Processing Error! =>", error.message);
|
|
|
|
return {
|
|
fileName: inputFile.name,
|
|
file,
|
|
};
|
|
}
|
|
}
|