import React, { useState } from 'react'; import { Box } from '@mui/material'; import ToolContent from '@components/ToolContent'; import { GetGroupsType } from '@components/options/ToolOptions'; import { ToolComponentProps } from '@tools/defineTool'; import ToolTextInput from '@components/input/ToolTextInput'; import ToolTextResult from '@components/result/ToolTextResult'; import { CardExampleType } from '@components/examples/ToolExamples'; import SelectWithDesc from '@components/options/SelectWithDesc'; import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; import SimpleRadio from '@components/options/SimpleRadio'; import TextFieldWithDesc from '@components/options/TextFieldWithDesc'; import { csvColumnsSwap } from './service'; const initialValues = { fromPositionStatus: true, toPositionStatus: true, fromPosition: '1', toPosition: '2', fromHeader: '', toHeader: '', emptyValuesFilling: true, customFiller: '', deleteComment: true, commentCharacter: '#', emptyLines: true }; type InitialValuesType = typeof initialValues; const exampleCards: CardExampleType[] = [ { title: 'Move the Key Column to the First Position', description: 'In this example, we use our CSV column swapping tool to bring the most important information to the first column. As we are planning to go on vacation soon, in the input of the tool, we load data about national parks that include their names and locations. To decide, which is the closest park to us, we need to see the parks location first, therefore, we swap the first and second data columns so that the "location" column is at the beginning of the CSV data.', sampleText: `park_name,location Yellowstone,Wyoming Yosemite,California Grand Canyon,Arizona Rocky Mountain,Colorado Zion Park,Utah`, sampleResult: `location,park_name Wyoming,Yellowstone California,Yosemite Arizona,Grand Canyon Colorado,Rocky Mountain Utah,Zion Park`, sampleOptions: { fromPositionStatus: true, toPositionStatus: true, fromPosition: '1', toPosition: '2', fromHeader: '', toHeader: '', emptyValuesFilling: false, customFiller: '*', deleteComment: false, commentCharacter: '', emptyLines: false } }, { title: 'Reorganize Columns in Vitamins CSV', description: 'In this example, a lab intern made a mistake and created a corrupted CSV file with mixed-up columns and missing data. To fix the file, we swap the columns based on the headers "Vitamin" and "Function" so that the "Vitamin" column becomes the first in the output data. We also fill the incomplete CSV data by adding a custom asterisk "*" symbol in place of missing values.', sampleText: `Function,Fat-Soluble,Vitamin,Sources Supports vision,Fat-Soluble,A,Carrots Immune function,Water-Soluble,C,Citrus fruits Bone health,Fat-Soluble,D,Fatty fish Antioxidant,Fat-Soluble,E,Nuts Blood clotting,Fat-Soluble,K,Leafy greens Energy production,Water-Soluble,B1 Energy production,Water-Soluble,B2 Energy production,Water-Soluble,B3,Meat Protein metabolism,Water-Soluble,B6,Poultry Nervous system,Water-Soluble,B12,Meat`, sampleResult: `Vitamin,Fat-Soluble,Function,Sources A,Fat-Soluble,Supports vision,Carrots C,Water-Soluble,Immune function,Citrus fruits D,Fat-Soluble,Bone health,Fatty fish E,Fat-Soluble,Antioxidant,Nuts K,Fat-Soluble,Blood clotting,Leafy greens B1,Water-Soluble,Energy production,* B2,Water-Soluble,Energy production,* B3,Water-Soluble,Energy production,Meat B6,Water-Soluble,Protein metabolism,Poultry B12,Water-Soluble,Nervous system,Meat`, sampleOptions: { fromPositionStatus: false, toPositionStatus: false, fromPosition: '1', toPosition: '2', fromHeader: 'Vitamin', toHeader: 'Function', emptyValuesFilling: false, customFiller: '*', deleteComment: false, commentCharacter: '', emptyLines: false } }, { title: 'Place Columns Side by Side for Analysis', description: 'In this example, we change the order of columns in a CSV dataset to have the columns essential for analysis adjacent to each other. We match the "ScreenSize" column by its name and place it in the second-to-last position "-2". This groups the "ScreenSize" and "Price" columns together, allowing us to easily compare and choose the phone we want to buy. We also remove empty lines and specify that lines starting with the "#" symbol are comments and should be left as is.', sampleText: `Brand,Model,ScreenSize,OS,Price Apple,iPhone 15 Pro Max,6.7″,iOS,$1299 Samsung,Galaxy S23 Ultra,6.8″,Android,$1199 Google,Pixel 7 Pro,6.4″,Android,$899 #OnePlus,11 Pro,6.7″,Android,$949 Xiaomi,13 Ultra,6.6″,Android,$849`, sampleResult: `Brand,Model,OS,ScreenSize,Price Apple,iPhone 15 Pro Max,iOS,6.7″,$1299 Samsung,Galaxy S23 Ultra,Android,6.8″,$1199 Google,Pixel 7 Pro,Android,6.4″,$899 Xiaomi,13 Ultra,Android,6.6″,$849`, sampleOptions: { fromPositionStatus: false, toPositionStatus: true, fromPosition: '1', toPosition: '4', fromHeader: 'ScreenSize', toHeader: 'Function', emptyValuesFilling: true, customFiller: 'x', deleteComment: true, commentCharacter: '#', emptyLines: true } } ]; export default function CsvToTsv({ title, longDescription }: ToolComponentProps) { const [input, setInput] = useState(''); const [result, setResult] = useState(''); const compute = (optionsValues: typeof initialValues, input: string) => { setResult( csvColumnsSwap( input, optionsValues.fromPositionStatus, optionsValues.fromPosition, optionsValues.toPositionStatus, optionsValues.toPosition, optionsValues.fromHeader, optionsValues.toHeader, optionsValues.emptyValuesFilling, optionsValues.customFiller, optionsValues.deleteComment, optionsValues.commentCharacter, optionsValues.emptyLines ) ); }; const getGroups: GetGroupsType = ({ values, updateField }) => [ { title: 'Swap-From Column', component: ( updateField('fromPositionStatus', true)} title="Set Column-From position" checked={values.fromPositionStatus} /> {values.fromPositionStatus && ( updateField('fromPosition', val)} type="number" /> )} updateField('fromPositionStatus', false)} title="Set Column-From Header" checked={!values.fromPositionStatus} /> {!values.fromPositionStatus && ( updateField('fromHeader', val)} /> )} ) }, { title: 'Swap-to Column', component: ( updateField('toPositionStatus', true)} title="Set Column-To position" checked={values.toPositionStatus} /> {values.toPositionStatus && ( updateField('toPosition', val)} type="number" /> )} updateField('toPositionStatus', false)} title="Set Column-To Header" checked={!values.toPositionStatus} /> {!values.toPositionStatus && ( updateField('toHeader', val)} /> )} ) }, { title: 'Incomplete Data', component: ( updateField('emptyValuesFilling', value)} description={ 'Fill incomplete CSV data with empty symbols or a custom symbol.' } /> {!values.emptyValuesFilling && ( updateField('customFiller', val)} /> )} ) }, { title: 'Comments and Empty Lines', component: ( updateField('deleteComment', value)} title="Delete Comments" description="if checked, comments given by the following character will be deleted" /> {!values.emptyValuesFilling && ( updateField('commentCharacter', val)} /> )} updateField('emptyLines', value)} title="Delete Empty Lines" description="Do not include empty lines in the output data." /> ) } ]; return ( } resultComponent={} initialValues={initialValues} getGroups={getGroups} setInput={setInput} compute={compute} toolInfo={{ title: `What is a ${title}?`, description: longDescription }} exampleCards={exampleCards} /> ); }