Skip to content

Commit

Permalink
refactor: Converted ReleaseNotes to ReleaseNotesModal
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkoelle committed May 16, 2021
1 parent 69f55b8 commit bb3f884
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 36 deletions.
16 changes: 1 addition & 15 deletions app/containers/TitleBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useSelector } from 'react-redux';
import { useTheme, Snackbar } from '@material-ui/core';
import { remote } from 'electron';
import { Alert } from '@material-ui/lab';
import ReleaseNotes from '../components/ReleaseNotes';
import {
selectRecentPaths,
selectWorkspacePath,
Expand Down Expand Up @@ -48,11 +47,6 @@ export default function TitleBar(props: TitleBarProps) {
const [maximized, setMaximized] = useState(currentWindow.isMaximized());
const [openFileError, setOpenFileError] = useState<boolean>(false);
const [openExportDialog, setOpenExportDialog] = useState<boolean>(false);
const [versionInfo, setVersionInfo] = useState({
releaseNotes: '',
releaseName: '',
});
const [openReleaseNotes, setOpenReleaseNotes] = useState(false);
// Only for force reloading menu
const [, setBackupPaths] = useState<string[]>([]);

Expand Down Expand Up @@ -122,9 +116,7 @@ export default function TitleBar(props: TitleBarProps) {
setOpenExportDialog,
setExportSheetId,
setReload,
setOpenUpdater,
setOpenReleaseNotes,
setVersionInfo
setOpenUpdater
) as any
}
theme={{
Expand Down Expand Up @@ -167,12 +159,6 @@ export default function TitleBar(props: TitleBarProps) {
>
{/* custom titlebar items */}
</FramelessTitlebar>
<ReleaseNotes
open={openReleaseNotes}
title={versionInfo?.releaseName}
releaseNotes={versionInfo?.releaseNotes}
handleClose={() => setOpenReleaseNotes(false)}
/>
<ExportDialog
open={openExportDialog}
handleClose={() => {
Expand Down
15 changes: 4 additions & 11 deletions app/menu/HelpMenu.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MenuItemConstructorOptions, remote, shell } from 'electron';
import * as Path from 'path';
import ReleaseNotesModal from '../modals/ReleaseNotesModal';

const buildHelpMenu = (
setOpenUpdater,
setOpenReleaseNotes,
setVersionInfo
showModal,
setOpenUpdater
): MenuItemConstructorOptions => {
return {
label: 'Help',
Expand All @@ -18,14 +18,7 @@ const buildHelpMenu = (
{
label: 'View Release Notes',
async click() {
setOpenReleaseNotes(true);
const info = await remote
.require('electron-updater')
.autoUpdater.checkForUpdates();
if (info === undefined) {
setOpenReleaseNotes(false);
}
setVersionInfo(info.versionInfo);
showModal(ReleaseNotesModal);
},
},
{ type: 'separator' },
Expand Down
6 changes: 2 additions & 4 deletions app/menu/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const buildMenu = (
setOpenExportDialog,
setExportSheetId,
setReload,
setOpenUpdater,
setOpenReleaseNotes,
setVersionInfo
setOpenUpdater
) => {
return [
buildFileMenu(
Expand All @@ -32,7 +30,7 @@ const buildMenu = (
setExportSheetId
),
buildViewMenu(dispatch, settings, setReload),
buildHelpMenu(setOpenUpdater, setOpenReleaseNotes, setVersionInfo),
buildHelpMenu(showModal, setOpenUpdater),
];
};

Expand Down
8 changes: 2 additions & 6 deletions app/modals/ModalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ModalProps {
}

const ModalContext = createContext<
<T>(component: FC<T & ModalProps>, options: T) => void
<T>(component: FC<T & ModalProps>, options?: T) => void
>(() => undefined);

export default function ModalProvider(props: ModalProviderProps) {
Expand All @@ -36,11 +36,7 @@ export default function ModalProvider(props: ModalProviderProps) {
<ModalContext.Provider value={openModal}>
{children}
{Modal !== null && modalOptions !== null && (
<Modal
open={Boolean(modalOptions)}
close={closeModal}
{...modalOptions}
/>
<Modal open={Boolean(Modal)} close={closeModal} {...modalOptions} />
)}
</ModalContext.Provider>
);
Expand Down
115 changes: 115 additions & 0 deletions app/modals/ReleaseNotesModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* eslint-disable promise/always-return */
/* eslint-disable react/no-danger */
/* eslint-disable react/prop-types */
/* eslint-disable react/jsx-props-no-spreading */
import {
CircularProgress,
Dialog,
DialogContent,
DialogTitle,
Grid,
Typography,
useTheme,
} from '@material-ui/core';
import { remote } from 'electron';
import { UpdateCheckResult, UpdateInfo } from 'electron-updater';
import React, { FC, useEffect, useState } from 'react';
import { ModalProps } from './ModalProvider';

type ReleaseNotesModalProps = ModalProps;

const disableLinks = (str, theme) => {
let result = '';
const temp = str.toString().split('\n');
if (temp) {
const tempRest = temp.slice(1);
result = tempRest
.join('\n')
.replaceAll(
'<a href',
`<span style='color:${theme.palette.text.disabled};' id`
)
.replaceAll('</a>', '</span>');
}
return result;
};

const ReleaseNotesModal: FC<ReleaseNotesModalProps> = ({ ...props }) => {
const { close } = props;
const theme = useTheme();
const [updateInfo, setUpdateInfo] = useState<UpdateInfo | undefined>();
const [loading, setLoading] = useState<boolean>(true);
const info: Promise<UpdateCheckResult> = remote
.require('electron-updater')
.autoUpdater.checkForUpdates();

useEffect(() => {
info
.then((res: UpdateCheckResult) => {
setUpdateInfo(res.updateInfo);
setLoading(false);
})
.catch(() => {
setLoading(false);
});
}, []);

let content: JSX.Element;
if (loading) {
// Loading
content = (
<DialogContent>
<Grid
container
justify="center"
alignItems="center"
style={{ height: '200px' }}
>
<Grid item>
<CircularProgress />
</Grid>
</Grid>
</DialogContent>
);
} else if (updateInfo) {
// Update Info received
content = (
<>
<DialogTitle disableTypography>
<Typography variant="h5">{`Version ${updateInfo?.version}`}</Typography>
</DialogTitle>
<DialogContent dividers>
<div
dangerouslySetInnerHTML={{
__html: disableLinks(updateInfo?.releaseNotes, theme),
}}
/>
</DialogContent>
</>
);
} else {
// Error
content = (
<DialogContent>
<Grid
container
justify="center"
alignItems="center"
style={{ height: '200px' }}
>
<Grid item>
<Typography>Error loading release notes!</Typography>
</Grid>
</Grid>
</DialogContent>
);
}

return (
<Dialog {...props} onClose={close} fullWidth>
{content}
</Dialog>
);
};

export default ReleaseNotesModal;
1 change: 1 addition & 0 deletions app/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as IPCConstants from './constants/ipc';
export default class AppUpdater {
constructor() {
autoUpdater.autoDownload = false;
// autoUpdater.fullChangelog = true;

ipcMain.on(
IPCConstants.CHECK_FOR_UPDATE_PENDING,
Expand Down

0 comments on commit bb3f884

Please sign in to comment.