Skip to content

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

web: show features usage details in subscription settings

Abdullah Atta25b0503a25fe
diff --git a/apps/web/src/dialogs/settings/components/subscription-status.tsx b/apps/web/src/dialogs/settings/components/subscription-status.tsx
index eec7ec4c1..3a6419170 100644
--- a/apps/web/src/dialogs/settings/components/subscription-status.tsx
+++ b/apps/web/src/dialogs/settings/components/subscription-status.tsx
@@ -18,19 +18,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { useStore as useUserStore } from "../../../stores/user-store";
-import { Flex, Text } from "@theme-ui/components";
+import { Button, Flex, Grid, Progress, Text } from "@theme-ui/components";
import { strings } from "@notesnook/intl";
import { getSubscriptionInfo } from "./user-profile";
+import { BuyDialog } from "../../buy-dialog";
+import { PromptDialog } from "../../prompt";
+import { TaskManager } from "../../../common/task-manager";
+import { db } from "../../../common/db";
+import { showToast } from "../../../utils/toast";
+import {
+ formatBytes,
+ getFeature,
+ getFeaturesUsage,
+ usePromise
+} from "@notesnook/common";
export function SubscriptionStatus() {
const user = useUserStore((store) => store.user);
+ const featuresUsage = usePromise(() => getFeaturesUsage(), [user]);
const { title, autoRenew, expiryDate, trial, legacy } =
getSubscriptionInfo(user);
-
- const subtitle = autoRenew
+ const subtitle = trial
+ ? "Your free trial is on-going."
+ : autoRenew
? `Your subscription will auto renew on ${expiryDate}.`
- : `Your account will automatically downgrade to the Free plan on ${expiryDate}.`;
+ : expiryDate
+ ? `Your account will automatically downgrade to the Free plan on ${expiryDate}.`
+ : "";
if (!user) return null;
return (
@@ -42,7 +57,8 @@ export function SubscriptionStatus() {
justifyContent: "center",
alignItems: "start",
bg: "var(--background-secondary)",
- p: 2
+ p: 2,
+ gap: 2
// mb: isBasic ? 0 : 4
}}
>
@@ -57,18 +73,70 @@ export function SubscriptionStatus() {
>
{strings.currentPlan()}
</Text>
- <Text
- variant="heading"
- sx={{
- mt: 2
- }}
- >
+ <Text variant="heading">
{title}
{legacy ? " (legacy)" : ""}
</Text>
- <Text variant="body">
- {trial ? "Your free trial is on-going." : subtitle}
- </Text>
+ {subtitle ? <Text variant="body">{subtitle}</Text> : null}
+ {featuresUsage.status === "fulfilled" ? (
+ <Grid
+ sx={{
+ gridTemplateRows: "1fr ".repeat(featuresUsage.value.length),
+ gridTemplateColumns: "1fr 2fr 1fr"
+ }}
+ >
+ {featuresUsage.value.map((feature, index) => (
+ <>
+ <Text sx={{ gridRow: index + 1, gridColumn: 1 }} variant="body">
+ {getFeature(feature.id).title}
+ </Text>
+ <Progress
+ sx={{ gridRow: index + 1, gridColumn: 2 }}
+ max={feature.total}
+ value={feature.used}
+ />
+ <Text
+ sx={{ gridRow: index + 1, gridColumn: 3 }}
+ variant="subBody"
+ >
+ {feature.id === "storage"
+ ? `${formatBytes(feature.used)}/${formatBytes(
+ feature.total
+ )}`
+ : `${feature.used} of ${feature.total}`}
+ </Text>
+ </>
+ ))}
+ </Grid>
+ ) : null}
+
+ {title === "Free" ? (
+ <Flex sx={{ alignItems: "center", justifyContent: "center", gap: 1 }}>
+ <Button variant="accent" onClick={() => BuyDialog.show({})}>
+ {strings.upgradeToPro()}
+ </Button>
+ <Button
+ variant="secondary"
+ onClick={async () => {
+ const giftCode = await PromptDialog.show({
+ title: strings.redeemGiftCode(),
+ description: strings.redeemGiftCodeDesc()
+ });
+ if (giftCode) {
+ await TaskManager.startTask({
+ type: "modal",
+ title: strings.redeemingGiftCode(),
+ subtitle: strings.pleaseWait() + "...",
+ action: () => db.subscriptions.redeemCode(giftCode)
+ }).catch((e) => showToast("error", e.message));
+ }
+ }}
+ sx={{ bg: "background" }}
+ >
+ {strings.redeemGiftCode()}
+ </Button>
+ </Flex>
+ ) : null}
</Flex>
{/* {isBasic ? <Features /> : null} */}
</>
diff --git a/apps/web/src/dialogs/settings/components/user-profile.tsx b/apps/web/src/dialogs/settings/components/user-profile.tsx
index 1e4c4c152..40bade4ed 100644
--- a/apps/web/src/dialogs/settings/components/user-profile.tsx
+++ b/apps/web/src/dialogs/settings/components/user-profile.tsx
@@ -47,23 +47,23 @@ export function getSubscriptionInfo(user: User | undefined): {
autoRenew?: boolean;
} {
const { type, expiry, plan, status, provider } = user?.subscription || {};
- if (!expiry) return { title: "FREE" };
+ if (!expiry) return { title: "Free" };
const legacy = !!type;
const trial =
status === SubscriptionStatus.TRIAL || type === SubscriptionType.TRIAL;
const title =
plan === SubscriptionPlan.BELIEVER
- ? "BELIEVER"
+ ? "Believer"
: plan === SubscriptionPlan.PRO ||
type === SubscriptionType.PREMIUM ||
type === SubscriptionType.PREMIUM_CANCELED
- ? "PRO"
+ ? "Pro"
: plan === SubscriptionPlan.ESSENTIAL
- ? "ESSENTIAL"
+ ? "Essential"
: plan === SubscriptionPlan.EDUCATION
- ? "EDUCATION"
- : "FREE";
+ ? "Education"
+ : "Free";
const autoRenew =
(status === SubscriptionStatus.ACTIVE ||
status === SubscriptionStatus.TRIAL) &&
diff --git a/packages/common/src/utils/is-feature-available.ts b/packages/common/src/utils/is-feature-available.ts
index 897e1aebf..c19af13d3 100644
--- a/packages/common/src/utils/is-feature-available.ts
+++ b/packages/common/src/utils/is-feature-available.ts
@@ -85,13 +85,14 @@ function createLimit<
isAllowed?: (value: TValue) => Limit["isAllowed"]
): Limit<TCaption> {
const inferredValue =
- value ?? caption === "infinity"
+ value ??
+ (caption === "infinity"
? Infinity
: typeof caption === "boolean"
? !!caption
: typeof caption === "number"
? caption
- : false;
+ : false);
return {
caption,
isAllowed: isAllowed
@@ -541,9 +542,11 @@ export type FeatureUsage = {
export async function getFeaturesUsage(): Promise<FeatureUsage[]> {
const { isLegacyPro, plan } = await getUserPlan();
const usage: FeatureUsage[] = [];
+ console.log(isLegacyPro, plan);
for (const key in features) {
const feature = getFeature(key as FeatureId);
const limit = getFeatureLimitFromPlan(feature, plan, isLegacyPro);
+ console.log(limit, feature, key);
if (!feature.used || typeof limit.value !== "number") continue;
usage.push({
id: key as FeatureId,