Compare commits

...

1 Commits

Author SHA1 Message Date
Luccas Mateus
0f7d28e02e [flatuitable][m] - add bytes + parsingConfig 2023-09-20 08:12:32 -03:00
4 changed files with 46 additions and 29 deletions

View File

@ -0,0 +1,5 @@
---
'@portaljs/components': minor
---
FlatUiTables now accepts a bytes param and a parsingConfig param for CSV links

6
package-lock.json generated
View File

@ -46942,7 +46942,7 @@
}, },
"packages/ckan": { "packages/ckan": {
"name": "@portaljs/ckan", "name": "@portaljs/ckan",
"version": "0.0.3", "version": "0.1.0",
"dependencies": { "dependencies": {
"formik": "^2.2.9", "formik": "^2.2.9",
"swr": "^2.1.5", "swr": "^2.1.5",
@ -47347,7 +47347,7 @@
}, },
"packages/components": { "packages/components": {
"name": "@portaljs/components", "name": "@portaljs/components",
"version": "0.3.1", "version": "0.3.2",
"dependencies": { "dependencies": {
"@githubocto/flat-ui": "^0.14.1", "@githubocto/flat-ui": "^0.14.1",
"@heroicons/react": "^2.0.17", "@heroicons/react": "^2.0.17",
@ -47828,7 +47828,7 @@
}, },
"packages/remark-wiki-link": { "packages/remark-wiki-link": {
"name": "@portaljs/remark-wiki-link", "name": "@portaljs/remark-wiki-link",
"version": "1.0.4", "version": "1.1.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"mdast-util-to-markdown": "^1.5.0", "mdast-util-to-markdown": "^1.5.0",

View File

@ -5,22 +5,20 @@ import LoadingSpinner from './LoadingSpinner';
const queryClient = new QueryClient(); const queryClient = new QueryClient();
export async function getCsv(url: string, corsProxy?: string) { export async function getCsv(url: string, bytes) {
if (corsProxy) {
url = corsProxy + url;
}
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {
Range: 'bytes=0-5132288', Range: `bytes=0-${bytes}`,
}, },
}); });
const data = await response.text(); const data = await response.text();
return data; return data;
} }
export async function parseCsv(file: string): Promise<any> { export async function parseCsv(file: string, parsingConfig): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Papa.parse(file, { Papa.parse(file, {
...parsingConfig,
header: true, header: true,
dynamicTyping: true, dynamicTyping: true,
skipEmptyLines: true, skipEmptyLines: true,
@ -41,25 +39,28 @@ export interface FlatUiTableProps {
url?: string; url?: string;
data?: { [key: string]: number | string }[]; data?: { [key: string]: number | string }[];
rawCsv?: string; rawCsv?: string;
corsProxy?: string;
randomId?: number; randomId?: number;
bytes: number;
parsingConfig: any;
} }
export const FlatUiTable: React.FC<FlatUiTableProps> = ({ export const FlatUiTable: React.FC<FlatUiTableProps> = ({
url, url,
data, data,
rawCsv, rawCsv,
corsProxy, bytes = 5132288,
parsingConfig = {},
}) => { }) => {
const randomId = Math.random(); const randomId = Math.random();
return ( return (
// Provide the client to your App // Provide the client to your App
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<TableInner <TableInner
corsProxy={corsProxy} bytes={bytes}
url={url} url={url}
data={data} data={data}
rawCsv={rawCsv} rawCsv={rawCsv}
randomId={randomId} randomId={randomId}
parsingConfig={parsingConfig}
/> />
</QueryClientProvider> </QueryClientProvider>
); );
@ -69,8 +70,9 @@ const TableInner: React.FC<FlatUiTableProps> = ({
url, url,
data, data,
rawCsv, rawCsv,
corsProxy,
randomId, randomId,
bytes,
parsingConfig,
}) => { }) => {
if (data) { if (data) {
return ( return (
@ -81,12 +83,16 @@ const TableInner: React.FC<FlatUiTableProps> = ({
} }
const { data: csvString, isLoading: isDownloadingCSV } = useQuery( const { data: csvString, isLoading: isDownloadingCSV } = useQuery(
['dataCsv', url, randomId], ['dataCsv', url, randomId],
() => getCsv(url as string, corsProxy), () => getCsv(url as string, bytes),
{ enabled: !!url } { enabled: !!url }
); );
const { data: parsedData, isLoading: isParsing } = useQuery( const { data: parsedData, isLoading: isParsing } = useQuery(
['dataPreview', csvString, randomId], ['dataPreview', csvString, randomId],
() => parseCsv(rawCsv ? (rawCsv as string) : (csvString as string)), () =>
parseCsv(
rawCsv ? (rawCsv as string) : (csvString as string),
parsingConfig
),
{ enabled: rawCsv ? true : !!csvString } { enabled: rawCsv ? true : !!csvString }
); );
if (isParsing || isDownloadingCSV) if (isParsing || isDownloadingCSV)

View File

@ -9,17 +9,24 @@ const meta: Meta = {
tags: ['autodocs'], tags: ['autodocs'],
argTypes: { argTypes: {
data: { data: {
description: "Data to be displayed in the table, must be setup as an array of key value pairs" description:
'Data to be displayed in the table, must be setup as an array of key value pairs',
}, },
csv: { csv: {
description: "CSV data as string.", description: 'CSV data as string.',
}, },
url: { url: {
description: "Fetch the data from a CSV file remotely. only the first 5MB of data will be displayed" description:
'Fetch the data from a CSV file remotely. only the first 5MB of data will be displayed',
},
bytes: {
description:
'Fetch the data from a CSV file remotely. only the first <bytes> of data will be displayed',
},
parsingConfig: {
description:
'Configuration for parsing the CSV data. See https://www.papaparse.com/docs#config for more details',
}, },
corsProxy: {
description: "Optionally you cant set a CORS Proxy to which all your requests you be redirected"
}
}, },
}; };
@ -29,7 +36,7 @@ type Story = StoryObj<FlatUiTableProps>;
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args // More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
export const FromColumnsAndData: Story = { export const FromColumnsAndData: Story = {
name: "Table data", name: 'Table data',
args: { args: {
data: [ data: [
{ id: 1, lastName: 'Snow', firstName: 'Jon', age: 35 }, { id: 1, lastName: 'Snow', firstName: 'Jon', age: 35 },
@ -44,20 +51,19 @@ export const FromColumnsAndData: Story = {
}; };
export const FromRawCSV: Story = { export const FromRawCSV: Story = {
name: "Table from raw CSV", name: 'Table from raw CSV',
args: { args: {
rawCsv: ` rawCsv: `
Year,Temp Anomaly Year,Temp Anomaly
1850,-0.418 1850,-0.418
2020,0.923 2020,0.923
` `,
} },
}; };
export const FromURL: Story = { export const FromURL: Story = {
name: "Table from URL", name: 'Table from URL',
args: { args: {
url: "https://raw.githubusercontent.com/datasets/finance-vix/main/data/vix-daily.csv" url: 'https://ckan-dev.sse.datopian.com/datastore/dump/601c9cf0-595e-46d8-88fc-d1ab2904e2db',
} },
}; };