|
| 1 | +import { Box } from '@mui/material'; |
| 2 | +import React, { useState } from 'react'; |
| 3 | +import ToolContent from '@components/ToolContent'; |
| 4 | +import { ToolComponentProps } from '@tools/defineTool'; |
| 5 | +import ToolTextInput from '@components/input/ToolTextInput'; |
| 6 | +import ToolTextResult from '@components/result/ToolTextResult'; |
| 7 | +import { GetGroupsType } from '@components/options/ToolOptions'; |
| 8 | +import { CardExampleType } from '@components/examples/ToolExamples'; |
| 9 | +import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; |
| 10 | +import SimpleRadio from '@components/options/SimpleRadio'; |
| 11 | +import { truncateClockTime } from './service'; |
| 12 | + |
| 13 | +const initialValues = { |
| 14 | + onlySecond: true, |
| 15 | + zeroPrint: false, |
| 16 | + zeroPadding: true |
| 17 | +}; |
| 18 | +type InitialValuesType = typeof initialValues; |
| 19 | +const exampleCards: CardExampleType<InitialValuesType>[] = [ |
| 20 | + { |
| 21 | + title: 'Truncate Seconds', |
| 22 | + description: |
| 23 | + 'In this example, we get rid of the seconds part from several timer values. We select the "Truncate Only Seconds" mode and get a list of timer values consisting only of hours and minutes in format "hh:mm" (the ":ss" part is removed).', |
| 24 | + sampleText: `01:28:06 |
| 25 | +07:39:56 |
| 26 | +02:12:41 |
| 27 | +10:10:38`, |
| 28 | + sampleResult: `01:28 |
| 29 | +07:39 |
| 30 | +02:12 |
| 31 | +10:10`, |
| 32 | + sampleOptions: { onlySecond: true, zeroPrint: false, zeroPadding: true } |
| 33 | + }, |
| 34 | + { |
| 35 | + title: 'Truncate Minutes and Seconds', |
| 36 | + description: |
| 37 | + 'This example truncates five clock times to an hour. It drops the minutes and seconds parts and only outputs the hours with zero padding.', |
| 38 | + sampleText: `04:42:03 |
| 39 | +07:09:59 |
| 40 | +11:29:16 |
| 41 | +21:30:45 |
| 42 | +13:03:09`, |
| 43 | + sampleResult: `04 |
| 44 | +07 |
| 45 | +11 |
| 46 | +21 |
| 47 | +13`, |
| 48 | + sampleOptions: { onlySecond: false, zeroPrint: false, zeroPadding: true } |
| 49 | + }, |
| 50 | + { |
| 51 | + title: 'Set Seconds to Zero', |
| 52 | + description: |
| 53 | + 'In this example, we set the seconds part of each time to zero by first truncating the time to minutes and then appending a zero at the end in place of the truncated seconds. To do this, we switch on the seconds-truncation mode and activate the option to print-zero-time-parts. We also turn off the padding option and get the output time in format "h:m:s", where the seconds are always zero so the final format is "h:m:0".', |
| 54 | + sampleText: `17:25:55 |
| 55 | +10:16:07 |
| 56 | +12:02:09 |
| 57 | +06:05:11`, |
| 58 | + sampleResult: `17:25:0 |
| 59 | +10:16:0 |
| 60 | +12:2:0 |
| 61 | +6:5:0`, |
| 62 | + sampleOptions: { onlySecond: true, zeroPrint: true, zeroPadding: true } |
| 63 | + } |
| 64 | +]; |
| 65 | + |
| 66 | +export default function TruncateClockTime({ |
| 67 | + title, |
| 68 | + longDescription |
| 69 | +}: ToolComponentProps) { |
| 70 | + const [input, setInput] = useState<string>(''); |
| 71 | + const [result, setResult] = useState<string>(''); |
| 72 | + |
| 73 | + const compute = (optionsValues: typeof initialValues, input: string) => { |
| 74 | + setResult( |
| 75 | + truncateClockTime( |
| 76 | + input, |
| 77 | + optionsValues.onlySecond, |
| 78 | + optionsValues.zeroPrint, |
| 79 | + optionsValues.zeroPadding |
| 80 | + ) |
| 81 | + ); |
| 82 | + }; |
| 83 | + |
| 84 | + const getGroups: GetGroupsType<InitialValuesType> | null = ({ |
| 85 | + values, |
| 86 | + updateField |
| 87 | + }) => [ |
| 88 | + { |
| 89 | + title: 'Truncation Side', |
| 90 | + component: ( |
| 91 | + <Box> |
| 92 | + <SimpleRadio |
| 93 | + onClick={() => updateField('onlySecond', true)} |
| 94 | + checked={values.onlySecond} |
| 95 | + title={'Truncate Only Seconds'} |
| 96 | + description={'Drop the seconds component from each clock time.'} |
| 97 | + /> |
| 98 | + <SimpleRadio |
| 99 | + onClick={() => updateField('onlySecond', false)} |
| 100 | + checked={!values.onlySecond} |
| 101 | + title={'Truncate Minutes and Seconds'} |
| 102 | + description={ |
| 103 | + 'Drop both – the minutes and seconds components from each clock time.' |
| 104 | + } |
| 105 | + /> |
| 106 | + </Box> |
| 107 | + ) |
| 108 | + }, |
| 109 | + { |
| 110 | + title: 'Print Dropped Components', |
| 111 | + component: ( |
| 112 | + <Box> |
| 113 | + <CheckboxWithDesc |
| 114 | + onChange={(val) => updateField('zeroPrint', val)} |
| 115 | + checked={values.zeroPrint} |
| 116 | + title={'Zero-print Truncated Parts'} |
| 117 | + description={'Display the dropped parts as zero values "00".'} |
| 118 | + /> |
| 119 | + </Box> |
| 120 | + ) |
| 121 | + }, |
| 122 | + { |
| 123 | + title: 'Time Padding', |
| 124 | + component: ( |
| 125 | + <Box> |
| 126 | + <CheckboxWithDesc |
| 127 | + onChange={(val) => updateField('zeroPadding', val)} |
| 128 | + checked={values.zeroPadding} |
| 129 | + title={'Use Zero Padding'} |
| 130 | + description={'Make all time components always be two digits wide.'} |
| 131 | + /> |
| 132 | + </Box> |
| 133 | + ) |
| 134 | + } |
| 135 | + ]; |
| 136 | + |
| 137 | + return ( |
| 138 | + <ToolContent |
| 139 | + title={title} |
| 140 | + input={input} |
| 141 | + inputComponent={<ToolTextInput value={input} onChange={setInput} />} |
| 142 | + resultComponent={<ToolTextResult value={result} />} |
| 143 | + initialValues={initialValues} |
| 144 | + getGroups={getGroups} |
| 145 | + setInput={setInput} |
| 146 | + compute={compute} |
| 147 | + toolInfo={{ title: `What is a ${title}?`, description: longDescription }} |
| 148 | + exampleCards={exampleCards} |
| 149 | + /> |
| 150 | + ); |
| 151 | +} |
0 commit comments