forked from laurent22/joplin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Desktop: Seamless-Updates - creation of update notification (laurent2…
- Loading branch information
1 parent
24731ed
commit 88b3c7f
Showing
7 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/app-desktop/gui/UpdateNotification/UpdateNotification.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as React from 'react'; | ||
import { useCallback, useContext, useEffect, useMemo, useRef } from 'react'; | ||
import { themeStyle } from '@joplin/lib/theme'; | ||
import NotyfContext from '../NotyfContext'; | ||
import { UpdateInfo } from 'electron-updater'; | ||
import { ipcRenderer, IpcRendererEvent } from 'electron'; | ||
import { AutoUpdaterEvents } from '../../services/autoUpdater/AutoUpdaterService'; | ||
import { NotyfNotification } from 'notyf'; | ||
import { _ } from '@joplin/lib/locale'; | ||
import { htmlentities } from '@joplin/utils/html'; | ||
|
||
interface UpdateNotificationProps { | ||
themeId: number; | ||
} | ||
|
||
export enum UpdateNotificationEvents { | ||
ApplyUpdate = 'apply-update', | ||
Dismiss = 'dismiss-update-notification', | ||
} | ||
|
||
const changelogLink = 'https://github.com/laurent22/joplin/releases'; | ||
|
||
window.openChangelogLink = () => { | ||
ipcRenderer.send('open-link', changelogLink); | ||
}; | ||
|
||
const UpdateNotification = ({ themeId }: UpdateNotificationProps) => { | ||
const notyfContext = useContext(NotyfContext); | ||
const notificationRef = useRef<NotyfNotification | null>(null); // Use ref to hold the current notification | ||
|
||
const theme = useMemo(() => themeStyle(themeId), [themeId]); | ||
|
||
const notyf = useMemo(() => { | ||
const output = notyfContext; | ||
output.options.types = notyfContext.options.types.map(type => { | ||
if (type.type === 'success') { | ||
type.background = theme.backgroundColor5; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied | ||
(type.icon as any).color = theme.backgroundColor5; | ||
} | ||
return type; | ||
}); | ||
return output; | ||
}, [notyfContext, theme]); | ||
|
||
const handleDismissNotification = useCallback(() => { | ||
notyf.dismiss(notificationRef.current); | ||
notificationRef.current = null; | ||
}, [notyf]); | ||
|
||
const handleApplyUpdate = useCallback(() => { | ||
ipcRenderer.send('apply-update-now'); | ||
handleDismissNotification(); | ||
}, [handleDismissNotification]); | ||
|
||
|
||
const handleUpdateDownloaded = useCallback((_event: IpcRendererEvent, info: UpdateInfo) => { | ||
if (notificationRef.current) return; | ||
|
||
const updateAvailableHtml = htmlentities(_('A new update (%s) is available', info.version)); | ||
const seeChangelogHtml = htmlentities(_('See changelog')); | ||
const restartNowHtml = htmlentities(_('Restart now')); | ||
const updateLaterHtml = htmlentities(_('Update later')); | ||
|
||
const messageHtml = ` | ||
<div class="update-notification" style="color: ${theme.color2};"> | ||
${updateAvailableHtml} <a href="#" onclick="openChangelogLink()" style="color: ${theme.color2};">${seeChangelogHtml}</a> | ||
<div style="display: flex; gap: 10px; margin-top: 8px;"> | ||
<button onclick="document.dispatchEvent(new CustomEvent('${UpdateNotificationEvents.ApplyUpdate}'))" class="notyf__button notyf__button--confirm" style="color: ${theme.color2};">${restartNowHtml}</button> | ||
<button onclick="document.dispatchEvent(new CustomEvent('${UpdateNotificationEvents.Dismiss}'))" class="notyf__button notyf__button--dismiss" style="color: ${theme.color2};">${updateLaterHtml}</button> | ||
</div> | ||
</div> | ||
`; | ||
|
||
const notification: NotyfNotification = notyf.open({ | ||
type: 'success', | ||
message: messageHtml, | ||
position: { | ||
x: 'right', | ||
y: 'bottom', | ||
}, | ||
duration: 0, | ||
}); | ||
|
||
notificationRef.current = notification; | ||
}, [notyf, theme]); | ||
|
||
useEffect(() => { | ||
ipcRenderer.on(AutoUpdaterEvents.UpdateDownloaded, handleUpdateDownloaded); | ||
document.addEventListener(UpdateNotificationEvents.ApplyUpdate, handleApplyUpdate); | ||
document.addEventListener(UpdateNotificationEvents.Dismiss, handleDismissNotification); | ||
|
||
return () => { | ||
ipcRenderer.removeListener(AutoUpdaterEvents.UpdateDownloaded, handleUpdateDownloaded); | ||
document.removeEventListener(UpdateNotificationEvents.ApplyUpdate, handleApplyUpdate); | ||
document.removeEventListener(UpdateNotificationEvents.Dismiss, handleDismissNotification); | ||
}; | ||
}, [handleApplyUpdate, handleDismissNotification, handleUpdateDownloaded]); | ||
|
||
|
||
return ( | ||
<div style={{ display: 'none' }}/> | ||
); | ||
}; | ||
|
||
export default UpdateNotification; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.update-notification { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
|
||
.button-container { | ||
display: flex; | ||
gap: 10px; | ||
margin-top: 8px; | ||
} | ||
|
||
.notyf__button { | ||
padding: 5px 10px; | ||
border: 1px solid; | ||
border-radius: 4px; | ||
background-color: transparent; | ||
cursor: pointer; | ||
|
||
&:hover { | ||
background-color: rgba(255, 255, 255, 0.2); | ||
} | ||
} | ||
|
||
a { | ||
text-decoration: underline; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters