diff --git a/src/renderer/src/pages/settings/settings-account.tsx b/src/renderer/src/pages/settings/settings-account.tsx index 9cf35541..213ab830 100644 --- a/src/renderer/src/pages/settings/settings-account.tsx +++ b/src/renderer/src/pages/settings/settings-account.tsx @@ -1,255 +1,255 @@ -import { Avatar, Button, SelectField } from "@renderer/components"; -import { Controller, useForm } from "react-hook-form"; -import { useTranslation } from "react-i18next"; -import { useDate, useToast, useUserDetails } from "@renderer/hooks"; -import { useCallback, useContext, useEffect, useState } from "react"; -import { - CloudIcon, - KeyIcon, - MailIcon, - XCircleFillIcon, -} from "@primer/octicons-react"; -import { settingsContext } from "@renderer/context"; -import { AuthPage } from "@shared"; -import "./settings-account.scss"; - -interface FormValues { - profileVisibility: "PUBLIC" | "FRIENDS" | "PRIVATE"; -} - -export function SettingsAccount() { - const { t } = useTranslation("settings"); - - const [isUnblocking, setIsUnblocking] = useState(false); - - const { showSuccessToast } = useToast(); - - const { blockedUsers, fetchBlockedUsers } = useContext(settingsContext); - - const { formatDate } = useDate(); - - const { - control, - formState: { isSubmitting }, - setValue, - handleSubmit, - } = useForm(); - - const { - userDetails, - hasActiveSubscription, - patchUser, - fetchUserDetails, - updateUserDetails, - unblockUser, - } = useUserDetails(); - - useEffect(() => { - if (userDetails?.profileVisibility) { - setValue("profileVisibility", userDetails.profileVisibility); - } - }, [userDetails, setValue]); - - useEffect(() => { - const unsubscribe = window.electron.onAccountUpdated(() => { - fetchUserDetails().then((response) => { - if (response) { - updateUserDetails(response); - } - }); - showSuccessToast(t("account_data_updated_successfully")); - }); - - return () => { - unsubscribe(); - }; - }, [fetchUserDetails, updateUserDetails, t, showSuccessToast]); - - const visibilityOptions = [ - { value: "PUBLIC", label: t("public") }, - { value: "FRIENDS", label: t("friends_only") }, - { value: "PRIVATE", label: t("private") }, - ]; - - const onSubmit = async (values: FormValues) => { - await patchUser(values); - showSuccessToast(t("changes_saved")); - }; - - const handleUnblockClick = useCallback( - (id: string) => { - setIsUnblocking(true); - - unblockUser(id) - .then(() => { - fetchBlockedUsers(); - showSuccessToast(t("user_unblocked")); - }) - .finally(() => { - setIsUnblocking(false); - }); - }, - [unblockUser, fetchBlockedUsers, t, showSuccessToast] - ); - - const getHydraCloudSectionContent = () => { - const hasSubscribedBefore = Boolean(userDetails?.subscription?.expiresAt); - const isRenewalActive = userDetails?.subscription?.status === "active"; - - if (!hasSubscribedBefore) { - return { - description: {t("no_subscription")}, - callToAction: t("become_subscriber"), - }; - } - - if (hasActiveSubscription) { - return { - description: isRenewalActive ? ( - <> - - {t("subscription_renews_on", { - date: formatDate(userDetails.subscription!.expiresAt!), - })} - - {t("bill_sent_until")} - - ) : ( - <> - {t("subscription_renew_cancelled")} - - {t("subscription_active_until", { - date: formatDate(userDetails!.subscription!.expiresAt!), - })} - - - ), - callToAction: t("manage_subscription"), - }; - } - - return { - description: ( - - {t("subscription_expired_at", { - date: formatDate(userDetails!.subscription!.expiresAt!), - })} - - ), - callToAction: t("renew_subscription"), - }; - }; - - if (!userDetails) return null; - - return ( -
- { - const handleChange = ( - event: React.ChangeEvent - ) => { - field.onChange(event); - handleSubmit(onSubmit)(); - }; - - return ( -
- ({ - key: visiblity.value, - value: visiblity.value, - label: visiblity.label, - }))} - disabled={isSubmitting} - /> - - {t("profile_visibility_description")} -
- ); - }} - /> - -
-

{t("current_email")}

-

{userDetails?.email ?? t("no_email_account")}

- -
- - - -
-
- -
-

Hydra Cloud

-
- {getHydraCloudSectionContent().description} -
- - -
- -
-

{t("blocked_users")}

- - {blockedUsers.length > 0 ? ( -
    - {blockedUsers.map((user) => { - return ( -
  • -
    - - {user.displayName} -
    - - -
  • - ); - })} -
- ) : ( - {t("no_users_blocked")} - )} -
- - ); -} +import { Avatar, Button, SelectField } from "@renderer/components"; +import { Controller, useForm } from "react-hook-form"; +import { useTranslation } from "react-i18next"; +import { useDate, useToast, useUserDetails } from "@renderer/hooks"; +import { useCallback, useContext, useEffect, useState } from "react"; +import { + CloudIcon, + KeyIcon, + MailIcon, + XCircleFillIcon, +} from "@primer/octicons-react"; +import { settingsContext } from "@renderer/context"; +import { AuthPage } from "@shared"; +import "./settings-account.scss"; + +interface FormValues { + profileVisibility: "PUBLIC" | "FRIENDS" | "PRIVATE"; +} + +export function SettingsAccount() { + const { t } = useTranslation("settings"); + + const [isUnblocking, setIsUnblocking] = useState(false); + + const { showSuccessToast } = useToast(); + + const { blockedUsers, fetchBlockedUsers } = useContext(settingsContext); + + const { formatDate } = useDate(); + + const { + control, + formState: { isSubmitting }, + setValue, + handleSubmit, + } = useForm(); + + const { + userDetails, + hasActiveSubscription, + patchUser, + fetchUserDetails, + updateUserDetails, + unblockUser, + } = useUserDetails(); + + useEffect(() => { + if (userDetails?.profileVisibility) { + setValue("profileVisibility", userDetails.profileVisibility); + } + }, [userDetails, setValue]); + + useEffect(() => { + const unsubscribe = window.electron.onAccountUpdated(() => { + fetchUserDetails().then((response) => { + if (response) { + updateUserDetails(response); + } + }); + showSuccessToast(t("account_data_updated_successfully")); + }); + + return () => { + unsubscribe(); + }; + }, [fetchUserDetails, updateUserDetails, t, showSuccessToast]); + + const visibilityOptions = [ + { value: "PUBLIC", label: t("public") }, + { value: "FRIENDS", label: t("friends_only") }, + { value: "PRIVATE", label: t("private") }, + ]; + + const onSubmit = async (values: FormValues) => { + await patchUser(values); + showSuccessToast(t("changes_saved")); + }; + + const handleUnblockClick = useCallback( + (id: string) => { + setIsUnblocking(true); + + unblockUser(id) + .then(() => { + fetchBlockedUsers(); + showSuccessToast(t("user_unblocked")); + }) + .finally(() => { + setIsUnblocking(false); + }); + }, + [unblockUser, fetchBlockedUsers, t, showSuccessToast] + ); + + const getHydraCloudSectionContent = () => { + const hasSubscribedBefore = Boolean(userDetails?.subscription?.expiresAt); + const isRenewalActive = userDetails?.subscription?.status === "active"; + + if (!hasSubscribedBefore) { + return { + description: {t("no_subscription")}, + callToAction: t("become_subscriber"), + }; + } + + if (hasActiveSubscription) { + return { + description: isRenewalActive ? ( + <> + + {t("subscription_renews_on", { + date: formatDate(userDetails.subscription!.expiresAt!), + })} + + {t("bill_sent_until")} + + ) : ( + <> + {t("subscription_renew_cancelled")} + + {t("subscription_active_until", { + date: formatDate(userDetails!.subscription!.expiresAt!), + })} + + + ), + callToAction: t("manage_subscription"), + }; + } + + return { + description: ( + + {t("subscription_expired_at", { + date: formatDate(userDetails!.subscription!.expiresAt!), + })} + + ), + callToAction: t("renew_subscription"), + }; + }; + + if (!userDetails) return null; + + return ( +
+ { + const handleChange = ( + event: React.ChangeEvent + ) => { + field.onChange(event); + handleSubmit(onSubmit)(); + }; + + return ( +
+ ({ + key: visiblity.value, + value: visiblity.value, + label: visiblity.label, + }))} + disabled={isSubmitting} + /> + + {t("profile_visibility_description")} +
+ ); + }} + /> + +
+

{t("current_email")}

+

{userDetails?.email ?? t("no_email_account")}

+

Nome de usuário:

+

{userDetails?.username}

+
+ + + +
+
+
+

Hydra Cloud

+
+ {getHydraCloudSectionContent().description} +
+ + +
+ +
+

{t("blocked_users")}

+ + {blockedUsers.length > 0 ? ( +
    + {blockedUsers.map((user) => { + return ( +
  • +
    + + {user.displayName} +
    + + +
  • + ); + })} +
+ ) : ( + {t("no_users_blocked")} + )} +
+ + ); +}