Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add error boundary and snackbar store #15

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/renderer/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { toast } from 'react-toastify';
import defaultToastStyleOptions from 'shared/constants';

interface Props {
children: React.ReactNode;
}

interface State {
error: boolean | Error;
}

class ErrorBoundary extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { error: false };
}

static getDerivedStateFromError(error: Error): object {
return { error };
}

// componentDidCatch(error: Error, errorInfo: any): void {
// // add our error logging service here
// // errorLoggingService(error,errorInfo)
// }

render(): React.ReactNode {
const { children } = this.props;
const { error } = this.state;
if (error) {
toast('Something went wrong', defaultToastStyleOptions);
}

return children;
}
}

export default ErrorBoundary;
20 changes: 13 additions & 7 deletions src/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { createRoot } from 'react-dom/client';
import { Provider } from 'react-redux';
import { MemoryRouter as Router } from 'react-router-dom';
import { PersistGate } from 'redux-persist/integration/react';
import { ToastContainer } from 'react-toastify';
import App from './App';
import store, { persistor } from './store/store';
import ErrorBoundary from './components/ErrorBoundary';
import 'react-toastify/dist/ReactToastify.css';

declare global {
interface Window {
Expand All @@ -14,11 +17,14 @@ declare global {
const container = document.getElementById('root') as HTMLElement;
const root = createRoot(container);
root.render(
<Provider store={store}>
<PersistGate persistor={persistor}>
<Router>
<App />
</Router>
</PersistGate>
</Provider>
<ErrorBoundary>
<Provider store={store}>
<PersistGate persistor={persistor}>
<Router>
<App />
</Router>
</PersistGate>
<ToastContainer />
</Provider>
</ErrorBoundary>
);
7 changes: 2 additions & 5 deletions src/renderer/pages/Apps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Id, ToastContainer, ToastOptions, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import Button from 'renderer/components/Button';
import defaultToastStyleOptions from 'shared/constants';
import {
selectInstalledApps,
selectRunningApps,
Expand Down Expand Up @@ -122,12 +123,8 @@
data: { appId: string; progress: Progress; filename: string }
) => {
const TOAST_STYLE: ToastOptions = {
position: 'bottom-center',
...defaultToastStyleOptions,
hideProgressBar: false,
closeOnClick: false,
pauseOnHover: false,
draggable: false,
theme: 'dark',
progressClassName: 'toast-progress-bar',
autoClose: false,
};
Expand Down Expand Up @@ -166,7 +163,7 @@
};

useEffect(() => {
dispatch(setupInstalledApps());

Check warning on line 166 in src/renderer/pages/Apps.tsx

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array
}, []);

return (
Expand Down
12 changes: 9 additions & 3 deletions src/renderer/pages/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
startNode,
stopNode,
} from 'renderer/features/nodeSlice';
import { showSnackbar } from 'renderer/store/snackbar';
import { useAppDispatch, useAppSelector } from 'renderer/utils/hooks';
import { styled } from 'styled-components';
import Spinner from 'renderer/components/Spinner';
Expand Down Expand Up @@ -133,9 +134,14 @@ export default function Navigtion() {
const runningApps = useAppSelector(selectRunningApps);

const handleScreenshot = async () => {
setShowSpinner(true);
await window.electron.ipcRenderer.madara.sendTweet();
setShowSpinner(false);
try {
setShowSpinner(true);
await window.electron.ipcRenderer.madara.sendTweet();
setShowSpinner(false);
} catch (err) {
dispatch(showSnackbar());
setShowSpinner(false);
}
};

return (
Expand Down
39 changes: 39 additions & 0 deletions src/renderer/store/snackbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createSlice } from '@reduxjs/toolkit';
import { toast, ToastOptions } from 'react-toastify';
import defaultToastStyleOptions from 'shared/constants';

const initialState = {
isVisible: false,
};

const snackbarSlice = createSlice({
name: 'snackbar',
initialState,
// The `reducers` field lets us define reducers and generate associated actions
reducers: {
showToast: (state) => {
state.isVisible = true;
},
hideToast: (state) => {
state.isVisible = false;
},
},
});

const { showToast, hideToast } = snackbarSlice.actions;

export const showSnackbar = (message?: string) => (dispatch: any) => {
dispatch(showToast());
const TOAST_STYLE: ToastOptions = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are also using this config over here - https://github.com/apoorvsadana/madara-tsukuyomi/blob/97cd3d568eb86f49075da6ad2fcebb6a75beb7a9/src/renderer/pages/Apps.tsx#L124

can we store the common things in a config?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this

...defaultToastStyleOptions,
autoClose: 2000,
};
toast(message ?? 'Something went wrong', { ...TOAST_STYLE });
};

export const hideSnackbar = () => (dispatch: any) => {
dispatch(hideToast());
toast.dismiss();
};

export default snackbarSlice.reducer;
2 changes: 2 additions & 0 deletions src/renderer/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import storage from 'redux-persist/lib/storage';
import appsReducer from '../features/appsSlice';
import nodeReducer from '../features/nodeSlice';
import walletReducer from '../features/walletSlice';
import snackbarReducer from './snackbar';

const persistConfig = {
key: 'root',
Expand All @@ -16,6 +17,7 @@ const rootReducer = combineReducers({
node: nodeReducer,
wallet: walletReducer,
apps: appsReducer,
snackbar: snackbarReducer,
});

const persistedReducer = persistReducer(persistConfig, rootReducer);
Expand Down
12 changes: 12 additions & 0 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ToastOptions } from 'react-toastify';

const defaultToastStyleOptions: ToastOptions = {
position: 'bottom-center',
hideProgressBar: true,
closeOnClick: false,
pauseOnHover: false,
draggable: false,
theme: 'dark',
};

export default defaultToastStyleOptions;
Loading