Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: add dialog announcement

thecodrrf911f4ad6349
diff --git a/apps/web/src/app-effects.js b/apps/web/src/app-effects.js
index 38c7fc4b7..36978a54b 100644
--- a/apps/web/src/app-effects.js
+++ b/apps/web/src/app-effects.js
@@ -14,9 +14,9 @@ import { CHECK_IDS, EV, EVENTS } from "notes-core/common";
import { registerKeyMap } from "./common/key-map";
import { isUserPremium } from "./hooks/use-is-user-premium";
import { loadTrackerScript } from "./utils/analytics";
-import Modal from "react-modal";
+import useAnnouncements from "./utils/use-announcements";
+import { showAnnouncementDialog } from "./common/dialog-controller";
-Modal.setAppElement("#root");
if (process.env.NODE_ENV === "production") {
loadTrackerScript();
console.log = () => {};
@@ -33,6 +33,7 @@ export default function AppEffects({ setShow }) {
const initUser = useUserStore((store) => store.init);
const initNotes = useNotesStore((store) => store.init);
const setIsVaultCreated = useStore((store) => store.setIsVaultCreated);
+ const [announcements, remove] = useAnnouncements("dialog");
useEffect(
function initializeApp() {
@@ -128,6 +129,13 @@ export default function AppEffects({ setShow }) {
};
}, []);
+ useEffect(() => {
+ if (!announcements.length) return;
+ (async () => {
+ await showAnnouncementDialog(announcements[0], remove);
+ })();
+ }, [announcements, remove]);
+
return <React.Fragment />;
}
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 3fd7c486d..1508a4aaf 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -551,3 +551,15 @@ export function showTrackingDetailsDialog() {
<Dialogs.TrackingDetailsDialog onClose={(res) => perform(res)} />
));
}
+
+export function showAnnouncementDialog(announcement, remove) {
+ return showDialog((Dialogs, perform) => (
+ <Dialogs.AnnouncementDialog
+ announcement={announcement}
+ onClose={(res) => {
+ remove(announcement.id);
+ perform(res);
+ }}
+ />
+ ));
+}
diff --git a/apps/web/src/components/announcements/body.js b/apps/web/src/components/announcements/body.js
new file mode 100644
index 000000000..fa24fd47c
--- /dev/null
+++ b/apps/web/src/components/announcements/body.js
@@ -0,0 +1,222 @@
+import { Button, Flex, Image as RebassImage, Text as RebassText } from "rebass";
+import { allowedPlatforms } from "../../utils/use-announcements";
+import { showBuyDialog } from "../../common/dialog-controller";
+import { ANALYTICS_EVENTS, trackEvent } from "../../utils/analytics";
+
+var margins = [0, 2];
+var HORIZONTAL_MARGIN = 3;
+
+export default function AnnouncementBody({ components, type }) {
+ return components.map((item) => {
+ item.style = item.style || {};
+
+ if (type === "inline") {
+ HORIZONTAL_MARGIN = 2;
+ margins = [0, 1];
+ switch (item.type) {
+ case "title":
+ return <Title item={item} />;
+ case "description":
+ return <Description item={item} />;
+ case "callToActions":
+ return <InlineCalltoActions item={item} />;
+ case "text":
+ return (
+ <Text
+ value={item.value}
+ sx={mapStyle(item.style)}
+ mx={HORIZONTAL_MARGIN}
+ />
+ );
+ default:
+ return null;
+ }
+ } else if (type === "dialog") {
+ HORIZONTAL_MARGIN = 3;
+ margins = [0, 2];
+
+ switch (item.type) {
+ case "image":
+ return <Image item={item} />;
+ case "list":
+ return <List item={item} />;
+ case "title":
+ return <Title item={item} />;
+ case "subheading":
+ return <Title item={item} fontSize={"title"} />;
+ case "description":
+ return <Description item={item} />;
+ case "callToActions":
+ return <CalltoActions item={item} />;
+ case "text":
+ return (
+ <Text
+ value={item.value}
+ sx={mapStyle(item.style)}
+ mx={HORIZONTAL_MARGIN}
+ />
+ );
+ default:
+ return null;
+ }
+ }
+ return null;
+ });
+}
+
+function Image({ item }) {
+ const { src, style } = item;
+ return (
+ <RebassImage
+ src={src}
+ maxHeight="150px"
+ sx={{
+ objectFit: "cover",
+ ...mapStyle(style),
+ }}
+ />
+ );
+}
+
+function List({ item }) {
+ const { items, listType, style } = item;
+ return (
+ <Flex
+ as={listType || "ul"}
+ mt={1}
+ sx={mapStyle(style)}
+ flexDirection="column"
+ >
+ {items.map((item) => (
+ <ListItem item={item} />
+ ))}
+ </Flex>
+ );
+}
+
+function ListItem({ item }) {
+ const { text } = item;
+ return <Text as="li" value={text} />;
+}
+
+function Title({ item, fontSize }) {
+ const { text, style } = item;
+ return (
+ <Text
+ value={text}
+ variant="heading"
+ mx={HORIZONTAL_MARGIN}
+ fontSize={fontSize}
+ sx={mapStyle(style)}
+ />
+ );
+}
+
+function Description({ item }) {
+ const { text, style } = item;
+ return (
+ <Text
+ value={text}
+ variant="title"
+ fontWeight="normal"
+ color="fontTertiary"
+ mx={HORIZONTAL_MARGIN}
+ sx={mapStyle(style)}
+ />
+ );
+}
+
+function Text({ value, ...restProps }) {
+ return (
+ <RebassText variant={"body"} {...restProps}>
+ {value}
+ </RebassText>
+ );
+}
+
+function CalltoActions({ item }) {
+ const { actions, style } = item;
+ return (
+ <Flex
+ justifyContent="center"
+ alignItems="center"
+ // bg="bgSecondary"
+ px={2}
+ pb={2}
+ sx={mapStyle(style)}
+ >
+ {actions
+ ?.filter((cta) =>
+ cta.platforms.some(
+ (platform) => allowedPlatforms.indexOf(platform) > -1
+ )
+ )
+ .map((action, index) => (
+ <CalltoAction action={action} index={index} />
+ ))}
+ </Flex>
+ );
+}
+
+function InlineCalltoActions({ item }) {
+ const { actions, style } = item;
+ return (
+ <Flex px={2} sx={mapStyle(style)}>
+ {actions
+ ?.filter((cta) =>
+ cta.platforms.some(
+ (platform) => allowedPlatforms.indexOf(platform) > -1
+ )
+ )
+ .map((action, index) => (
+ <CalltoAction action={action} index={index} />
+ ))}
+ </Flex>
+ );
+}
+
+function CalltoAction({ action, index }) {
+ return (
+ <Button
+ as={action.type === "link" ? "a" : "button"}
+ href={action.type === "link" ? action.data : ""}
+ variant={
+ index === 0
+ ? "primary"
+ : index === 1
+ ? "secondary"
+ : index === 2
+ ? "tertiary"
+ : "shade"
+ }
+ sx={{
+ ":first-of-type": {
+ mr: 1,
+ },
+ }}
+ onClick={async () => {
+ trackEvent(ANALYTICS_EVENTS.announcementCta, action.data);
+ switch (action.type) {
+ case "link":
+ const url = new URL(action.data);
+ if (url.origin === window.location.origin)
+ window.open(action.data, "_self");
+ else window.open(action.data, "_blank");
+ break;
+ case "promo":
+ const [coupon, plan] = action.data.split(":");
+ await showBuyDialog(plan, coupon);
+ break;
+ default:
+ return;
+ }
+ }}
+ >
+ {action.title}
+ </Button>
+ );
+}
+
+function mapStyle(style) {
+ return { mt: margins[style.marginTop], mb: margins[style.marginBottom] };
+}
diff --git a/apps/web/src/components/announcements/index.js b/apps/web/src/components/announcements/index.js
index de72854a8..022354e45 100644
--- a/apps/web/src/components/announcements/index.js
+++ b/apps/web/src/components/announcements/index.js
@@ -1,22 +1,18 @@
-import React, { useMemo } from "react";
-import { Button, Flex, Text } from "rebass";
+import { useMemo } from "react";
+import { Flex, Text } from "rebass";
import * as Icon from "../icons";
-import { showBuyDialog } from "../../common/dialog-controller";
import { ANALYTICS_EVENTS, trackEvent } from "../../utils/analytics";
-import { allowedPlatforms } from "../../utils/use-announcements";
+import AnnouncementBody from "./body";
function Announcements({ announcements, removeAnnouncement }) {
const announcement = useMemo(() => announcements[0], [announcements]);
return (
- <Flex flexDirection="column" p={2} pt={0}>
- <Flex flex="1" justifyContent="space-between" alignItems="center">
+ <Flex flexDirection="column" mt={0} sx={{ borderRadius: "default" }}>
+ <Flex flex="1" justifyContent="space-between" alignItems="center" mx={2}>
<Flex
justifyContent="center"
alignItems="center"
- bg="shade"
- px={1}
- py={"1px"}
sx={{ borderRadius: "default" }}
>
<Icon.Announcement size={12} color="primary" sx={{ mr: "3px" }} />
@@ -32,6 +28,7 @@ function Announcements({ announcements, removeAnnouncement }) {
variant="subBody"
fontWeight="bold"
sx={{
+ borderRadius: "default",
cursor: "pointer",
":hover": { opacity: 0.7 },
}}
@@ -46,66 +43,7 @@ function Announcements({ announcements, removeAnnouncement }) {
Dismiss
</Text>
</Flex>
- <Text variant="heading" mt={1}>
- {announcement.title}
- </Text>
-
- {announcement && (
- <Text variant="body" lineHeight="18px">
- {announcement.description}
- </Text>
- )}
- <Flex justifyContent="space-evenly" mt={1}>
- {announcement.callToActions
- ?.filter((cta) =>
- cta.platforms.some(
- (platform) => allowedPlatforms.indexOf(platform) > -1
- )
- )
- .map((action, index) => (
- <Button
- flex={1}
- m={0}
- p={1}
- as={action.type === "link" ? "a" : "button"}
- href={action.type === "link" ? action.data : ""}
- variant={
- index === 0
- ? "primary"
- : index === 1
- ? "secondary"
- : index === 2
- ? "tertiary"
- : "shade"
- }
- sx={{
- ":first-of-type": {
- mr: 1,
- },
- }}
- fontWeight="bold"
- onClick={async () => {
- trackEvent(ANALYTICS_EVENTS.announcementCta, action.data);
- switch (action.type) {
- case "link":
- const url = new URL(action.data);
- if (url.origin === window.location.origin)
- window.open(action.data, "_self");
- else window.open(action.data, "_blank");
- break;
- case "promo":
- const [coupon, plan] = action.data.split(":");
- await showBuyDialog(plan, coupon);
- break;
- default:
- return;
- }
- }}
- >
- {action.title}
- </Button>
- ))}
- </Flex>
+ <AnnouncementBody components={announcement.body} type="inline" />
</Flex>
);
}
diff --git a/apps/web/src/components/dialogs/announcement-dialog.js b/apps/web/src/components/dialogs/announcement-dialog.js
new file mode 100644
index 000000000..eb25d7a0e
--- /dev/null
+++ b/apps/web/src/components/dialogs/announcement-dialog.js
@@ -0,0 +1,73 @@
+import Modal from "react-modal";
+import { useTheme } from "emotion-theming";
+import { Flex } from "rebass";
+import AnnouncementBody from "../announcements/body";
+
+function AnnouncementDialog(props) {
+ const { announcement } = props;
+ const theme = useTheme();
+
+ return (
+ <Modal
+ isOpen={true}
+ onRequestClose={props.onClose}
+ shouldCloseOnEsc
+ shouldReturnFocusAfterClose
+ shouldFocusAfterRender
+ onAfterOpen={(e) => {
+ if (!props.onClose) return;
+ // we need this work around because ReactModal content spreads over the overlay
+ const child = e.contentEl.firstElementChild;
+ e.contentEl.onmousedown = function (e) {
+ if (!e.screenX && !e.screenY) return;
+ if (
+ e.x < child.offsetLeft ||
+ e.x > child.offsetLeft + child.clientWidth ||
+ e.y < child.offsetTop ||
+ e.y > child.offsetTop + child.clientHeight
+ ) {
+ props.onClose();
+ }
+ };
+ if (props.onOpen) props.onOpen();
+ }}
+ style={{
+ content: {
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0,
+ display: "flex",
+ justifyContent: "center",
+ backgroundColor: undefined,
+ padding: 0,
+ overflowY: "hidden",
+ border: 0,
+ zIndex: 0,
+ },
+ overlay: {
+ zIndex: 999,
+ background: theme.colors.overlay,
+ },
+ }}
+ >
+ <Flex
+ flexDirection="column"
+ width={["100%", "80%", "30%"]}
+ maxHeight={["100%", "80%", "80%"]}
+ bg="background"
+ alignSelf={"center"}
+ overflowY="auto"
+ sx={{
+ position: "relative",
+ overflow: "hidden",
+ boxShadow: "4px 5px 18px 2px #00000038",
+ borderRadius: "dialog",
+ }}
+ >
+ <AnnouncementBody components={announcement.body} type="dialog" />
+ </Flex>
+ </Modal>
+ );
+}
+export default AnnouncementDialog;
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.js
index 870666a9b..638c98e6c 100644
--- a/apps/web/src/components/dialogs/dialog.js
+++ b/apps/web/src/components/dialogs/dialog.js
@@ -1,9 +1,9 @@
-import React from "react";
import { Flex, Text, Button as RebassButton } from "rebass";
import * as Icon from "../icons";
import Modal from "react-modal";
import { useTheme } from "emotion-theming";
+Modal.setAppElement("#root");
function Dialog(props) {
const theme = useTheme();
return (
diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.js
index e8e8d12fa..71cf78949 100644
--- a/apps/web/src/components/dialogs/index.js
+++ b/apps/web/src/components/dialogs/index.js
@@ -13,6 +13,7 @@ import SessionExpiredDialog from "./sessionexpireddialog";
import FeatureDialog from "./feature-dialog";
import TrackingDetailsDialog from "./trackingdetailsdialog";
import ReminderDialog from "./reminderdialog";
+import AnnouncementDialog from "./announcementdialog";
const Dialogs = {
AddNotebookDialog,
@@ -30,5 +31,6 @@ const Dialogs = {
FeatureDialog,
ProgressDialog,
ReminderDialog,
+ AnnouncementDialog,
};
export default Dialogs;
diff --git a/apps/web/src/utils/use-announcements.js b/apps/web/src/utils/use-announcements.js
index 743507fed..4a232d4c6 100644
--- a/apps/web/src/utils/use-announcements.js
+++ b/apps/web/src/utils/use-announcements.js
@@ -6,7 +6,12 @@ import { isUserPremium } from "../hooks/use-is-user-premium";
var CACHED_ANNOUNCEMENTS = [];
var cancelled = false;
-export default function useAnnouncements() {
+/**
+ *
+ * @param {"inline"|"dialog"} type
+ * @returns
+ */
+export default function useAnnouncements(type = "inline") {
const [announcements, setAnnouncements] = useState([]);
useEffect(() => {
@@ -22,8 +27,8 @@ export default function useAnnouncements() {
if (cancelled) return;
let announcements = [];
for (let announcement of CACHED_ANNOUNCEMENTS) {
- if (await shouldShowAnnouncement(announcement))
- announcements.push(announcement);
+ if (!(await shouldShowAnnouncement(announcement))) continue;
+ announcements.push(announcement);
}
setAnnouncements(announcements);
}
@@ -43,7 +48,8 @@ export default function useAnnouncements() {
return copy;
});
}, []);
- return [announcements, remove];
+
+ return [announcements.filter((a) => a.type === type), remove];
}
export const allowedPlatforms = ["all", process.env.REACT_APP_PLATFORM];