@greatapps/common 1.1.98 → 1.1.99

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/dist/components/account/hooks/useTwoFactorAuthForm.mjs +5 -13
  2. package/dist/components/account/hooks/useTwoFactorAuthForm.mjs.map +1 -1
  3. package/dist/components/account/sections/MyProfileSection.mjs +30 -19
  4. package/dist/components/account/sections/MyProfileSection.mjs.map +1 -1
  5. package/dist/components/account/sections/PreferencesSection.mjs +11 -10
  6. package/dist/components/account/sections/PreferencesSection.mjs.map +1 -1
  7. package/dist/index.mjs +3 -1
  8. package/dist/index.mjs.map +1 -1
  9. package/dist/modules/accounts/actions/account-management.action.mjs +36 -168
  10. package/dist/modules/accounts/actions/account-management.action.mjs.map +1 -1
  11. package/dist/modules/accounts/actions/find-current-account.action.mjs +3 -2
  12. package/dist/modules/accounts/actions/find-current-account.action.mjs.map +1 -1
  13. package/dist/modules/accounts/actions/list-currencies.action.mjs +3 -2
  14. package/dist/modules/accounts/actions/list-currencies.action.mjs.map +1 -1
  15. package/dist/modules/accounts/actions/list-timezones.action.mjs +3 -2
  16. package/dist/modules/accounts/actions/list-timezones.action.mjs.map +1 -1
  17. package/dist/modules/accounts/hooks/current-account.hook.mjs +2 -1
  18. package/dist/modules/accounts/hooks/current-account.hook.mjs.map +1 -1
  19. package/dist/modules/accounts/hooks/useAccountManagement.mjs +8 -5
  20. package/dist/modules/accounts/hooks/useAccountManagement.mjs.map +1 -1
  21. package/dist/modules/accounts/hooks/useCurrencies.mjs +2 -1
  22. package/dist/modules/accounts/hooks/useCurrencies.mjs.map +1 -1
  23. package/dist/modules/accounts/hooks/useTimezones.mjs +2 -1
  24. package/dist/modules/accounts/hooks/useTimezones.mjs.map +1 -1
  25. package/dist/modules/auth/actions/logout.action.mjs +6 -3
  26. package/dist/modules/auth/actions/logout.action.mjs.map +1 -1
  27. package/dist/modules/auth/actions/register.action.mjs +6 -3
  28. package/dist/modules/auth/actions/register.action.mjs.map +1 -1
  29. package/dist/modules/auth/actions/verify-two-factor.action.mjs +5 -10
  30. package/dist/modules/auth/actions/verify-two-factor.action.mjs.map +1 -1
  31. package/dist/modules/auth/hooks/logout.hook.mjs +2 -1
  32. package/dist/modules/auth/hooks/logout.hook.mjs.map +1 -1
  33. package/dist/modules/auth/hooks/register.hook.mjs +2 -1
  34. package/dist/modules/auth/hooks/register.hook.mjs.map +1 -1
  35. package/dist/modules/auth/hooks/useUserQuery.mjs +2 -2
  36. package/dist/modules/auth/hooks/useUserQuery.mjs.map +1 -1
  37. package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs +3 -2
  38. package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs.map +1 -1
  39. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs +2 -1
  40. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs.map +1 -1
  41. package/dist/modules/projects/actions/add-project-user.action.mjs +3 -2
  42. package/dist/modules/projects/actions/add-project-user.action.mjs.map +1 -1
  43. package/dist/modules/projects/actions/list-available-users.action.mjs +3 -2
  44. package/dist/modules/projects/actions/list-available-users.action.mjs.map +1 -1
  45. package/dist/modules/projects/actions/list-project-users.action.mjs +3 -2
  46. package/dist/modules/projects/actions/list-project-users.action.mjs.map +1 -1
  47. package/dist/modules/projects/actions/remove-project-user.action.mjs +3 -2
  48. package/dist/modules/projects/actions/remove-project-user.action.mjs.map +1 -1
  49. package/dist/modules/projects/hooks/list-available-users.hook.mjs +8 -1
  50. package/dist/modules/projects/hooks/list-available-users.hook.mjs.map +1 -1
  51. package/dist/modules/projects/hooks/list-project-users.hook.mjs +8 -1
  52. package/dist/modules/projects/hooks/list-project-users.hook.mjs.map +1 -1
  53. package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs +3 -2
  54. package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs.map +1 -1
  55. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs +5 -1
  56. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs.map +1 -1
  57. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs +2 -1
  58. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs.map +1 -1
  59. package/dist/modules/users/action/find-user-by-id.action.mjs +3 -14
  60. package/dist/modules/users/action/find-user-by-id.action.mjs.map +1 -1
  61. package/dist/modules/users/action/list-messages.action.mjs +6 -2
  62. package/dist/modules/users/action/list-messages.action.mjs.map +1 -1
  63. package/dist/modules/users/action/mark-all-messages-as-read.action.mjs +3 -2
  64. package/dist/modules/users/action/mark-all-messages-as-read.action.mjs.map +1 -1
  65. package/dist/modules/users/action/mark-message-as-read.action.mjs +3 -2
  66. package/dist/modules/users/action/mark-message-as-read.action.mjs.map +1 -1
  67. package/dist/modules/users/hooks/messages.hook.mjs +11 -9
  68. package/dist/modules/users/hooks/messages.hook.mjs.map +1 -1
  69. package/dist/utils/safeServerAction.mjs +10 -16
  70. package/dist/utils/safeServerAction.mjs.map +1 -1
  71. package/dist/utils/withAction.mjs +13 -0
  72. package/dist/utils/withAction.mjs.map +1 -0
  73. package/package.json +1 -1
  74. package/src/components/account/hooks/useTwoFactorAuthForm.tsx +101 -114
  75. package/src/components/account/sections/MyProfileSection.tsx +29 -26
  76. package/src/components/account/sections/PreferencesSection.tsx +10 -16
  77. package/src/index.ts +1 -0
  78. package/src/modules/accounts/actions/account-management.action.ts +90 -261
  79. package/src/modules/accounts/actions/find-current-account.action.ts +5 -4
  80. package/src/modules/accounts/actions/list-currencies.action.ts +8 -7
  81. package/src/modules/accounts/actions/list-timezones.action.ts +8 -7
  82. package/src/modules/accounts/hooks/current-account.hook.tsx +6 -5
  83. package/src/modules/accounts/hooks/useAccountManagement.ts +85 -83
  84. package/src/modules/accounts/hooks/useCurrencies.ts +15 -14
  85. package/src/modules/accounts/hooks/useTimezones.ts +15 -14
  86. package/src/modules/auth/actions/logout.action.ts +8 -7
  87. package/src/modules/auth/actions/register.action.ts +9 -6
  88. package/src/modules/auth/actions/verify-two-factor.action.ts +8 -21
  89. package/src/modules/auth/hooks/logout.hook.tsx +2 -1
  90. package/src/modules/auth/hooks/register.hook.tsx +2 -1
  91. package/src/modules/auth/hooks/useUserQuery.ts +2 -2
  92. package/src/modules/ia-credits/actions/list-ia-credits.action.ts +8 -11
  93. package/src/modules/ia-credits/hooks/ia-credits.hook.ts +2 -1
  94. package/src/modules/projects/actions/add-project-user.action.ts +9 -8
  95. package/src/modules/projects/actions/list-available-users.action.ts +9 -8
  96. package/src/modules/projects/actions/list-project-users.action.ts +9 -8
  97. package/src/modules/projects/actions/remove-project-user.action.ts +9 -8
  98. package/src/modules/projects/hooks/list-available-users.hook.ts +33 -26
  99. package/src/modules/projects/hooks/list-project-users.hook.ts +33 -26
  100. package/src/modules/subscriptions/actions/list-subscriptions.action.ts +9 -11
  101. package/src/modules/subscriptions/hooks/find-active-subscription.hook.ts +19 -15
  102. package/src/modules/subscriptions/hooks/list-subscriptions.hook.ts +17 -15
  103. package/src/modules/users/action/find-user-by-id.action.ts +8 -25
  104. package/src/modules/users/action/list-messages.action.ts +12 -10
  105. package/src/modules/users/action/mark-all-messages-as-read.action.ts +8 -8
  106. package/src/modules/users/action/mark-message-as-read.action.ts +8 -8
  107. package/src/modules/users/hooks/messages.hook.ts +60 -58
  108. package/src/utils/safeServerAction.ts +18 -24
  109. package/src/utils/withAction.ts +13 -0
@@ -1,114 +1,101 @@
1
- 'use client';
2
-
3
- import { useState, useCallback, useEffect } from 'react';
4
- import { useMutation } from '@tanstack/react-query';
5
- import { toast } from 'sonner';
6
- import { Toast } from '../../ui/feedback/Toast';
7
- import { copyToClipboard } from '../../../utils/browser/clipboard';
8
- import {
9
- generateTwoFactorAction,
10
- confirmTwoFactorAction,
11
- } from '../../../modules/accounts/actions/account-management.action';
12
-
13
- type Step = 'code' | 'recovery';
14
-
15
- interface UseTwoFactorAuthFormProps {
16
- onSuccess?: () => void;
17
- open?: boolean;
18
- }
19
-
20
- export default function useTwoFactorAuthForm({ onSuccess, open }: UseTwoFactorAuthFormProps = {}) {
21
- const [code, setCode] = useState('');
22
- const [step, setStep] = useState<Step>('code');
23
-
24
- const generateMutation = useMutation({
25
- mutationFn: generateTwoFactorAction,
26
- onSuccess: (result) => {
27
- if (!result.success) {
28
- toast.custom((t) => (
29
- <Toast variant="error" message={result.error ?? 'Erro ao gerar QR Code. Tente novamente.'} toastId={t} />
30
- ));
31
- }
32
- },
33
- onError: () => {
34
- toast.custom((t) => (
35
- <Toast variant="error" message="Erro ao gerar QR Code. Tente novamente." toastId={t} />
36
- ));
37
- },
38
- });
39
-
40
- const confirmMutation = useMutation({
41
- mutationFn: (otpCode: string) => confirmTwoFactorAction(otpCode),
42
- onSuccess: (result) => {
43
- if (!result.success) {
44
- toast.custom((t) => (
45
- <Toast variant="error" message={result.error ?? 'Código inválido'} toastId={t} />
46
- ));
47
- return;
48
- }
49
- setStep('recovery');
50
- },
51
- onError: () => {
52
- toast.custom((t) => (
53
- <Toast variant="error" message="Erro ao confirmar código. Tente novamente." toastId={t} />
54
- ));
55
- },
56
- });
57
-
58
- const { mutate: triggerGenerate } = generateMutation;
59
-
60
- useEffect(() => {
61
- if (open) {
62
- triggerGenerate();
63
- }
64
- }, [open, triggerGenerate]);
65
-
66
- const generateFailed =
67
- (generateMutation.isSuccess && !generateMutation.data?.success) || generateMutation.isError;
68
-
69
- const isCodeComplete = code.length === 6;
70
-
71
- const handleSubmit = () => {
72
- if (!isCodeComplete) return;
73
- confirmMutation.mutate(code);
74
- };
75
-
76
- const handleFinalize = () => {
77
- onSuccess?.();
78
- setTimeout(resetForm, 300);
79
- };
80
-
81
- const handleCopyCodes = useCallback(async () => {
82
- const codes = generateMutation.data?.backupCodes ?? [];
83
- const success = await copyToClipboard(codes.join('\n'));
84
- if (success) {
85
- toast.custom((t) => (
86
- <Toast variant="success" message="Códigos copiados com sucesso" toastId={t} />
87
- ));
88
- }
89
- }, [generateMutation.data]);
90
-
91
- const resetForm = () => {
92
- setCode('');
93
- setStep('code');
94
- generateMutation.reset();
95
- confirmMutation.reset();
96
- };
97
-
98
- return {
99
- code,
100
- setCode,
101
- isCodeComplete,
102
- isGenerating: generateMutation.isPending,
103
- isConfirming: confirmMutation.isPending,
104
- generateFailed,
105
- retryGenerate: triggerGenerate,
106
- qrcode: generateMutation.data?.qrcode,
107
- backupCodes: generateMutation.data?.backupCodes ?? [],
108
- step,
109
- handleSubmit,
110
- handleFinalize,
111
- handleCopyCodes,
112
- resetForm,
113
- };
114
- }
1
+ 'use client';
2
+
3
+ import { useState, useCallback, useEffect } from 'react';
4
+ import { useMutation } from '@tanstack/react-query';
5
+ import { toast } from 'sonner';
6
+ import { Toast } from '../../ui/feedback/Toast';
7
+ import { copyToClipboard } from '../../../utils/browser/clipboard';
8
+ import {
9
+ generateTwoFactorAction,
10
+ confirmTwoFactorAction,
11
+ } from '../../../modules/accounts/actions/account-management.action';
12
+ import { withAction } from '../../../utils/withAction';
13
+
14
+ type Step = 'code' | 'recovery';
15
+
16
+ interface UseTwoFactorAuthFormProps {
17
+ onSuccess?: () => void;
18
+ open?: boolean;
19
+ }
20
+
21
+ export default function useTwoFactorAuthForm({ onSuccess, open }: UseTwoFactorAuthFormProps = {}) {
22
+ const [code, setCode] = useState('');
23
+ const [step, setStep] = useState<Step>('code');
24
+
25
+ const generateMutation = useMutation({
26
+ mutationFn: withAction(generateTwoFactorAction),
27
+ onError: () => {
28
+ toast.custom((t) => (
29
+ <Toast variant="error" message="Erro ao gerar QR Code. Tente novamente." toastId={t} />
30
+ ));
31
+ },
32
+ });
33
+
34
+ const confirmMutation = useMutation({
35
+ mutationFn: withAction(confirmTwoFactorAction),
36
+ onSuccess: () => {
37
+ setStep('recovery');
38
+ },
39
+ onError: () => {
40
+ toast.custom((t) => (
41
+ <Toast variant="error" message="Erro ao confirmar código. Tente novamente." toastId={t} />
42
+ ));
43
+ },
44
+ });
45
+
46
+ const { mutate: triggerGenerate } = generateMutation;
47
+
48
+ useEffect(() => {
49
+ if (open) {
50
+ triggerGenerate();
51
+ }
52
+ }, [open, triggerGenerate]);
53
+
54
+ const generateFailed = generateMutation.isError;
55
+
56
+ const isCodeComplete = code.length === 6;
57
+
58
+ const handleSubmit = () => {
59
+ if (!isCodeComplete) return;
60
+ confirmMutation.mutate(code);
61
+ };
62
+
63
+ const handleFinalize = () => {
64
+ onSuccess?.();
65
+ setTimeout(resetForm, 300);
66
+ };
67
+
68
+ const handleCopyCodes = useCallback(async () => {
69
+ const codes = generateMutation.data?.backupCodes ?? [];
70
+ const success = await copyToClipboard(codes.join('\n'));
71
+ if (success) {
72
+ toast.custom((t) => (
73
+ <Toast variant="success" message="Códigos copiados com sucesso" toastId={t} />
74
+ ));
75
+ }
76
+ }, [generateMutation.data]);
77
+
78
+ const resetForm = () => {
79
+ setCode('');
80
+ setStep('code');
81
+ generateMutation.reset();
82
+ confirmMutation.reset();
83
+ };
84
+
85
+ return {
86
+ code,
87
+ setCode,
88
+ isCodeComplete,
89
+ isGenerating: generateMutation.isPending,
90
+ isConfirming: confirmMutation.isPending,
91
+ generateFailed,
92
+ retryGenerate: triggerGenerate,
93
+ qrcode: generateMutation.data?.qrcode,
94
+ backupCodes: generateMutation.data?.backupCodes ?? [],
95
+ step,
96
+ handleSubmit,
97
+ handleFinalize,
98
+ handleCopyCodes,
99
+ resetForm,
100
+ };
101
+ }
@@ -84,47 +84,50 @@ export function MyProfileSection({ onClose, storageUrl }: MyProfileSectionProps)
84
84
  };
85
85
 
86
86
  const handleDelete = async () => {
87
- const result = isOwner
88
- ? await deleteAccount.mutateAsync()
89
- : await deleteAccountUser.mutateAsync(user!.id);
90
-
91
- if (!result.success) {
87
+ try {
88
+ if (isOwner) {
89
+ await deleteAccount.mutateAsync();
90
+ } else {
91
+ await deleteAccountUser.mutateAsync(user!.id);
92
+ }
93
+ await logout();
94
+ } catch (error) {
92
95
  toast.custom((t) => (
93
96
  <Toast
94
97
  variant="error"
95
- message={result.error ?? 'Erro ao excluir. Tente novamente.'}
98
+ message={error instanceof Error ? error.message : 'Erro ao excluir. Tente novamente.'}
96
99
  toastId={t}
97
100
  />
98
101
  ));
99
102
  setDeleteConfirmOpen(false);
100
- return;
101
103
  }
102
-
103
- await logout();
104
104
  };
105
105
 
106
106
  const onSubmit = async (data: MyProfileFormData) => {
107
- const result = await updateAccountUser.mutateAsync({
108
- name: data.name,
109
- gender: GENDER_OPTIONS.find((g) => g.value === data.gender)?.apiValue,
110
- ...(selectedPhoto ? { photo: selectedPhoto } : {}),
111
- ...(photoRemoved ? { photo: '' } : {}),
112
- });
107
+ try {
108
+ await updateAccountUser.mutateAsync({
109
+ name: data.name,
110
+ gender: GENDER_OPTIONS.find((g) => g.value === data.gender)?.apiValue,
111
+ ...(selectedPhoto ? { photo: selectedPhoto } : {}),
112
+ ...(photoRemoved ? { photo: '' } : {}),
113
+ });
113
114
 
114
- if (!result.success) {
115
+ form.reset(data);
116
+ setSelectedPhoto(null);
117
+ setPhotoRemoved(false);
115
118
  toast.custom((t) => (
116
- <Toast variant="error" message={result.error ?? 'Erro ao atualizar perfil'} toastId={t} />
119
+ <Toast variant="success" message="Perfil atualizado com sucesso" toastId={t} />
120
+ ));
121
+ onClose();
122
+ } catch (error) {
123
+ toast.custom((t) => (
124
+ <Toast
125
+ variant="error"
126
+ message={error instanceof Error ? error.message : 'Erro ao atualizar perfil'}
127
+ toastId={t}
128
+ />
117
129
  ));
118
- return;
119
130
  }
120
-
121
- form.reset(data);
122
- setSelectedPhoto(null);
123
- setPhotoRemoved(false);
124
- toast.custom((t) => (
125
- <Toast variant="success" message="Perfil atualizado com sucesso" toastId={t} />
126
- ));
127
- onClose();
128
131
  };
129
132
 
130
133
  return (
@@ -87,28 +87,14 @@ export function PreferencesSection({ onClose }: PreferencesSectionProps) {
87
87
  setIsSaving(true);
88
88
  try {
89
89
  const apiLanguage = LANGUAGE_OPTIONS.find((o) => o.value === data.language)?.apiValue ?? data.language.toLowerCase();
90
- const userResult = await updateAccountUser.mutateAsync({
90
+ await updateAccountUser.mutateAsync({
91
91
  language: apiLanguage,
92
92
  receive_sms: data.receive_sms,
93
93
  receive_email: data.receive_email,
94
94
  });
95
95
 
96
- if (!userResult.success) {
97
- toast.custom((t) => (
98
- <Toast variant="error" message={userResult.error ?? 'Erro ao salvar preferências'} toastId={t} />
99
- ));
100
- return;
101
- }
102
-
103
96
  if (canEditPreferences && Object.keys(accountFields).length > 0) {
104
- const accountResult = await updateAccount.mutateAsync(accountFields);
105
-
106
- if (!accountResult.success) {
107
- toast.custom((t) => (
108
- <Toast variant="error" message={accountResult.error ?? 'Erro ao salvar preferências da conta'} toastId={t} />
109
- ));
110
- return;
111
- }
97
+ await updateAccount.mutateAsync(accountFields);
112
98
  }
113
99
 
114
100
  reset(data);
@@ -116,6 +102,14 @@ export function PreferencesSection({ onClose }: PreferencesSectionProps) {
116
102
  <Toast variant="success" message="Preferências salvas com sucesso" toastId={t} />
117
103
  ));
118
104
  onClose();
105
+ } catch (error) {
106
+ toast.custom((t) => (
107
+ <Toast
108
+ variant="error"
109
+ message={error instanceof Error ? error.message : 'Erro ao salvar preferências'}
110
+ toastId={t}
111
+ />
112
+ ));
119
113
  } finally {
120
114
  setIsSaving(false);
121
115
  }
package/src/index.ts CHANGED
@@ -74,6 +74,7 @@ export type {
74
74
  export { cn } from "./infra/utils/clsx";
75
75
  export { buildQueryParams } from "./infra/utils/params";
76
76
  export { parseSchema, parseResult } from "./infra/utils/parser";
77
+ export { withAction } from "./utils/withAction";
77
78
 
78
79
  // Layout
79
80
  export {