Update Upload component to not rely on react query and fix error showing

This commit is contained in:
Arnaud 2024-09-30 12:45:06 +02:00
parent ecf1fd5f0a
commit fc9345693f
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
6 changed files with 164 additions and 172 deletions

32
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@codex-storage/marketplace-ui-components", "name": "@codex-storage/marketplace-ui-components",
"version": "0.0.11", "version": "0.0.12",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@codex-storage/marketplace-ui-components", "name": "@codex-storage/marketplace-ui-components",
"version": "0.0.11", "version": "0.0.12",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"lucide-react": "^0.441.0" "lucide-react": "^0.441.0"
@ -21,7 +21,6 @@
"@storybook/react": "^8.2.9", "@storybook/react": "^8.2.9",
"@storybook/react-vite": "^8.2.9", "@storybook/react-vite": "^8.2.9",
"@storybook/test": "^8.2.9", "@storybook/test": "^8.2.9",
"@tanstack/react-query": "^5.51.24",
"@typescript-eslint/eslint-plugin": "^8.6.0", "@typescript-eslint/eslint-plugin": "^8.6.0",
"@typescript-eslint/parser": "^8.0.0", "@typescript-eslint/parser": "^8.0.0",
"@vitejs/plugin-react": "^4.3.1", "@vitejs/plugin-react": "^4.3.1",
@ -42,7 +41,6 @@
}, },
"peerDependencies": { "peerDependencies": {
"@codex-storage/sdk-js": "0.0.6", "@codex-storage/sdk-js": "0.0.6",
"@tanstack/react-query": "^5.51.24",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1" "react-dom": "^18.3.1"
} }
@ -2540,32 +2538,6 @@
"storybook": "^8.3.2" "storybook": "^8.3.2"
} }
}, },
"node_modules/@tanstack/query-core": {
"version": "5.56.2",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.56.2.tgz",
"integrity": "sha512-gor0RI3/R5rVV3gXfddh1MM+hgl0Z4G7tj6Xxpq6p2I03NGPaJ8dITY9Gz05zYYb/EJq9vPas/T4wn9EaDPd4Q==",
"dev": true,
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/react-query": {
"version": "5.56.2",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.56.2.tgz",
"integrity": "sha512-SR0GzHVo6yzhN72pnRhkEFRAHMsUo5ZPzAxfTMvUxFIDVS6W9LYUp6nXW3fcHVdg0ZJl8opSH85jqahvm6DSVg==",
"dev": true,
"dependencies": {
"@tanstack/query-core": "5.56.2"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"react": "^18 || ^19"
}
},
"node_modules/@testing-library/dom": { "node_modules/@testing-library/dom": {
"version": "10.4.0", "version": "10.4.0",
"resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.0.tgz", "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.0.tgz",

View File

@ -5,7 +5,7 @@
"type": "git", "type": "git",
"url": "https://github.com/codex-storage/codex-marketplace-ui-components" "url": "https://github.com/codex-storage/codex-marketplace-ui-components"
}, },
"version": "0.0.11", "version": "0.0.12",
"type": "module", "type": "module",
"scripts": { "scripts": {
"prepack": "npm run build", "prepack": "npm run build",
@ -36,7 +36,6 @@
}, },
"peerDependencies": { "peerDependencies": {
"@codex-storage/sdk-js": "0.0.6", "@codex-storage/sdk-js": "0.0.6",
"@tanstack/react-query": "^5.51.24",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1" "react-dom": "^18.3.1"
}, },
@ -50,7 +49,6 @@
"@storybook/react": "^8.2.9", "@storybook/react": "^8.2.9",
"@storybook/react-vite": "^8.2.9", "@storybook/react-vite": "^8.2.9",
"@storybook/test": "^8.2.9", "@storybook/test": "^8.2.9",
"@tanstack/react-query": "^5.51.24",
"@typescript-eslint/eslint-plugin": "^8.6.0", "@typescript-eslint/eslint-plugin": "^8.6.0",
"@typescript-eslint/parser": "^8.0.0", "@typescript-eslint/parser": "^8.0.0",
"@vitejs/plugin-react": "^4.3.1", "@vitejs/plugin-react": "^4.3.1",

View File

@ -12,7 +12,6 @@ import { Toast } from "../Toast/Toast";
import { UploadStatus } from "./types"; import { UploadStatus } from "./types";
import { CircleCheck, TriangleAlert, CircleX, CircleStop } from "lucide-react"; import { CircleCheck, TriangleAlert, CircleX, CircleStop } from "lucide-react";
import { Spinner } from "../Spinner/Spinner"; import { Spinner } from "../Spinner/Spinner";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { CodexData } from "@codex-storage/sdk-js"; import { CodexData } from "@codex-storage/sdk-js";
import { WebFileIcon } from "../WebFileIcon/WebFileIcon"; import { WebFileIcon } from "../WebFileIcon/WebFileIcon";
import { ButtonIcon } from "../ButtonIcon/ButtonIcon"; import { ButtonIcon } from "../ButtonIcon/ButtonIcon";
@ -55,6 +54,9 @@ type Action =
| { | {
type: "cancel"; type: "cancel";
} }
| {
type: "delete";
}
| { | {
type: "error"; type: "error";
error: string; error: string;
@ -81,20 +83,24 @@ function reducer(state: State, action: Action) {
case "completed": { case "completed": {
return { return {
...state, ...state,
// Just to ensure the file upload is in done status,
// in case of the onprogress callback function was not called
status: "done" as UploadStatus, status: "done" as UploadStatus,
cid: action.cid, cid: action.cid,
}; };
} }
case "cancel": { case "cancel": {
if (state.status === "progress") { return {
return { ...state,
...state, status: "error" as UploadStatus,
status: "error" as UploadStatus, error: "The upload has been cancelled.",
error: "The upload has been cancelled.", };
}; }
}
case "delete": {
return { return {
progress: { loaded: 0, total: 0 }, progress: { loaded: 0, total: 0 },
cid: "", cid: "",
@ -125,8 +131,7 @@ export function UploadFile({
// useWorker, // useWorker,
}: UploadFileProps) { }: UploadFileProps) {
const abort = useRef<(() => void) | null>(null); const abort = useRef<(() => void) | null>(null);
const queryClient = useQueryClient(); // const worker = useRef<Worker | null>(null);
const worker = useRef<Worker | null>(null);
const [toast, setToast] = useState({ time: 0, message: "" }); const [toast, setToast] = useState({ time: 0, message: "" });
const [state, dispatch] = useReducer<Reducer<State, Action>>(reducer, { const [state, dispatch] = useReducer<Reducer<State, Action>>(reducer, {
progress: { loaded: 0, total: 0 }, progress: { loaded: 0, total: 0 },
@ -135,47 +140,27 @@ export function UploadFile({
status: "progress" as UploadStatus, status: "progress" as UploadStatus,
error: "", error: "",
}); });
const { mutateAsync } = useMutation({
mutationKey: ["upload"],
mutationFn: (file: File) => {
const res = codexData.upload(file, onProgress);
abort.current = res.abort; const upload = useCallback(async () => {
const { abort: a, result } = codexData.upload(file, onProgress);
abort.current = a;
const res = await result;
if (res.error) {
dispatch({ type: "error", error: res.data.message });
return;
}
dispatch({ type: "completed", cid: state.cid });
onSuccess?.(state.cid, file);
}, [state.cid, codexData, onSuccess, file]);
return res.result.then((safe) =>
safe.error
? Promise.reject(safe.data.message)
: Promise.resolve(safe.data)
);
},
onError: (error) => {
// worker.current?.terminate();
dispatch({ type: "error", error: error.message });
},
onSuccess: (cid: string) => {
onInternalSuccess(cid);
},
});
const init = useRef(false); const init = useRef(false);
const onInternalSuccess = useCallback(
(cid: string) => {
worker.current?.terminate();
queryClient.invalidateQueries({
queryKey: ["cids"],
});
if (onSuccess) {
onSuccess(cid, file);
dispatch({ type: "reset" });
} else {
dispatch({ type: "completed", cid });
}
},
[onSuccess, dispatch, queryClient, file]
);
const onProgress = (loaded: number, total: number) => { const onProgress = (loaded: number, total: number) => {
dispatch({ dispatch({
type: "progress", type: "progress",
@ -204,7 +189,7 @@ export function UploadFile({
reader.readAsDataURL(file); reader.readAsDataURL(file);
} }
mutateAsync(file); upload();
// if (useWorker) { // if (useWorker) {
// worker.current = new Worker(new URL("./worker", import.meta.url), { // worker.current = new Worker(new URL("./worker", import.meta.url), {
@ -239,24 +224,28 @@ export function UploadFile({
// } else { // } else {
// mutateAsync(file); // mutateAsync(file);
// } // }
}, [file, mutateAsync, onInternalSuccess, codexData]); }, [file, upload, codexData]);
const onCancel = () => { const onCancel = () => {
if (worker.current) { // if (worker.current) {
worker.current.postMessage({ type: "abort" }); // worker.current.postMessage({ type: "abort" });
} else { // } else {
abort.current?.(); // abort.current?.();
} // }
abort.current?.();
dispatch({ type: "cancel" }); const type = state.status === "progress" ? "cancel" : "delete";
dispatch({ type });
}; };
const onInternalClose = () => { const onInternalClose = () => {
if (worker.current) { // if (worker.current) {
worker.current.postMessage({ type: "abort" }); // worker.current.postMessage({ type: "abort" });
} else { // } else {
abort.current?.(); // abort.current?.();
} // }
abort.current?.();
onClose(id); onClose(id);
}; };
@ -274,7 +263,7 @@ export function UploadFile({
const parts = file.name.split("."); const parts = file.name.split(".");
const extension = parts.pop(); const extension = parts.pop();
const filename = parts.join("."); const filename = parts.join(".");
const { cid, error, preview, progress, status } = state; const { cid, error = "", preview, progress, status } = state;
const onAction = state.status === "progress" ? onCancel : onInternalClose; const onAction = state.status === "progress" ? onCancel : onInternalClose;
const percent = const percent =
progress.total > 0 ? (progress.loaded / progress.total) * 100 : 0; progress.total > 0 ? (progress.loaded / progress.total) * 100 : 0;
@ -350,7 +339,7 @@ export function UploadFile({
</> </>
)} )}
{error && <SimpleText variant="error">{error}</SimpleText>} <SimpleText variant="error">{error ? error : <>&nbsp;</>}</SimpleText>
<Toast message={toast.message} time={toast.time} variant="success" /> <Toast message={toast.message} time={toast.time} variant="success" />
</div> </div>

View File

@ -1,9 +1,8 @@
import type { Meta } from "@storybook/react"; import type { Meta } from "@storybook/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { Upload } from "../src/components/Upload/Upload"; import { Upload } from "../src/components/Upload/Upload";
import { fn } from "@storybook/test"; import { fn } from "@storybook/test";
import "./Upload.stories.css"; import "./Upload.stories.css";
import { CodexDataSdk, CodexDataSlowSdk } from "./sdk"; import { CodexDataSdk, CodexDataSlowSdk, CodexDataErrorSdk } from "./sdk";
const meta = { const meta = {
title: "Advanced/Upload", title: "Advanced/Upload",
@ -25,8 +24,6 @@ const meta = {
export default meta; export default meta;
const queryClient = new QueryClient();
type Props = { type Props = {
onClick?: () => void; onClick?: () => void;
@ -40,11 +37,7 @@ type Props = {
}; };
const Template = (p: Props) => { const Template = (p: Props) => {
return ( return <Upload multiple {...p} codexData={CodexDataSdk} />;
<QueryClientProvider client={queryClient}>
{<Upload multiple {...p} codexData={CodexDataSdk} />}
</QueryClientProvider>
);
}; };
export const Multiple = Template.bind({}); export const Multiple = Template.bind({});
@ -52,9 +45,7 @@ export const Multiple = Template.bind({});
const SlowTemplate = (p: Props) => { const SlowTemplate = (p: Props) => {
return ( return (
<div className="demo"> <div className="demo">
<QueryClientProvider client={queryClient}> <Upload multiple codexData={CodexDataSlowSdk} {...p} />
{<Upload multiple codexData={CodexDataSlowSdk} {...p} />}
</QueryClientProvider>
</div> </div>
); );
}; };
@ -64,18 +55,33 @@ export const Slow = SlowTemplate.bind({});
const SingleTemplate = (p: Props) => { const SingleTemplate = (p: Props) => {
return ( return (
<div className="demo"> <div className="demo">
<QueryClientProvider client={queryClient}> {
{ <Upload
<Upload multiple={false}
multiple={false} editable={false}
editable={false} codexData={CodexDataSlowSdk}
codexData={CodexDataSlowSdk} {...p}
{...p} />
/> }
}
</QueryClientProvider>
</div> </div>
); );
}; };
export const Single = SingleTemplate.bind({}); export const Single = SingleTemplate.bind({});
const ErrorTemplate = (p: Props) => {
return (
<div className="demo">
{
<Upload
multiple={false}
editable={false}
codexData={CodexDataErrorSdk}
{...p}
/>
}
</div>
);
};
export const Error = ErrorTemplate.bind({});

View File

@ -1,36 +1,34 @@
import { CodexData, UploadResponse } from "@codex-storage/sdk-js"; import { CodexData, CodexError, UploadResponse } from "@codex-storage/sdk-js";
class CodexDataMock extends CodexData { class CodexDataMock extends CodexData {
override upload( override upload(
_: File, _: File,
onProgress?: (loaded: number, total: number) => void onProgress?: (loaded: number, total: number) => void
): Promise<UploadResponse> { ): UploadResponse {
return new Promise<UploadResponse>((resolve) => { let timeout: number;
let timeout: number;
resolve({ return {
abort: () => { abort: () => {
window.clearInterval(timeout); window.clearInterval(timeout);
}, },
result: new Promise((resolve) => { result: new Promise((resolve) => {
let count = 0; let count = 0;
timeout = window.setInterval(() => { timeout = window.setInterval(() => {
count++; count++;
onProgress?.(500 * count, 1500); onProgress?.(500 * count, 1500);
if (count === 3) { if (count === 3) {
window.clearInterval(timeout); window.clearInterval(timeout);
resolve({ resolve({
error: false, error: false,
data: Date.now().toString(), data: Date.now().toString(),
}); });
} }
}, 1500); }, 1500);
}), }),
}); };
});
} }
} }
@ -40,34 +38,66 @@ class CodexDataSlowMock extends CodexData {
override upload( override upload(
_: File, _: File,
onProgress?: (loaded: number, total: number) => void onProgress?: (loaded: number, total: number) => void
): Promise<UploadResponse> { ): UploadResponse {
return new Promise<UploadResponse>((resolve) => { let timeout: number;
let timeout: number;
resolve({ return {
abort: () => { abort: () => {
window.clearInterval(timeout); window.clearInterval(timeout);
}, },
result: new Promise((resolve) => { result: new Promise((resolve) => {
let count = 0; let count = 0;
timeout = window.setInterval(() => { timeout = window.setInterval(() => {
count++; count++;
onProgress?.(500 * count, 1500); onProgress?.(500 * count, 1500);
if (count === 3) { if (count === 3) {
window.clearInterval(timeout); window.clearInterval(timeout);
resolve({ resolve({
error: false, error: false,
data: Date.now().toString(), data: Date.now().toString(),
}); });
} }
}, 1500); }, 1500);
}), }),
}); }
});
} }
} }
export const CodexDataSlowSdk = new CodexDataSlowMock(""); export const CodexDataSlowSdk = new CodexDataSlowMock("");
class CodexDataErrorMock extends CodexData {
override upload(
_: File,
onProgress?: (loaded: number, total: number) => void
): UploadResponse {
let timeout: number;
return {
abort: () => {
window.clearInterval(timeout);
},
result: new Promise((resolve) => {
let count = 0;
timeout = window.setInterval(() => {
count++;
onProgress?.(500 * count, 1500);
if (count === 3) {
window.clearInterval(timeout);
resolve({
error: true,
data: new CodexError("Some error here"),
});
}
}, 1500);
}),
}
}
}
export const CodexDataErrorSdk = new CodexDataErrorMock("");

View File

@ -5,14 +5,13 @@ import react from "@vitejs/plugin-react";
import { libInjectCss } from "vite-plugin-lib-inject-css"; import { libInjectCss } from "vite-plugin-lib-inject-css";
import { extname, relative } from "path"; import { extname, relative } from "path";
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
import pkg from "glob"; import { globSync } from "glob";
const { glob } = pkg;
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
worker: { worker: {
rollupOptions: { rollupOptions: {
external: ["@codex-storage/sdk-js", "@tanstack/react-query"], external: ["@codex-storage/sdk-js"],
output: { output: {
globals: { globals: {
"@codex-storage/sdk-js": "codex-sdk-js", "@codex-storage/sdk-js": "codex-sdk-js",
@ -39,13 +38,11 @@ export default defineConfig({
"react", "react",
"react/jsx-runtime", "react/jsx-runtime",
"@codex-storage/sdk-js", "@codex-storage/sdk-js",
"@tanstack/react-query",
], ],
input: Object.fromEntries( input: Object.fromEntries(
glob globSync("src/**/*.{ts,tsx}", {
.sync("src/**/*.{ts,tsx}", { ignore: ["src/**/*.d.ts"],
ignore: ["src/**/*.d.ts"], })
})
.map((file) => [ .map((file) => [
// The name of the entry point // The name of the entry point
// lib/nested/foo.ts becomes nested/foo // lib/nested/foo.ts becomes nested/foo