Skip to content

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

feat: add announcement

thecodrr0a9fa7768815
diff --git a/apps/web/src/components/announcement/index.js b/apps/web/src/components/announcement/index.js
new file mode 100644
index 000000000..78613f7f3
--- /dev/null
+++ b/apps/web/src/components/announcement/index.js
@@ -0,0 +1,54 @@
+import React from "react";
+import { Button, Flex, Text } from "rebass";
+import * as Icon from "../icons";
+import useAnnouncement from "../../utils/useAnnouncement";
+import { hashNavigate } from "../../navigation";
+
+function Announcement() {
+ const [announcement, removeAnnouncement] = useAnnouncement();
+ if (!announcement) return null;
+ return (
+ <Flex
+ bg={"shade"}
+ // onClick={reminder?.action}
+ flexDirection="column"
+ p={2}
+ >
+ <Flex flex="1" justifyContent="space-between" alignItems="center">
+ <Text variant="title">{announcement.title}</Text>
+ <Icon.Close
+ size={20}
+ color="primary"
+ onClick={() => removeAnnouncement()}
+ sx={{ cursor: "pointer" }}
+ />
+ </Flex>
+
+ {announcement && <Text variant="body">{announcement.description}</Text>}
+
+ {announcement.cta && (
+ <Button
+ m={0}
+ mt={1}
+ p={1}
+ onClick={() => {
+ switch (announcement.cta.type) {
+ case "link":
+ window.open(announcement.cta.action, "_blank");
+ break;
+ case "promo":
+ hashNavigate(`/login?redirect=/buy/${announcement.cta.action}`);
+ break;
+ default:
+ return;
+ }
+ }}
+ >
+ {announcement.cta.text}
+ </Button>
+ )}
+ </Flex>
+ );
+}
+
+export default Announcement;
diff --git a/apps/web/src/components/list-container/index.js b/apps/web/src/components/list-container/index.js
index 5743d59f5..15727be0c 100644
--- a/apps/web/src/components/list-container/index.js
+++ b/apps/web/src/components/list-container/index.js
@@ -9,6 +9,7 @@ import GroupHeader from "../group-header";
import ListProfiles from "../../common/list-profiles";
import ScrollContainer from "../scroll-container";
import ReminderBar from "../reminder-bar";
+import Announcement from "../announcement";
const CustomScrollbarsVirtualList = React.forwardRef((props, ref) => (
<ScrollContainer {...props} forwardedRef={ref} />
@@ -43,6 +44,7 @@ function ListContainer(props) {
</Flex>
) : (
<>
+ <Announcement />
<ReminderBar />
<Flex variant="columnFill" data-test-id="note-list">
{props.children
@@ -52,7 +54,7 @@ function ListContainer(props) {
{({ height, width }) => (
<List
ref={listRef}
- height={height - 1} // TODO: we have to subtract 1 from total height so scrollbar doesn't appear (not tested widely)
+ height={height}
width={width}
itemKey={(index) => {
switch (index) {
@@ -78,46 +80,59 @@ function ListContainer(props) {
itemCount={props.items.length}
>
{({ index, style }) => {
+ //const item = pinnedItems[index] || props.items[pinnedItems.length + index];
const item = props.items[index];
if (!item) return null;
- return (
- <div
- key={item.id || item.title}
- style={{
- ...style,
- zIndex: item.type === "header" ? 2 : 1,
- }}
- >
- {item.type === "header" ? (
- <GroupHeader
- title={item.title}
- index={index}
- groups={props.items.filter(
- (v) =>
- v.type === "header" &&
- v.title !== item.title
- )}
- wasJumpedTo={index === jumpToIndex}
- onJump={(title) => {
- const index = props.items.findIndex(
- (v) => v.title === title
- );
- if (index < 0) return;
- setJumpToIndex(index);
- listRef.current.scrollToItem(
- index,
- "center"
- );
- setTimeout(() => {
- setJumpToIndex(-1);
- }, 1900);
+
+ switch (item.type) {
+ case "header":
+ return (
+ <div
+ key={item.id || item.title}
+ style={{
+ ...style,
+ zIndex: 2,
+ }}
+ >
+ <GroupHeader
+ title={item.title}
+ index={index}
+ groups={props.items.filter(
+ (v) =>
+ v.type === "header" &&
+ v.title !== item.title
+ )}
+ wasJumpedTo={index === jumpToIndex}
+ onJump={(title) => {
+ const index = props.items.findIndex(
+ (v) => v.title === title
+ );
+ if (index < 0) return;
+ setJumpToIndex(index);
+ listRef.current.scrollToItem(
+ index,
+ "center"
+ );
+ setTimeout(() => {
+ setJumpToIndex(-1);
+ }, 1900);
+ }}
+ />
+ </div>
+ );
+ default:
+ return (
+ <div
+ key={item.id || item.title}
+ style={{
+ ...style,
+ zIndex: 1,
}}
- />
- ) : (
- profile.item(index, item, context)
- )}
- </div>
- );
+ >
+ {profile.item(index, item, context)}
+ </div>
+ );
+ }
}}
</List>
)}
diff --git a/apps/web/src/utils/useAnnouncement.js b/apps/web/src/utils/useAnnouncement.js
new file mode 100644
index 000000000..8ba5d9f27
--- /dev/null
+++ b/apps/web/src/utils/useAnnouncement.js
@@ -0,0 +1,66 @@
+import { useCallback, useEffect, useState } from "react";
+import { isUserPremium, SUBSCRIPTION_STATUS } from "../common";
+import { db } from "../common/db";
+import Config from "./config";
+import { store as userstore } from "../stores/user-store";
+
+var CACHED_ANNOUNCEMENT;
+export default function useAnnouncement() {
+ const [announcement, setAnnouncement] = useState();
+
+ useEffect(() => {
+ (async function () {
+ CACHED_ANNOUNCEMENT = CACHED_ANNOUNCEMENT || (await db.announcement());
+
+ if (
+ !CACHED_ANNOUNCEMENT ||
+ Config.get(CACHED_ANNOUNCEMENT.id) ||
+ !shouldShowAnnouncement(CACHED_ANNOUNCEMENT)
+ )
+ return;
+
+ setAnnouncement(CACHED_ANNOUNCEMENT);
+ })();
+ }, []);
+
+ const remove = useCallback(() => {
+ Config.set(announcement.id, "removed");
+ setAnnouncement();
+ }, [announcement]);
+ return [announcement, remove];
+}
+
+const allowedPlatforms = ["all", "web", "desktop"];
+function shouldShowAnnouncement(announcement) {
+ let show = allowedPlatforms.indexOf(announcement.platform) > -1;
+ if (!show) return;
+
+ const subStatus = userstore.get().user?.subscription?.type;
+ switch (announcement.userType) {
+ case "pro":
+ show = isUserPremium();
+ break;
+ case "trial":
+ show = subStatus === SUBSCRIPTION_STATUS.TRIAL;
+ break;
+ case "trialExpired":
+ show = subStatus === SUBSCRIPTION_STATUS.BASIC;
+ break;
+ case "loggedOut":
+ show = !userstore.get().isLoggedIn;
+ break;
+ case "unverified":
+ show = !userstore.get().user?.isEmailVerified;
+ break;
+ case "proExpired":
+ show =
+ subStatus === SUBSCRIPTION_STATUS.PREMIUM_EXPIRED ||
+ subStatus === SUBSCRIPTION_STATUS.PREMIUM_CANCELED;
+ break;
+ case "any":
+ default:
+ break;
+ }
+
+ return show;
+}
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index 7edea09c5..f184274a7 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -9223,7 +9223,7 @@ notes-core@../notes-core/:
"notes-core@git+https://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:[email protected]/streetwriters/notesnook-core.git":
version "6.6.4"
- resolved "git+https://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:[email protected]/streetwriters/notesnook-core.git#9eb15a9484763dbee9a3a7715374d266f974bda7"
+ resolved "git+https://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:[email protected]/streetwriters/notesnook-core.git#11e5bf6838e79912ef68328025b2f1cd88eccaf9"
dependencies:
fast-sort "^2.0.1"
fuzzysort "^1.1.4"