mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 14:09:31 +02:00
fix: text split try catch
This commit is contained in:
@@ -1,26 +1,41 @@
|
||||
import { BrowserRouter, useRoutes } from 'react-router-dom'
|
||||
import routesConfig from '../config/routesConfig'
|
||||
import Navbar from './Navbar'
|
||||
import { Suspense } from 'react'
|
||||
import Loading from './Loading'
|
||||
import { ThemeProvider } from '@mui/material'
|
||||
import theme from '../config/muiConfig'
|
||||
import { BrowserRouter, useRoutes } from 'react-router-dom';
|
||||
import routesConfig from '../config/routesConfig';
|
||||
import Navbar from './Navbar';
|
||||
import { Suspense } from 'react';
|
||||
import Loading from './Loading';
|
||||
import { ThemeProvider } from '@mui/material';
|
||||
import theme from '../config/muiConfig';
|
||||
import { CustomSnackBarProvider } from '../contexts/CustomSnackBarContext';
|
||||
import { SnackbarProvider } from 'notistack';
|
||||
|
||||
const AppRoutes = () => {
|
||||
return useRoutes(routesConfig)
|
||||
}
|
||||
return useRoutes(routesConfig);
|
||||
};
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<ThemeProvider theme={theme}>
|
||||
<BrowserRouter>
|
||||
<Navbar />
|
||||
<Suspense fallback={<Loading />}>
|
||||
<AppRoutes />
|
||||
</Suspense>
|
||||
</BrowserRouter>
|
||||
<SnackbarProvider
|
||||
maxSnack={5}
|
||||
anchorOrigin={{
|
||||
vertical: 'bottom',
|
||||
horizontal: 'right'
|
||||
}}
|
||||
classes={{
|
||||
containerRoot: 'bottom-0 right-0 mb-52 md:mb-68 mr-8 lg:mr-80 z-99'
|
||||
}}
|
||||
>
|
||||
<CustomSnackBarProvider>
|
||||
<BrowserRouter>
|
||||
<Navbar />
|
||||
<Suspense fallback={<Loading />}>
|
||||
<AppRoutes />
|
||||
</Suspense>
|
||||
</BrowserRouter>
|
||||
</CustomSnackBarProvider>
|
||||
</SnackbarProvider>
|
||||
</ThemeProvider>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export default App
|
||||
export default App;
|
||||
|
@@ -1,11 +1,17 @@
|
||||
import Typography from '@mui/material/Typography'
|
||||
import { Box, Stack, TextField } from '@mui/material'
|
||||
import Button from '@mui/material/Button'
|
||||
import DownloadIcon from '@mui/icons-material/Download'
|
||||
import ContentPasteIcon from '@mui/icons-material/ContentPaste'
|
||||
import React from 'react'
|
||||
import Typography from '@mui/material/Typography';
|
||||
import { Box, Stack, TextField } from '@mui/material';
|
||||
import Button from '@mui/material/Button';
|
||||
import DownloadIcon from '@mui/icons-material/Download';
|
||||
import ContentPasteIcon from '@mui/icons-material/ContentPaste';
|
||||
import React from 'react';
|
||||
|
||||
export default function ToolTextResult({ title = 'Result', value }: { title?: string; value: string }) {
|
||||
export default function ToolTextResult({
|
||||
title = 'Result',
|
||||
value
|
||||
}: {
|
||||
title?: string;
|
||||
value: string;
|
||||
}) {
|
||||
return (
|
||||
<Box>
|
||||
<Typography fontSize={30} color={'primary'}>
|
||||
@@ -17,5 +23,5 @@ export default function ToolTextResult({ title = 'Result', value }: { title?: st
|
||||
<Button startIcon={<ContentPasteIcon />}>Copy to clipboard</Button>
|
||||
</Stack>
|
||||
</Box>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { createTheme } from '@mui/material'
|
||||
import { createTheme } from '@mui/material';
|
||||
|
||||
const theme = createTheme({
|
||||
typography: {
|
||||
@@ -6,7 +6,8 @@ const theme = createTheme({
|
||||
textTransform: 'none'
|
||||
}
|
||||
},
|
||||
palette: { background: { default: '#ebf5ff' } }
|
||||
})
|
||||
palette: { background: { default: '#ebf5ff' } },
|
||||
zIndex: { snackbar: 100000 }
|
||||
});
|
||||
|
||||
export default theme
|
||||
export default theme;
|
||||
|
35
src/contexts/CustomSnackBarContext.tsx
Normal file
35
src/contexts/CustomSnackBarContext.tsx
Normal file
@@ -0,0 +1,35 @@
|
||||
import { createContext, FC, ReactNode } from 'react';
|
||||
import { Zoom } from '@mui/material';
|
||||
import { useSnackbar } from 'notistack';
|
||||
|
||||
type CustomSnackBarContext = {
|
||||
showSnackBar: (message: string, type: 'error' | 'success') => void;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
||||
export const CustomSnackBarContext = createContext<CustomSnackBarContext>(
|
||||
{} as CustomSnackBarContext
|
||||
);
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const CustomSnackBarProvider: FC<Props> = ({ children }) => {
|
||||
const { enqueueSnackbar } = useSnackbar();
|
||||
const showSnackBar = (message: string, type: 'error' | 'success') => {
|
||||
enqueueSnackbar(message, {
|
||||
variant: type,
|
||||
anchorOrigin: {
|
||||
vertical: 'top',
|
||||
horizontal: 'right'
|
||||
},
|
||||
TransitionComponent: Zoom
|
||||
});
|
||||
};
|
||||
return (
|
||||
<CustomSnackBarContext.Provider value={{ showSnackBar }}>
|
||||
{children}
|
||||
</CustomSnackBarContext.Provider>
|
||||
);
|
||||
};
|
@@ -3,13 +3,14 @@ import ToolLayout from '../../../components/ToolLayout';
|
||||
import { Box, Stack, TextField } from '@mui/material';
|
||||
import Grid from '@mui/material/Grid';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||||
import ToolTextInput from '../../../components/input/ToolTextInput';
|
||||
import ToolTextResult from '../../../components/result/ToolTextResult';
|
||||
import { Field, Formik, FormikProps, useFormikContext } from 'formik';
|
||||
import * as Yup from 'yup';
|
||||
import ToolOptions from '../../../components/ToolOptions';
|
||||
import { splitIntoChunks, splitTextByLength } from './service';
|
||||
import { CustomSnackBarContext } from '../../../contexts/CustomSnackBarContext';
|
||||
|
||||
type SplitOperatorType = 'symbol' | 'regex' | 'length' | 'chunks';
|
||||
const initialValues = {
|
||||
@@ -142,38 +143,45 @@ export default function SplitText() {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
const formRef = useRef<FormikProps<typeof initialValues>>(null);
|
||||
const { showSnackBar } = useContext(CustomSnackBarContext);
|
||||
|
||||
const FormikListenerComponent = () => {
|
||||
const { values } = useFormikContext<typeof initialValues>();
|
||||
|
||||
useEffect(() => {
|
||||
const {
|
||||
splitSeparatorType,
|
||||
outputSeparator,
|
||||
charBeforeChunk,
|
||||
charAfterChunk,
|
||||
chunksValue,
|
||||
symbolValue,
|
||||
regexValue,
|
||||
lengthValue
|
||||
} = values;
|
||||
let splitText;
|
||||
switch (splitSeparatorType) {
|
||||
case 'symbol':
|
||||
splitText = input.split(symbolValue);
|
||||
break;
|
||||
case 'regex':
|
||||
splitText = input.split(new RegExp(regexValue));
|
||||
break;
|
||||
case 'length':
|
||||
splitText = splitTextByLength(input, Number(lengthValue));
|
||||
break;
|
||||
case 'chunks':
|
||||
splitText = splitIntoChunks(input, Number(chunksValue)).map(
|
||||
(chunk) => `${charBeforeChunk}${chunk}${charAfterChunk}`
|
||||
);
|
||||
try {
|
||||
const {
|
||||
splitSeparatorType,
|
||||
outputSeparator,
|
||||
charBeforeChunk,
|
||||
charAfterChunk,
|
||||
chunksValue,
|
||||
symbolValue,
|
||||
regexValue,
|
||||
lengthValue
|
||||
} = values;
|
||||
let splitText;
|
||||
switch (splitSeparatorType) {
|
||||
case 'symbol':
|
||||
splitText = input.split(symbolValue);
|
||||
break;
|
||||
case 'regex':
|
||||
splitText = input.split(new RegExp(regexValue));
|
||||
break;
|
||||
case 'length':
|
||||
splitText = splitTextByLength(input, Number(lengthValue));
|
||||
break;
|
||||
case 'chunks':
|
||||
splitText = splitIntoChunks(input, Number(chunksValue)).map(
|
||||
(chunk) => `${charBeforeChunk}${chunk}${charAfterChunk}`
|
||||
);
|
||||
}
|
||||
const res = splitText.join(outputSeparator);
|
||||
setResult(res);
|
||||
} catch (exception: unknown) {
|
||||
if (exception instanceof Error)
|
||||
showSnackBar(exception.message, 'error');
|
||||
}
|
||||
const res = splitText.join(outputSeparator);
|
||||
setResult(res);
|
||||
}, [values, input]);
|
||||
|
||||
return null; // This component doesn't render anything
|
||||
|
Reference in New Issue
Block a user