Skip to content

Commit

Permalink
feat: better notification management
Browse files Browse the repository at this point in the history
  • Loading branch information
iyzana committed Feb 11, 2023
1 parent c22dcef commit 2122b48
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
30 changes: 21 additions & 9 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,27 @@ function App() {
}>({});

const addNotification = useCallback((notification: Notification) => {
setNotifications((notifications) => [...notifications, notification]);
setNotifications((notifications) => [
...notifications.filter((notification) => !notification.permanent),
notification,
...notifications.filter((notification) => notification.permanent),
]);
}, []);

useEffect(() => {
ws.onclose = () => {
console.log('disconnected');
addNotification({
message: 'Connection lost',
level: 'error',
permanent: true,
});
// wait 200ms before showing connection lost because on site-reload
// firefox first closes the websocket resulting in the error briefly
// showing up when it is not necessary
const timeout = setTimeout(() => {
addNotification({
message: 'Connection lost',
level: 'error',
permanent: true,
});
}, 200);
return () => clearTimeout(timeout);
};
}, [addNotification]);

Expand Down Expand Up @@ -77,17 +87,19 @@ function App() {
}, [addNotification, messageCallbacks]);

useEffect(() => {
if (notifications.length === 0) {
if (!notifications.find((notification) => !notification.permanent)) {
// no non-permanent notifications
return;
}
const timeout = setTimeout(() => {
setNotifications((notifications) => {
const dismissIndex = notifications.findIndex(
(notification) => !notification.permanent,
);
return notifications.splice(dismissIndex, 1);
console.log(`dismissing ${dismissIndex}`);
return dismissIndex === -1 ? notifications : notifications.slice(1);
});
}, 3000);
}, 2000);
return () => clearTimeout(timeout);
}, [notifications]);

Expand Down
25 changes: 24 additions & 1 deletion frontend/src/component/Infobox.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,37 @@
justify-content: space-between;
}

.infobox.info {
.infobox.success {
background-color: #147447;
}

.infobox.info {
background-color: #005187;
}

.infobox.error {
background-color: #880011;
}

.remaining {
background-color: var(--card);
padding: var(--s0);
margin: calc(-1 * var(--s0)) 0;
border-radius: var(--radius);
}

.infobox.success .remaining {
background-color: #0d492c;
}

.infobox.info .remaining {
background-color: #003254;
}

.infobox.error .remaining {
background-color: #55000b;
}

.copylink {
margin: calc(-1 * var(--s0)) 0;
}
17 changes: 12 additions & 5 deletions frontend/src/component/Infobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function Infobox({ notifications, addNotification }: InfoboxProps) {
}
navigator.clipboard.writeText(window.location.href);
addNotification({
message: 'Link copied',
level: 'info',
message: 'Room link copied to clipboard',
level: 'success',
permanent: false,
});
};
Expand All @@ -45,15 +45,22 @@ function Infobox({ notifications, addNotification }: InfoboxProps) {
className={`infobox ${notification != null ? notification.level : ''}`}
>
{notification != null ? (
<span className="">{notifications[0].message}</span>
<>
<span>{notifications[0].message}</span>
{notifications.length > 1 ? (
<span className="remaining">
{notifications.length - 1} remaining
</span>
) : null}
</>
) : (
<>
<span className="connections">
{numUsers === 1 ? 'no one else connected' : `${numUsers} connected`}
{numUsers === 1 ? 'No one else connected' : `${numUsers} connected`}
</span>
<div>
<button className="copylink" onClick={copyLink}>
COPY LINK
Copy room link
</button>
</div>
</>
Expand Down
6 changes: 0 additions & 6 deletions frontend/src/component/Input.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
border-radius: 0 0 var(--radius) var(--radius);
}

.error {
width: 100%;
background-color: #880011;
padding: var(--s0);
}

.input-send {
background-color: unset;
}
6 changes: 3 additions & 3 deletions frontend/src/component/Queue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ function Queue({ addNotification, setWorking }: QueueProps) {
} else if (msg === 'queue err not-found') {
addNotification({
message: 'No video found',
level: 'error',
level: 'info',
permanent: false,
});
setWorking(false);
} else if (msg === 'queue err duplicate') {
addNotification({
message: 'Already in queue',
level: 'error',
level: 'info',
permanent: false,
});
setWorking(false);
Expand Down Expand Up @@ -101,7 +101,7 @@ function Queue({ addNotification, setWorking }: QueueProps) {
<div className="status">
{queue.length === 0 ? null : (
<button className="skip" onClick={skip}>
SKIP <FontAwesomeIcon icon={faAngleDoubleRight} />
Skip <FontAwesomeIcon icon={faAngleDoubleRight} />
</button>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/model/Notification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default interface Notification {
message: string;
level: 'info' | 'error';
level: 'success' | 'info' | 'error';
permanent: boolean;
}

0 comments on commit 2122b48

Please sign in to comment.