Skip to content

🎥 Make videos programmatically with React

nicer notifications

Jonny Burgerba10438b3af2
diff --git a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
index 4a30c63fd5..9b8e6e0203 100644
--- a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
+++ b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
@@ -268,6 +268,9 @@ const DuplicateCompositionLoaded: React.FC<{
const trigger = useCallback(() => {
setSubmitting(true);
+ setSelectedModal(null);
+ const notification = showNotification('Duplicating...', null);
+
applyCodemod({
codemod: {
type: 'duplicate-composition',
@@ -280,11 +283,10 @@ const DuplicateCompositionLoaded: React.FC<{
},
})
.then(() => {
- showNotification('Duplicated composition', 2000);
- setSelectedModal(null);
+ notification.replaceContent(`Created "${newId}" composition`, 2000);
})
.catch((err) => {
- showNotification(
+ notification.replaceContent(
`Could not duplicate composition: ${err.message}`,
2000,
);
diff --git a/packages/studio/src/components/Notifications/Notification.tsx b/packages/studio/src/components/Notifications/Notification.tsx
index b9ebeb7f85..b69073343e 100644
--- a/packages/studio/src/components/Notifications/Notification.tsx
+++ b/packages/studio/src/components/Notifications/Notification.tsx
@@ -18,11 +18,15 @@ const notification: React.CSSProperties = {
export const Notification: React.FC<{
children: React.ReactNode;
created: number;
- duration: number;
+ duration: number | null;
id: string;
onRemove: (id: string) => void;
}> = ({children, id, duration, created, onRemove}) => {
useEffect(() => {
+ if (duration === null) {
+ return;
+ }
+
const timeout = setTimeout(
() => {
onRemove(id);
diff --git a/packages/studio/src/components/Notifications/NotificationCenter.tsx b/packages/studio/src/components/Notifications/NotificationCenter.tsx
index a37c9b2052..7ce3d1b1e4 100644
--- a/packages/studio/src/components/Notifications/NotificationCenter.tsx
+++ b/packages/studio/src/components/Notifications/NotificationCenter.tsx
@@ -22,20 +22,27 @@ type TNotification = {
id: string;
content: React.ReactNode;
created: number;
- duration: number;
+ duration: number | null;
+};
+
+type CreatedNotification = {
+ replaceContent: (
+ newContent: React.ReactNode,
+ durationInMs: number | null,
+ ) => void;
};
type TNotificationCenter = {
- addNotification: (notification: TNotification) => void;
+ addNotification: (notification: TNotification) => CreatedNotification;
};
const notificationCenter = createRef<TNotificationCenter>();
export const showNotification = (
content: React.ReactNode,
- durationInMs: number,
+ durationInMs: number | null,
) => {
- notificationCenter.current?.addNotification({
+ return (notificationCenter.current as TNotificationCenter).addNotification({
content,
duration: durationInMs,
created: Date.now(),
@@ -52,11 +59,36 @@ export const NotificationCenter: React.FC = () => {
});
}, []);
- const addNotification = useCallback((notification: TNotification) => {
- setNotifications((previousNotifications) => {
- return [...previousNotifications, notification];
- });
- }, []);
+ const addNotification = useCallback(
+ (notification: TNotification): CreatedNotification => {
+ setNotifications((previousNotifications) => {
+ return [...previousNotifications, notification];
+ });
+
+ return {
+ replaceContent: (
+ newContent: React.ReactNode,
+ durationInMs: number | null,
+ ) => {
+ setNotifications((oldNotifications) => {
+ return oldNotifications.map((notificationToMap) => {
+ if (notificationToMap.id === notification.id) {
+ return {
+ ...notificationToMap,
+ duration: durationInMs,
+ content: newContent,
+ created: Date.now(),
+ };
+ }
+
+ return notificationToMap;
+ });
+ });
+ },
+ };
+ },
+ [],
+ );
useImperativeHandle(
notificationCenter,