@appcorp/fusion-storybook 0.5.36 → 0.5.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base-modules/admission/context/use-admission-module.d.ts +1 -1
- package/base-modules/admission/context/use-admission-module.js +23 -36
- package/base-modules/admission/form.js +117 -54
- package/base-modules/expense/context/shared.d.ts +5 -0
- package/base-modules/expense/context/shared.js +1 -0
- package/base-modules/expense/context/use-expense-module.d.ts +2 -1
- package/base-modules/expense/context/use-expense-module.js +49 -5
- package/base-modules/expense/form.js +4 -2
- package/base-modules/school/context.d.ts +13 -7
- package/base-modules/school/context.js +46 -5
- package/base-modules/school/form.js +11 -1
- package/base-modules/teacher/context.js +9 -37
- package/base-modules/user/context/use-user-module.d.ts +1 -0
- package/base-modules/user/context/use-user-module.js +22 -43
- package/base-modules/user/context.d.ts +5 -0
- package/base-modules/user/context.js +1 -0
- package/base-modules/workspace/context.d.ts +7 -1
- package/base-modules/workspace/context.js +46 -5
- package/base-modules/workspace/form.js +11 -1
- package/constants.d.ts +0 -4
- package/constants.js +0 -4
- package/package.json +2 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/utils/upload-file.d.ts +42 -0
- package/utils/upload-file.js +116 -0
- package/base-modules/teacher/avatar-upload.d.ts +0 -36
- package/base-modules/teacher/avatar-upload.js +0 -104
- package/utils/file-to-base64.d.ts +0 -1
- package/utils/file-to-base64.js +0 -10
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forms that can own uploaded files. The value is used as the second path
|
|
3
|
+
* segment: `<workspaceId>/<formName>/<uuid>.<ext>`.
|
|
4
|
+
*/
|
|
5
|
+
export type UploadFormName = "admission" | "expense" | "teacher" | "user" | "school" | "workspace";
|
|
6
|
+
export interface UploadFileOptions {
|
|
7
|
+
workspaceId: string;
|
|
8
|
+
formName: UploadFormName;
|
|
9
|
+
maxSizeBytes?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface UploadedFile {
|
|
12
|
+
/** Public Supabase URL to store in the database. */
|
|
13
|
+
url: string;
|
|
14
|
+
/** Object key inside the bucket. */
|
|
15
|
+
path: string;
|
|
16
|
+
}
|
|
17
|
+
export type UploadErrorCode = "config" | "workspace" | "size" | "type" | "conversion" | "upload";
|
|
18
|
+
export declare class UploadError extends Error {
|
|
19
|
+
readonly code: UploadErrorCode;
|
|
20
|
+
constructor(message: string, code: UploadErrorCode);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Convert an image to WebP before upload. Non-image files are returned as-is.
|
|
24
|
+
*/
|
|
25
|
+
export declare const prepareFileForUpload: (file: File) => Promise<{
|
|
26
|
+
blob: Blob;
|
|
27
|
+
contentType: string;
|
|
28
|
+
extension: string;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Upload one file directly from the browser to the public Supabase bucket.
|
|
32
|
+
*
|
|
33
|
+
* Object key: `<workspaceId>/<formName>/<uuid>.<ext>`.
|
|
34
|
+
* Images are always converted to `.webp` first.
|
|
35
|
+
*/
|
|
36
|
+
export declare const uploadFile: (file: File, options: UploadFileOptions) => Promise<UploadedFile>;
|
|
37
|
+
/**
|
|
38
|
+
* Upload several files in parallel. Preserves input order.
|
|
39
|
+
*/
|
|
40
|
+
export declare const uploadFiles: (files: File[], options: UploadFileOptions) => Promise<UploadedFile[]>;
|
|
41
|
+
/** Test-only helper to reset the memoized browser client. */
|
|
42
|
+
export declare const resetUploadClientForTests: () => void;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createBrowserClient } from "@supabase/ssr";
|
|
3
|
+
import { blobToWebP } from "webp-converter-browser";
|
|
4
|
+
import { supabasePublicStorageUrl } from "../constants";
|
|
5
|
+
export class UploadError extends Error {
|
|
6
|
+
constructor(message, code) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "UploadError";
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const EXTENSION_BY_MIME = {
|
|
13
|
+
"application/pdf": "pdf",
|
|
14
|
+
"application/msword": "doc",
|
|
15
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
16
|
+
"application/vnd.ms-excel": "xls",
|
|
17
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
18
|
+
"text/csv": "csv",
|
|
19
|
+
"text/plain": "txt",
|
|
20
|
+
"image/jpeg": "jpg",
|
|
21
|
+
"image/png": "png",
|
|
22
|
+
"image/webp": "webp",
|
|
23
|
+
"image/gif": "gif",
|
|
24
|
+
"image/bmp": "bmp",
|
|
25
|
+
};
|
|
26
|
+
const IMAGE_MIME_TYPES = new Set([
|
|
27
|
+
"image/jpeg",
|
|
28
|
+
"image/png",
|
|
29
|
+
"image/webp",
|
|
30
|
+
"image/gif",
|
|
31
|
+
"image/bmp",
|
|
32
|
+
]);
|
|
33
|
+
const isImage = (file) => IMAGE_MIME_TYPES.has(file.type);
|
|
34
|
+
const extensionFor = (file) => {
|
|
35
|
+
var _a;
|
|
36
|
+
const fromMime = EXTENSION_BY_MIME[file.type];
|
|
37
|
+
if (fromMime)
|
|
38
|
+
return fromMime;
|
|
39
|
+
const match = /\.([a-z0-9]+)$/i.exec(file.name);
|
|
40
|
+
return ((_a = match === null || match === void 0 ? void 0 : match[1]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || "bin";
|
|
41
|
+
};
|
|
42
|
+
let browserClient = null;
|
|
43
|
+
const getSupabaseClient = () => {
|
|
44
|
+
if (browserClient)
|
|
45
|
+
return browserClient;
|
|
46
|
+
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
|
47
|
+
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
|
48
|
+
if (!url || !anonKey) {
|
|
49
|
+
throw new UploadError("Supabase storage is not configured (missing URL or anon key)", "config");
|
|
50
|
+
}
|
|
51
|
+
browserClient = createBrowserClient(url, anonKey);
|
|
52
|
+
return browserClient;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Convert an image to WebP before upload. Non-image files are returned as-is.
|
|
56
|
+
*/
|
|
57
|
+
export const prepareFileForUpload = async (file) => {
|
|
58
|
+
if (!isImage(file)) {
|
|
59
|
+
return {
|
|
60
|
+
blob: file,
|
|
61
|
+
contentType: file.type || "application/octet-stream",
|
|
62
|
+
extension: extensionFor(file),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
const webp = await blobToWebP(file, { quality: 0.8 });
|
|
67
|
+
return { blob: webp, contentType: "image/webp", extension: "webp" };
|
|
68
|
+
}
|
|
69
|
+
catch (_a) {
|
|
70
|
+
throw new UploadError("Failed to convert image to WebP", "conversion");
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Upload one file directly from the browser to the public Supabase bucket.
|
|
75
|
+
*
|
|
76
|
+
* Object key: `<workspaceId>/<formName>/<uuid>.<ext>`.
|
|
77
|
+
* Images are always converted to `.webp` first.
|
|
78
|
+
*/
|
|
79
|
+
export const uploadFile = async (file, options) => {
|
|
80
|
+
if (!options.workspaceId) {
|
|
81
|
+
throw new UploadError("Workspace is not available", "workspace");
|
|
82
|
+
}
|
|
83
|
+
if (!file || file.size === 0) {
|
|
84
|
+
throw new UploadError("File is empty", "type");
|
|
85
|
+
}
|
|
86
|
+
if (options.maxSizeBytes && file.size > options.maxSizeBytes) {
|
|
87
|
+
throw new UploadError(`File exceeds the ${Math.round(options.maxSizeBytes / 1024)} KB limit`, "size");
|
|
88
|
+
}
|
|
89
|
+
const bucket = process.env.NEXT_PUBLIC_SUPABASE_PUBLIC_BUCKET;
|
|
90
|
+
if (!bucket) {
|
|
91
|
+
throw new UploadError("Supabase storage bucket is not configured", "config");
|
|
92
|
+
}
|
|
93
|
+
const { blob, contentType, extension } = await prepareFileForUpload(file);
|
|
94
|
+
const path = `${options.workspaceId}/${options.formName}/${crypto.randomUUID()}.${extension}`;
|
|
95
|
+
const supabase = getSupabaseClient();
|
|
96
|
+
const { error } = await supabase.storage.from(bucket).upload(path, blob, {
|
|
97
|
+
cacheControl: process.env.NEXT_PUBLIC_SUPABASE_CACHE_CONTROL || "3600",
|
|
98
|
+
contentType,
|
|
99
|
+
upsert: false,
|
|
100
|
+
});
|
|
101
|
+
if (error) {
|
|
102
|
+
throw new UploadError(error.message || "Upload failed", "upload");
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
path,
|
|
106
|
+
url: `${supabasePublicStorageUrl}/${path}`,
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Upload several files in parallel. Preserves input order.
|
|
111
|
+
*/
|
|
112
|
+
export const uploadFiles = async (files, options) => Promise.all(files.map((file) => uploadFile(file, options)));
|
|
113
|
+
/** Test-only helper to reset the memoized browser client. */
|
|
114
|
+
export const resetUploadClientForTests = () => {
|
|
115
|
+
browserClient = null;
|
|
116
|
+
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Teacher avatar upload utility
|
|
3
|
-
* Handles client-side file validation, WebP conversion, and upload orchestration
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Validation error type
|
|
7
|
-
*/
|
|
8
|
-
export interface AvatarUploadError {
|
|
9
|
-
type: "size" | "format" | "conversion" | "unknown";
|
|
10
|
-
message: string;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Result of avatar upload process
|
|
14
|
-
*/
|
|
15
|
-
export interface AvatarUploadResult {
|
|
16
|
-
success: true;
|
|
17
|
-
fileBase64: string;
|
|
18
|
-
contentType: "image/webp";
|
|
19
|
-
originalFileName: string;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Validate file size and format
|
|
23
|
-
*/
|
|
24
|
-
export declare function validateAvatarFile(file: File): AvatarUploadError | null;
|
|
25
|
-
/**
|
|
26
|
-
* Convert image file to WebP format
|
|
27
|
-
*/
|
|
28
|
-
export declare function convertToWebPFormat(file: File): Promise<Blob | AvatarUploadError>;
|
|
29
|
-
/**
|
|
30
|
-
* Convert blob to base64 string
|
|
31
|
-
*/
|
|
32
|
-
export declare function blobToBase64(blob: Blob): Promise<string>;
|
|
33
|
-
/**
|
|
34
|
-
* Process avatar file: validate, convert to WebP, return base64
|
|
35
|
-
*/
|
|
36
|
-
export declare function processAvatarFile(file: File): Promise<AvatarUploadResult | AvatarUploadError>;
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Teacher avatar upload utility
|
|
3
|
-
* Handles client-side file validation, WebP conversion, and upload orchestration
|
|
4
|
-
*/
|
|
5
|
-
import { blobToWebP } from "webp-converter-browser";
|
|
6
|
-
// Constants
|
|
7
|
-
const MAX_FILE_SIZE = 100 * 1024; // 100KB in bytes
|
|
8
|
-
const ALLOWED_MIME_TYPES = [
|
|
9
|
-
"image/webp",
|
|
10
|
-
"image/png",
|
|
11
|
-
"image/jpeg",
|
|
12
|
-
"image/jpg",
|
|
13
|
-
"image/gif",
|
|
14
|
-
"image/bmp",
|
|
15
|
-
];
|
|
16
|
-
/**
|
|
17
|
-
* Validate file size and format
|
|
18
|
-
*/
|
|
19
|
-
export function validateAvatarFile(file) {
|
|
20
|
-
// Check file size
|
|
21
|
-
if (file.size > MAX_FILE_SIZE) {
|
|
22
|
-
return {
|
|
23
|
-
type: "size",
|
|
24
|
-
message: `File size must be less than 100KB. Your file is ${Math.round(file.size / 1024)}KB.`,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
// Check MIME type
|
|
28
|
-
if (!ALLOWED_MIME_TYPES.includes(file.type)) {
|
|
29
|
-
return {
|
|
30
|
-
type: "format",
|
|
31
|
-
message: `Unsupported image format. Allowed formats: PNG, JPEG, GIF, BMP, WebP.`,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Convert image file to WebP format
|
|
38
|
-
*/
|
|
39
|
-
export async function convertToWebPFormat(file) {
|
|
40
|
-
try {
|
|
41
|
-
// blobToWebP expects a Blob, so we use the file directly (File is a Blob)
|
|
42
|
-
const webpBlob = await blobToWebP(file, { quality: 0.8 });
|
|
43
|
-
return webpBlob;
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
console.error("webp conversion error", error);
|
|
47
|
-
const errorMessage = error instanceof Error
|
|
48
|
-
? error.message
|
|
49
|
-
: "Unknown error during WebP conversion";
|
|
50
|
-
return {
|
|
51
|
-
type: "conversion",
|
|
52
|
-
message: `Failed to convert image: ${errorMessage}`,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Convert blob to base64 string
|
|
58
|
-
*/
|
|
59
|
-
export function blobToBase64(blob) {
|
|
60
|
-
return new Promise((resolve, reject) => {
|
|
61
|
-
const reader = new FileReader();
|
|
62
|
-
reader.onloadend = () => {
|
|
63
|
-
const base64String = reader.result;
|
|
64
|
-
// Remove "data:image/webp;base64," prefix
|
|
65
|
-
const base64 = base64String.split(",")[1];
|
|
66
|
-
resolve(base64);
|
|
67
|
-
};
|
|
68
|
-
reader.onerror = reject;
|
|
69
|
-
reader.readAsDataURL(blob);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Process avatar file: validate, convert to WebP, return base64
|
|
74
|
-
*/
|
|
75
|
-
export async function processAvatarFile(file) {
|
|
76
|
-
// Validate file
|
|
77
|
-
const validationError = validateAvatarFile(file);
|
|
78
|
-
if (validationError) {
|
|
79
|
-
return validationError;
|
|
80
|
-
}
|
|
81
|
-
// Convert to WebP
|
|
82
|
-
const conversionResult = await convertToWebPFormat(file);
|
|
83
|
-
// if ("type" in conversionResult) {
|
|
84
|
-
// // It's an error
|
|
85
|
-
// return conversionResult as AvatarUploadError;
|
|
86
|
-
// }
|
|
87
|
-
// Convert blob to base64
|
|
88
|
-
try {
|
|
89
|
-
const fileBase64 = await blobToBase64(conversionResult);
|
|
90
|
-
return {
|
|
91
|
-
success: true,
|
|
92
|
-
fileBase64,
|
|
93
|
-
contentType: "image/webp",
|
|
94
|
-
originalFileName: file.name,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
99
|
-
return {
|
|
100
|
-
type: "unknown",
|
|
101
|
-
message: `Failed to encode image: ${errorMessage}`,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const fileToBase64: (file: File) => Promise<string>;
|
package/utils/file-to-base64.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export const fileToBase64 = (file) => new Promise((resolve, reject) => {
|
|
2
|
-
const reader = new FileReader();
|
|
3
|
-
reader.onload = () => {
|
|
4
|
-
var _a;
|
|
5
|
-
const result = reader.result;
|
|
6
|
-
resolve((_a = result.split(",")[1]) !== null && _a !== void 0 ? _a : "");
|
|
7
|
-
};
|
|
8
|
-
reader.onerror = () => reject(reader.error);
|
|
9
|
-
reader.readAsDataURL(file);
|
|
10
|
-
});
|