1 import { useState } from 'react';
3 import { c } from 'ttag';
5 import { useEffectOnce, useLoading } from '@proton/hooks';
6 import { queryUpdateUserSettings, queryUserSettings } from '@proton/shared/lib/api/docs/user';
7 import type { UserSettingsResponse } from '@proton/shared/lib/interfaces/docs/userSettings';
9 import useApi from '../useApi';
10 import useNotifications from '../useNotifications';
12 export const useDocsNotificationsSettings = () => {
14 const [isReady, setIsReady] = useState(false);
15 const [isLoading, withLoading] = useLoading();
16 const [isSubmitting, withSubmitLoading] = useLoading();
17 const { createNotification } = useNotifications();
19 const [emailNotificationsEnabled, setEmailNotificationsEnabled] = useState<boolean | null>(null);
20 const [emailTitleEnabled, setEmailTitleEnabled] = useState<boolean | null>(null);
24 api<UserSettingsResponse>(queryUserSettings()).then(({ UserSettings }) => {
25 setEmailNotificationsEnabled(UserSettings.DocsCommentsNotificationsEnabled);
26 setEmailTitleEnabled(UserSettings.DocsCommentsNotificationsIncludeDocumentName);
32 const updateNotificationSettings = ({
36 notificationsEnabled: boolean;
37 includeTitleEnabled: boolean;
39 return withSubmitLoading(
40 api<UserSettingsResponse>(
41 queryUpdateUserSettings({
42 DocsCommentsNotificationsEnabled: notificationsEnabled,
43 DocsCommentsNotificationsIncludeDocumentName: includeTitleEnabled,
49 text: c('Info').t`Settings updated`,
51 setEmailNotificationsEnabled(notificationsEnabled);
52 setEmailTitleEnabled(includeTitleEnabled);
57 text: c('Info').t`Settings update failed`,
59 // eslint-disable-next-line no-console
60 console.error('Settings update failed.', err);
64 const changeEmailNotificationsEnabledValue = (value: boolean) => {
65 return updateNotificationSettings({
66 notificationsEnabled: value,
67 includeTitleEnabled: emailTitleEnabled ?? false,
71 const changeDocumentTitleEnabledValue = (value: boolean) => {
72 return updateNotificationSettings({
73 notificationsEnabled: emailNotificationsEnabled ?? false,
74 includeTitleEnabled: value,
79 emailNotificationsEnabled,
84 changeEmailNotificationsEnabledValue,
85 changeDocumentTitleEnabledValue,
86 updateNotificationSettings,