Files
notesnook/apps/mobile/app/screens/settings/user-section.jsx

427 lines
15 KiB
React
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2025-08-21 10:34:00 +05:00
import { formatBytes } from "@notesnook/common";
2025-10-04 12:54:01 +05:00
import { SubscriptionPlan, SubscriptionProvider } from "@notesnook/core";
2025-08-01 11:19:22 +05:00
import { strings } from "@notesnook/intl";
2024-02-29 23:26:58 +05:00
import { useThemeColors } from "@notesnook/theme";
import { useNetInfo } from "@react-native-community/netinfo";
import dayjs from "dayjs";
2022-08-29 16:19:17 +05:00
import React from "react";
2025-10-05 00:58:02 +05:00
import { Image, Platform, TouchableOpacity, View } from "react-native";
2024-02-29 23:26:58 +05:00
import ImagePicker from "react-native-image-crop-picker";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
2024-02-29 23:26:58 +05:00
import { db } from "../../common/database";
import { presentDialog } from "../../components/dialog/functions";
import {
createFormRef,
validators
} from "../../components/ui/input/form-input";
2025-08-18 13:13:00 +05:00
import { PlanLimits } from "../../components/sheets/plan-limits";
2024-02-29 23:26:58 +05:00
import AppIcon from "../../components/ui/AppIcon";
2025-08-01 11:19:22 +05:00
import { Button } from "../../components/ui/button";
import { TimeSince } from "../../components/ui/time-since";
import Paragraph from "../../components/ui/typography/paragraph";
2025-09-24 10:23:00 +05:00
import { presentSheet, ToastManager } from "../../services/event-manager";
2025-08-01 11:19:22 +05:00
import Navigation from "../../services/navigation";
2025-10-04 12:54:01 +05:00
import PremiumService from "../../services/premium";
2024-02-29 23:26:58 +05:00
import { useThemeStore } from "../../stores/use-theme-store";
2024-02-05 12:14:45 +05:00
import { SyncStatus, useUserStore } from "../../stores/use-user-store";
2025-09-04 11:33:20 +05:00
import { planToDisplayName } from "../../utils/constants";
import { AppFontSize } from "../../utils/size";
2025-02-26 10:18:41 +05:00
import { DefaultAppStyles } from "../../utils/styles";
2025-08-21 10:34:00 +05:00
import { SectionItem } from "./section-item";
import SettingsService from "../../services/settings";
2024-02-05 12:14:45 +05:00
export const getTimeLeft = (t2) => {
let daysRemaining = dayjs(t2).diff(dayjs(), "days");
2021-11-24 09:57:23 +05:00
return {
time: dayjs(t2).diff(dayjs(), daysRemaining === 0 ? "hours" : "days"),
2021-11-24 09:57:23 +05:00
isHour: daysRemaining === 0
};
};
2024-03-26 08:57:04 +05:00
const ProfilePicPlaceholder = (props) => {
2024-02-05 12:14:45 +05:00
const { colors } = useThemeColors();
return (
2024-02-29 23:26:58 +05:00
<TouchableOpacity
2024-02-05 12:14:45 +05:00
style={{
alignItems: "center"
}}
2024-02-29 23:26:58 +05:00
activeOpacity={0.9}
2024-03-26 08:57:04 +05:00
onPress={props?.onChangePicture}
2024-02-05 12:14:45 +05:00
>
<View
style={{
backgroundColor: colors.primary.shade,
borderRadius: 100,
2025-08-01 11:19:22 +05:00
width: 80,
height: 80,
2024-02-05 12:14:45 +05:00
alignItems: "center",
justifyContent: "center"
}}
>
2025-08-21 10:34:00 +05:00
<Icon size={35} color={colors.primary.accent} name="account-outline" />
2024-02-05 12:14:45 +05:00
</View>
2024-02-29 23:26:58 +05:00
</TouchableOpacity>
2024-02-05 12:14:45 +05:00
);
};
2024-03-26 08:57:04 +05:00
const onChangePicture = () => {
2025-02-26 16:03:59 +05:00
useUserStore.setState({
disableAppLockRequests: true
});
2024-03-26 08:57:04 +05:00
const theme =
useThemeStore.getState().colorScheme === "dark"
? useThemeStore.getState().darkTheme
: useThemeStore.getState().lightTheme;
ImagePicker.openPicker({
compressImageMaxWidth: 256,
compressImageMaxHeight: 256,
compressImageQuality: 0.8,
avoidEmptySpaceAroundImage: true,
cropping: true,
cropperCircleOverlay: true,
mediaType: "photo",
forceJpg: true,
includeBase64: true,
writeTempFile: false,
cropperToolbarColor: theme.scopes.base.primary.background,
2024-08-13 15:13:46 +05:00
cropperToolbarTitle: strings.editProfilePicture(),
2024-03-26 08:57:04 +05:00
cropperActiveWidgetColor: theme.scopes.base.primary.accent,
cropperToolbarWidgetColor: theme.scopes.base.primary.icon
2025-02-26 16:03:59 +05:00
})
.then(async (image) => {
if (!image.data) return;
await db.settings.setProfile({
profilePicture: "data:image/jpeg;base64," + image.data
});
useUserStore.setState({
profile: db.settings.getProfile()
});
})
.finally(() => {
setTimeout(() => {
useUserStore.setState({
disableAppLockRequests: false
});
}, 1000);
2024-03-26 08:57:04 +05:00
});
};
2022-04-07 04:20:13 +05:00
const SettingsUserSection = ({ item }) => {
global: implement the new theme engine (#2196) * mobile: theme * theme: add theme engine * mobile: migrate app colors to new theme engine * mobile: fixed some colors * mobile: fix colors * mobile: store theme info in store * theme: `ColorsType` -> `Variants` * theme: use explicit return type for `useThemeColors` * theme: add `backdrop` color * mobile: `const colors` -> `const {colors} * theme: add default pitch-black theme * mobile: manage theme state via theme-engine * mobile: add theme scopes * mobile: commit * mobile: fix button width on applock screen * mobile: fix typings * mobile: fix theme definition * web: add partial support for custom themes only context menus & popups are left. * theme: add dialog & sheet scopes * global: sync with master branch and make everything work again * mobile: fix theme-engine usage in editor & app * mobile: fix colors * mobile: fix colors * mobile: cleanup * mobile: fix status bar color incorrect on entering foreground * mobile: fix dark color scheme * web: move emotion theme provider to @notesnook/theme * editor: add support for theme enging * web: adjust hover & focus colors on list item * mobile: migrate share ext to theme engine * mobile: fix editor theme provider * clipper: add support for the new theme engine * mobile: fix statusbar color on switch from bg * misc: fix build * mobile: fix build * misc: fix colors * mobile: fix theme colors * mobile: fix bottom padding * server: add theme server * theme: add previewColors * server: support themes query pagination * mobile: add client from theme server * server: reset cache on sync repo * server: fix types * server: show ip & port on start server * server: theme updates * web: finalize new theme engine on web * editor: fix build * global: fix @emotion/react version to 11.11.1 * editor: update katex patch * web: fix imports * global: fix @trpc/* versions * global: a huge set of changes 1. get rid of ThemeVariant. All variants can now be accessed anywhere. 2. remove unnecessary button variants 3. make buttons more responsive 4. implement themes server * web: add support for theme search and theme switching * global: update lockfiles * mobile: fix error * theme: use vite-plugin-react to start theme server * web: add support for auto updating themes * mobile: update theme selector * mobile: update theme if new verison available * theme: add `isomorphic-fetch` package * global: update lockfiles * web: add theme details dialog * setup: add scope for themes server in bootstrap script * web: add production server url * web: update lockfile * web: update lockfile * mobile: remove `react-native-blob-util` * web: add support for endless scrolling in themes * web: bring back dark/light mode option in settings * web: fix colors in places * theme: add selected variant * global: use single typescript version across the projects * web: fix sort & group options not having submenus * web: apply selected variant where appropriate * ui: use unique id for all menu items * config: add ui scope for commits * theme: export button variant creation fn * web: fix only 1 theme showing in theme selector * web: fix navigation item hover & other colors * mobile: update theme * editor: fix toolbar group alignments * editor: set theme provider at app level * theme: use scope name to get current scope * mobile: fix color usage in message card * theme: remove caching * editor: bring back icons in table menus * theme: use zustand to manage theme engine state * web: fix login/signup theming * mobile: fix webpack build * misc: remove ThemeProvider usage * editor: adjust theming and styling of editor toolbar * mobile: refactor * editor: fix toolbar group padding everywhere * web: fix settings sidebar is not scrollable * web: add loading indicator for themes loading * mobile: fix warning * mobile: fix ui issues * web: fix Loader errors on build * theme: add getPreviewColors & validateTheme * theme: fix theme validation * mobile: load theme from file * mobile: fix share extension crash * mobile: rename state * theme: add sourceURL property * theme: refactor theme-engine * web: add support for loading theme from file * web: improve button hover interaction * mobile: fix floating button color * mobile: update theme * mobile: fix border radius of context menu * mobile: set sheet overlay color to theme backdrop * mobile: set sidemenu backdrop to theme backdrop --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2023-08-01 12:07:21 +05:00
const { colors } = useThemeColors();
2025-08-21 10:34:00 +05:00
const [user] = useUserStore((state) => [state.user]);
const lastSynced = useUserStore((state) => state.lastSynced);
2024-02-05 12:14:45 +05:00
const lastSyncStatus = useUserStore((state) => state.lastSyncStatus);
const { isInternetReachable } = useNetInfo();
const isOffline = !isInternetReachable;
2024-02-29 23:26:58 +05:00
const userProfile = useUserStore((state) => state.profile);
2025-09-03 12:38:47 +05:00
const used = user?.storageUsed || 0;
const total = user?.totalStorage || 0;
2025-09-04 11:33:20 +05:00
2025-10-04 12:54:01 +05:00
const isCurrentPlatform =
2025-10-05 01:11:09 +05:00
(user?.subscription?.provider === SubscriptionProvider.APPLE &&
2025-10-04 12:54:01 +05:00
Platform.OS === "ios") ||
2025-10-05 01:11:09 +05:00
(user?.subscription?.provider === SubscriptionProvider.GOOGLE &&
2025-10-04 12:54:01 +05:00
Platform.OS === "android");
2021-11-24 09:57:23 +05:00
return (
<>
{user ? (
<>
<View
style={{
2025-02-26 10:18:41 +05:00
paddingHorizontal: DefaultAppStyles.GAP,
2025-09-10 12:28:44 +05:00
paddingTop: 25
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<View
style={{
2024-02-05 12:14:45 +05:00
flexDirection: "row",
width: "100%"
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<View
style={{
2025-08-01 11:19:22 +05:00
flexDirection: "row",
width: "100%",
gap: DefaultAppStyles.GAP,
alignItems: "center"
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<View
style={{
2024-02-05 12:14:45 +05:00
borderRadius: 100,
2025-08-01 11:19:22 +05:00
alignSelf: "flex-start"
2024-02-05 12:14:45 +05:00
}}
>
2024-02-29 23:26:58 +05:00
{userProfile?.profilePicture ? (
2024-03-26 08:57:04 +05:00
<TouchableOpacity
onPress={onChangePicture}
activeOpacity={1}
>
<Image
source={{
uri: userProfile?.profilePicture
}}
style={{
2025-08-01 11:19:22 +05:00
width: 80,
height: 80,
borderRadius: 80
2024-03-26 08:57:04 +05:00
}}
/>
</TouchableOpacity>
2024-02-29 23:26:58 +05:00
) : (
2024-03-26 08:57:04 +05:00
<ProfilePicPlaceholder onChangePicture={onChangePicture} />
2024-02-29 23:26:58 +05:00
)}
2024-02-05 12:14:45 +05:00
</View>
2025-08-01 11:19:22 +05:00
<View>
2024-02-29 23:26:58 +05:00
<Paragraph
onPress={() => {
presentDialog({
2024-08-13 15:13:46 +05:00
title: strings.setFullName(),
paragraph: strings.setFullNameDesc(),
positiveText: strings.save(),
form: {
formRef: createFormRef({
fullName: userProfile?.fullName || ""
}),
items: [
{
name: "fullName",
placeholder: strings.enterFullName(),
defaultValue: userProfile?.fullName,
validators: [
validators.required(strings.nameIsRequired())
]
}
],
onFormSubmit: async (form) => {
try {
await db.settings.setProfile({
fullName: form.getValue("fullName")
});
2024-02-29 23:26:58 +05:00
useUserStore.setState({
profile: db.settings.getProfile()
2024-02-29 23:26:58 +05:00
});
return true;
} catch (e) {
form.setError("fullName", e.message);
return false;
}
}
2024-02-29 23:26:58 +05:00
}
});
}}
color={colors.primary.heading}
2025-08-01 11:19:22 +05:00
size={AppFontSize.md}
2024-02-29 23:26:58 +05:00
>
{userProfile?.fullName
? userProfile.fullName + " "
2024-07-27 10:19:43 +05:00
: strings.setYourName() + " "}
2025-08-01 11:19:22 +05:00
<AppIcon name="pencil" size={AppFontSize.md} />
2024-02-05 12:14:45 +05:00
</Paragraph>
<Paragraph
color={colors.primary.heading}
size={AppFontSize.xs}
>
2024-02-05 12:14:45 +05:00
{user?.email}
</Paragraph>
<Paragraph
2021-11-24 09:57:23 +05:00
style={{
2025-08-01 11:19:22 +05:00
flexWrap: "wrap"
2022-01-22 12:57:05 +05:00
}}
size={AppFontSize.xs}
2024-02-05 12:14:45 +05:00
color={colors.secondary.heading}
2022-01-22 12:57:05 +05:00
>
2024-02-05 12:14:45 +05:00
{!user ? (
2024-07-27 10:19:43 +05:00
strings.notLoggedIn()
2024-02-05 12:14:45 +05:00
) : lastSynced && lastSynced !== "Never" ? (
<>
{lastSyncStatus === SyncStatus.Failed
2024-07-27 10:19:43 +05:00
? strings.syncFailed()
: strings.synced()}{" "}
2022-04-07 04:20:13 +05:00
<TimeSince
2024-02-05 12:14:45 +05:00
style={{
fontSize: AppFontSize.xs,
2024-02-05 12:14:45 +05:00
color: colors.secondary.paragraph
}}
2022-04-07 04:20:13 +05:00
time={lastSynced}
2025-08-01 11:19:22 +05:00
/>{" "}
ago
2024-07-27 10:19:43 +05:00
{isOffline ? ` (${strings.offline()})` : ""}
2024-02-05 12:14:45 +05:00
</>
) : (
2024-07-27 10:19:43 +05:00
strings.never()
2025-08-01 11:19:22 +05:00
)}
</Paragraph>
</View>
</View>
</View>
<View
style={{
paddingVertical: DefaultAppStyles.GAP_SMALL,
gap: DefaultAppStyles.GAP_VERTICAL,
2025-09-10 12:28:44 +05:00
borderRadius: 10
2025-08-01 11:19:22 +05:00
}}
>
<View
style={{
gap: DefaultAppStyles.GAP_SMALL,
paddingHorizontal: DefaultAppStyles.GAP_SMALL
}}
>
<View
style={{
flexDirection: "row",
width: "100%",
justifyContent: "space-between"
}}
>
<Paragraph size={AppFontSize.xxs}>
{strings.storage()}
</Paragraph>
<Paragraph size={AppFontSize.xxs}>
2025-09-03 10:54:01 +05:00
{formatBytes(used)}/
2025-09-04 11:33:20 +05:00
{total === -1
2025-09-03 10:54:01 +05:00
? "Unlimited"
: formatBytes(total) + " " + strings.used()}
2025-08-01 11:19:22 +05:00
</Paragraph>
</View>
<View
style={{
backgroundColor: colors.secondary.background,
width: "100%",
height: 5,
borderRadius: 10
}}
>
<View
style={{
2025-08-21 10:34:00 +05:00
backgroundColor: colors.primary.accent,
2025-08-01 11:19:22 +05:00
height: 5,
width: `${(used / total) * 100}%`,
borderRadius: 10
}}
/>
</View>
</View>
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
width: "100%",
paddingHorizontal: DefaultAppStyles.GAP_SMALL,
borderRadius: 10
}}
>
2025-08-18 13:13:00 +05:00
<TouchableOpacity
activeOpacity={1}
onPress={() => {
presentSheet({
component: <PlanLimits />
});
}}
>
2025-08-01 11:19:22 +05:00
<Paragraph size={AppFontSize.sm}>
2025-10-05 01:11:09 +05:00
{planToDisplayName(user.subscription?.plan)}
2025-08-01 11:19:22 +05:00
</Paragraph>
<Paragraph
color={colors.secondary.paragraph}
size={AppFontSize.xxxs}
>
{strings.viewAllLimits()}{" "}
<AppIcon name="information" size={AppFontSize.xxxs} />
2024-02-05 12:14:45 +05:00
</Paragraph>
2025-08-18 13:13:00 +05:00
</TouchableOpacity>
2025-08-01 11:19:22 +05:00
{((user.subscription?.provider ===
SubscriptionProvider.PADDLE ||
2025-10-05 01:11:09 +05:00
user.subscription?.provider ===
2025-10-04 12:54:01 +05:00
SubscriptionProvider.STREETWRITERS ||
!isCurrentPlatform) &&
PremiumService.get()) ||
SettingsService.getProperty("serverUrls") ? null : (
2025-09-24 10:23:00 +05:00
<Button
title={
2025-10-05 01:11:09 +05:00
user.subscription?.plan !== SubscriptionPlan.FREE
2025-09-24 10:23:00 +05:00
? strings.changePlan()
: strings.upgradePlan()
}
onPress={() => {
if (
user?.subscription?.plan === SubscriptionPlan.LEGACY_PRO
) {
ToastManager.show({
message: strings.cannotChangePlan(),
context: "local"
});
return;
}
2025-09-25 08:41:06 +05:00
if (
2025-10-05 01:11:09 +05:00
user.subscription?.plan !== SubscriptionPlan.FREE &&
user.subscription?.productId &&
user.subscription?.productId.includes("5year")
2025-09-25 08:41:06 +05:00
) {
ToastManager.show({
message:
"You have made a one time purchase. To change your plan please contact support.",
type: "info"
});
return;
}
2025-09-24 10:23:00 +05:00
Navigation.navigate("PayWall", {
context: "logged-in",
canGoBack: true
});
}}
type="accent"
fontSize={AppFontSize.xs}
style={{
paddingHorizontal: DefaultAppStyles.GAP_SMALL,
height: "auto",
paddingVertical: DefaultAppStyles.GAP_SMALL
}}
/>
)}
2021-11-24 09:57:23 +05:00
</View>
</View>
</View>
2025-08-21 10:34:00 +05:00
{item.sections.map((item) => (
2022-04-07 04:20:13 +05:00
<SectionItem key={item.name} item={item} />
2025-08-21 10:34:00 +05:00
))}
2021-11-24 09:57:23 +05:00
</>
) : null}
</>
);
};
export default SettingsUserSection;