Skip to content

Commit

Permalink
Add notification queue and cleanup toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
UdaraJay committed Nov 12, 2023
1 parent 60e04a4 commit 0354ec9
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 280 deletions.
12 changes: 9 additions & 3 deletions src/renderer/context/LinksContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ export const LinksContextProvider = ({ children }) => {
url
);

addNotification(url, 'thinking', 'Generating preview...');

// return cached preview if available
if (preview) {
return preview;
}

updateNotification(url, 'waiting', 'Generating preview...');
addNotification({
id: url,
type: 'waiting',
message: 'Creating link preview',
});

// otherwise generate a new preview
const _preview = await getPreview(url);
updateNotification(url, 'thinking', 'Generating preview...');
const aiCard = await generateMeta(url).catch(() => {
console.log(
'Failed to generate AI link preview, a basic preview will be used.'
Expand All @@ -59,6 +63,8 @@ export const LinksContextProvider = ({ children }) => {
// cache it
setLink(url, linkPreview);

removeNotification(url);

return linkPreview;
},
[currentPile]
Expand Down
83 changes: 46 additions & 37 deletions src/renderer/context/ToastsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,49 @@ import {
export const ToastsContext = createContext();

export const ToastsContextProvider = ({ children }) => {
// this keeps track of async tasks that the user is notified about
// by the AI. This can include general app notifications as well.
const [notifications, setNotifications] = useState([]);
const notificationTimeoutsRef = useRef({});
const [notificationsQueue, setNotificationsQueue] = useState([]);

const notificationTimeoutRef = useRef();

const processQueue = () => {
if (notificationsQueue.length > 0) {
// Set a timeout to dismiss the first notification
notificationTimeoutRef.current = setTimeout(() => {
setNotificationsQueue((currentQueue) => currentQueue.slice(1));
}, notificationsQueue[0].dismissTime || 5000); // Default 5 seconds
}
};

useEffect(() => {
// Clear any existing timeouts
if (notificationTimeoutRef.current) {
clearTimeout(notificationTimeoutRef.current);
notificationTimeoutRef.current = null;
}

processQueue();

// Clean up timeout on unmount
return () => {
Object.values(notificationTimeoutsRef.current).forEach(clearTimeout);
if (notificationTimeoutRef.current) {
clearTimeout(notificationTimeoutRef.current);
}
};
}, []);
}, [notificationsQueue]);

const addNotification = (
targetId,
const addNotification = ({
id,
type = 'info',
message,
autoDismiss = true
) => {
const newNotification = { id: targetId, type, message, autoDismiss };

setNotifications((currentNotifications) => [
...currentNotifications,
newNotification,
]);

if (autoDismiss) {
if (notificationTimeoutsRef.current[targetId]) {
clearTimeout(notificationTimeoutsRef.current[targetId]);
}

notificationTimeoutsRef.current[targetId] = setTimeout(() => {
removeNotification(targetId);
delete notificationTimeoutsRef.current[targetId];
}, 30000);
}
dismissTime = 5000,
}) => {
const newNotification = { id, type, message, dismissTime };
setNotificationsQueue((currentQueue) => [...currentQueue, newNotification]);
};

const updateNotification = (targetId, newType, newMessage) => {
setNotifications((currentNotifications) =>
currentNotifications.map((notification) =>
setNotificationsQueue((currentQueue) =>
currentQueue.map((notification) =>
notification.id === targetId
? { ...notification, type: newType, message: newMessage }
: notification
Expand All @@ -57,19 +61,24 @@ export const ToastsContextProvider = ({ children }) => {
};

const removeNotification = (targetId) => {
setNotifications((currentNotifications) =>
currentNotifications.filter(
(notification) => notification.id !== targetId
)
setNotificationsQueue((currentQueue) =>
currentQueue.filter((notification) => notification.id !== targetId)
);
if (notificationTimeoutsRef.current[targetId]) {
clearTimeout(notificationTimeoutsRef.current[targetId]);
delete notificationTimeoutsRef.current[targetId];

// If the notification being removed is the first in the queue, we need to clear the timeout
// and process the next notification if available
if (
notificationsQueue.length > 0 &&
notificationsQueue[0].id === targetId
) {
clearTimeout(notificationTimeoutRef.current);
notificationTimeoutRef.current = null;
processQueue();
}
};

const ToastsContextValue = {
notifications,
notifications: notificationsQueue,
addNotification,
updateNotification,
removeNotification,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/pages/Pile/Highlights/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Dialog from '@radix-ui/react-dialog';
import { useAIContext } from 'renderer/context/AIContext';
import { useHighlightsContext } from 'renderer/context/HighlightsContext';

// UNDER CONSTRUCTION
export default function HighlightsDialog() {
const { open, onOpenChange } = useHighlightsContext();

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/Pile/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ export default function PileLayout({ children }) {
{pileName} <span style={{ padding: '6px' }}>·</span> {now}
</div>
<div className={styles.right}>
<Toasts />
<Settings />
<HighlightsDialog />
<Link to="/" className={`${styles.iconHolder}`}>
<HomeIcon className={styles.homeIcon} />
</Link>
{/* <HighlightsDialog /> */}
</div>
</div>
{children}
</div>
</div>
<div id="dialog"></div>
<Toasts />
</div>
);
}
3 changes: 1 addition & 2 deletions src/renderer/pages/Pile/PileLayout.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,13 @@
align-items: center;
justify-content: center;
height: 32px;
width: 36px;
min-width: 36px;
border-radius: 8px;
transition: all ease-in-out 120ms;
-webkit-app-region: none;
background: transparent;
margin-right: -2px;


&:last-child {
margin-right: 8px;
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/pages/Pile/Settings/Settings.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
align-items: center;
justify-content: center;
height: 32px;
width: 36px;
min-width: 36px;
border-radius: 8px;
transition: all ease-in-out 120ms;
-webkit-app-region: none;
Expand Down
4 changes: 0 additions & 4 deletions src/renderer/pages/Pile/Settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ export default function Settings() {
className={styles.color1}
style={{ background: colors.primary }}
></div>
{/* <div
className={styles.color2}
style={{ background: colors.secondary }}
></div> */}
</button>
);
});
Expand Down
59 changes: 59 additions & 0 deletions src/renderer/pages/Pile/Toasts/Toast/Loaders/Info/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export default function Info(props) {
return (
<svg
{...props}
xmlns="http://www.w3.org/2000/svg"
width="44"
height="44"
viewBox="0 0 44 44"
stroke="currentcolor"
>
<g fill="none" fill-rule="evenodd" stroke-width="2">
<circle cx="22" cy="22" r="1">
<animate
attributeName="r"
begin="0s"
dur="1.8s"
values="1; 20"
calcMode="spline"
keyTimes="0; 1"
keySplines="0.165, 0.84, 0.44, 1"
repeatCount="indefinite"
/>
<animate
attributeName="stroke-opacity"
begin="0s"
dur="1.8s"
values="1; 0"
calcMode="spline"
keyTimes="0; 1"
keySplines="0.3, 0.61, 0.355, 1"
repeatCount="indefinite"
/>
</circle>
<circle cx="22" cy="22" r="1">
<animate
attributeName="r"
begin="-0.9s"
dur="1.8s"
values="1; 20"
calcMode="spline"
keyTimes="0; 1"
keySplines="0.165, 0.84, 0.44, 1"
repeatCount="indefinite"
/>
<animate
attributeName="stroke-opacity"
begin="-0.9s"
dur="1.8s"
values="1; 0"
calcMode="spline"
keyTimes="0; 1"
keySplines="0.3, 0.61, 0.355, 1"
repeatCount="indefinite"
/>
</circle>
</g>
</svg>
);
}
11 changes: 11 additions & 0 deletions src/renderer/pages/Pile/Toasts/Toast/Loaders/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The SVG loading animations used within Pile are from: https://github.com/SamHerbert/SVG-Loaders

The MIT License (MIT)

Copyright (c) 2014 Sam Herbert

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 changes: 33 additions & 0 deletions src/renderer/pages/Pile/Toasts/Toast/Loaders/Thinking/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default function Thinking(props) {
return (
<svg
{...props}
xmlns="http://www.w3.org/2000/svg"
width="135"
height="135"
viewBox="0 0 135 135"
fill="currentcolor"
>
<path d="M67.447 58c5.523 0 10-4.477 10-10s-4.477-10-10-10-10 4.477-10 10 4.477 10 10 10zm9.448 9.447c0 5.523 4.477 10 10 10 5.522 0 10-4.477 10-10s-4.478-10-10-10c-5.523 0-10 4.477-10 10zm-9.448 9.448c-5.523 0-10 4.477-10 10 0 5.522 4.477 10 10 10s10-4.478 10-10c0-5.523-4.477-10-10-10zM58 67.447c0-5.523-4.477-10-10-10s-10 4.477-10 10 4.477 10 10 10 10-4.477 10-10z">
<animateTransform
attributeName="transform"
type="rotate"
from="0 67 67"
to="-360 67 67"
dur="2.5s"
repeatCount="indefinite"
/>
</path>
<path d="M28.19 40.31c6.627 0 12-5.374 12-12 0-6.628-5.373-12-12-12-6.628 0-12 5.372-12 12 0 6.626 5.372 12 12 12zm30.72-19.825c4.686 4.687 12.284 4.687 16.97 0 4.686-4.686 4.686-12.284 0-16.97-4.686-4.687-12.284-4.687-16.97 0-4.687 4.686-4.687 12.284 0 16.97zm35.74 7.705c0 6.627 5.37 12 12 12 6.626 0 12-5.373 12-12 0-6.628-5.374-12-12-12-6.63 0-12 5.372-12 12zm19.822 30.72c-4.686 4.686-4.686 12.284 0 16.97 4.687 4.686 12.285 4.686 16.97 0 4.687-4.686 4.687-12.284 0-16.97-4.685-4.687-12.283-4.687-16.97 0zm-7.704 35.74c-6.627 0-12 5.37-12 12 0 6.626 5.373 12 12 12s12-5.374 12-12c0-6.63-5.373-12-12-12zm-30.72 19.822c-4.686-4.686-12.284-4.686-16.97 0-4.686 4.687-4.686 12.285 0 16.97 4.686 4.687 12.284 4.687 16.97 0 4.687-4.685 4.687-12.283 0-16.97zm-35.74-7.704c0-6.627-5.372-12-12-12-6.626 0-12 5.373-12 12s5.374 12 12 12c6.628 0 12-5.373 12-12zm-19.823-30.72c4.687-4.686 4.687-12.284 0-16.97-4.686-4.686-12.284-4.686-16.97 0-4.687 4.686-4.687 12.284 0 16.97 4.686 4.687 12.284 4.687 16.97 0z">
<animateTransform
attributeName="transform"
type="rotate"
from="0 67 67"
to="360 67 67"
dur="8s"
repeatCount="indefinite"
/>
</path>
</svg>
);
}
28 changes: 28 additions & 0 deletions src/renderer/pages/Pile/Toasts/Toast/Loaders/Waiting/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default function Waiting(props) {
return (
<svg
{...props}
xmlns="http://www.w3.org/2000/svg"
width="38"
height="38"
viewBox="0 0 38 38"
stroke="currentcolor"
>
<g fill="none" fill-rule="evenodd">
<g transform="translate(1 1)" stroke-width="3">
<circle stroke-opacity=".5" cx="18" cy="18" r="18" />
<path d="M36 18c0-9.94-8.06-18-18-18">
<animateTransform
attributeName="transform"
type="rotate"
from="0 18 18"
to="360 18 18"
dur="1s"
repeatCount="indefinite"
/>
</path>
</g>
</g>
</svg>
);
}
Loading

0 comments on commit 0354ec9

Please sign in to comment.