codex-marketplace-ui-compon.../stories/Upload.stories.tsx

115 lines
2.5 KiB
TypeScript
Raw Normal View History

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";
import { Upload } from "../src/components/Upload/Upload";
import { fn } from "@storybook/test";
2024-08-23 13:31:37 +00:00
import "./Upload.stories.css";
2024-08-20 13:57:58 +00:00
const meta = {
title: "Advanced/Upload",
component: Upload,
parameters: {
layout: "centered",
},
tags: ["autodocs"],
argTypes: {},
args: {
onMouseEnter: fn(),
onMouseLeave: fn(),
onClick: fn(),
onSuccess: fn(),
onDeleteItem: fn(),
onFileChange: fn(),
},
2024-08-20 13:57:58 +00:00
} satisfies Meta<typeof Upload>;
export default meta;
const queryClient = new QueryClient();
type Props = {
onClick?: () => void;
onMouseEnter?: () => void;
onMouseLeave?: () => void;
onSuccess?: () => void;
onDeletedItem?: () => void;
};
const Template = (p: Props) => {
2024-08-20 13:57:58 +00:00
return (
<QueryClientProvider client={queryClient}>
{<Upload useWorker={false} multiple {...p} />}
2024-08-20 13:57:58 +00:00
</QueryClientProvider>
);
};
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) => {
let timeout: number;
2024-08-20 13:57:58 +00:00
resolve({
abort: () => {
window.clearInterval(timeout);
},
result: new Promise((resolve) => {
let count = 0;
timeout = window.setInterval(() => {
2024-08-20 13:57:58 +00:00
count++;
onProgress(500 * count, 1500);
if (count === 3) {
window.clearInterval(timeout);
resolve({
2024-08-23 13:31:37 +00:00
error: false,
2024-08-20 13:57:58 +00:00
data: Date.now().toString(),
});
}
}, 1500);
}),
});
});
}
);
const SlowTemplate = (p: Props) => {
2024-08-20 13:57:58 +00:00
return (
<div className="demo">
<QueryClientProvider client={queryClient}>
{<Upload useWorker={false} multiple provider={slowProvider} {...p} />}
2024-08-20 13:57:58 +00:00
</QueryClientProvider>
</div>
);
};
export const Slow = SlowTemplate.bind({});
2024-08-20 13:57:58 +00:00
const SingleTemplate = (p: Props) => {
2024-08-20 13:57:58 +00:00
return (
<div className="demo">
<QueryClientProvider client={queryClient}>
{
<Upload
useWorker={false}
multiple={false}
editable={false}
provider={slowProvider}
{...p}
2024-08-20 13:57:58 +00:00
/>
}
</QueryClientProvider>
</div>
);
};
export const Single = SingleTemplate.bind({});