2024-08-22 15:07:05 +00:00
|
|
|
import type { Meta } from "@storybook/react";
|
2024-08-20 13:57:58 +00:00
|
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
|
import { UploadResponse } from "@codex/sdk-js";
|
2024-08-21 15:14:40 +00:00
|
|
|
import { Upload } from "../src/components/Upload/Upload";
|
2024-08-20 13:57:58 +00:00
|
|
|
|
|
|
|
const meta = {
|
|
|
|
title: "Advanced/Upload",
|
|
|
|
component: Upload,
|
|
|
|
parameters: {
|
|
|
|
layout: "centered",
|
|
|
|
},
|
|
|
|
tags: ["autodocs"],
|
|
|
|
argTypes: {},
|
|
|
|
} satisfies Meta<typeof Upload>;
|
|
|
|
|
|
|
|
export default meta;
|
|
|
|
|
|
|
|
const queryClient = new QueryClient();
|
|
|
|
|
|
|
|
const Template = () => {
|
|
|
|
return (
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
{<Upload useWorker={false} multiple />}
|
|
|
|
</QueryClientProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-08-21 15:14:40 +00:00
|
|
|
export const Multiple = Template.bind({});
|
2024-08-20 13:57:58 +00:00
|
|
|
|
|
|
|
const slowProvider = () =>
|
|
|
|
Promise.resolve(
|
|
|
|
(_: File, onProgress: (loaded: number, total: number) => void) => {
|
|
|
|
return new Promise<UploadResponse>((resolve) => {
|
2024-08-21 15:14:40 +00:00
|
|
|
let timeout: number;
|
2024-08-20 13:57:58 +00:00
|
|
|
|
|
|
|
resolve({
|
|
|
|
abort: () => {
|
|
|
|
window.clearInterval(timeout);
|
|
|
|
},
|
|
|
|
result: new Promise((resolve) => {
|
|
|
|
let count = 0;
|
2024-08-21 15:14:40 +00:00
|
|
|
timeout = window.setInterval(() => {
|
2024-08-20 13:57:58 +00:00
|
|
|
count++;
|
|
|
|
|
|
|
|
onProgress(500 * count, 1500);
|
|
|
|
|
|
|
|
if (count === 3) {
|
|
|
|
window.clearInterval(timeout);
|
|
|
|
|
|
|
|
resolve({
|
|
|
|
error: false as false,
|
|
|
|
data: Date.now().toString(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 1500);
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const SlowTemplate = () => {
|
|
|
|
return (
|
|
|
|
<div className="demo">
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
{<Upload useWorker={false} multiple provider={slowProvider} />}
|
|
|
|
</QueryClientProvider>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-08-21 15:14:40 +00:00
|
|
|
export const Slow = SlowTemplate.bind({});
|
2024-08-20 13:57:58 +00:00
|
|
|
|
|
|
|
const SingleTemplate = () => {
|
|
|
|
return (
|
|
|
|
<div className="demo">
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
{
|
|
|
|
<Upload
|
|
|
|
useWorker={false}
|
|
|
|
multiple={false}
|
|
|
|
editable={false}
|
|
|
|
provider={slowProvider}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
</QueryClientProvider>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-08-21 15:14:40 +00:00
|
|
|
export const Single = SingleTemplate.bind({});
|