@greatapps/common 1.1.97 → 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 (115) 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 +7 -2
  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/auth/services/auth.service.mjs +9 -4
  38. package/dist/modules/auth/services/auth.service.mjs.map +1 -1
  39. package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs +3 -2
  40. package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs.map +1 -1
  41. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs +2 -1
  42. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs.map +1 -1
  43. package/dist/modules/projects/actions/add-project-user.action.mjs +3 -2
  44. package/dist/modules/projects/actions/add-project-user.action.mjs.map +1 -1
  45. package/dist/modules/projects/actions/list-available-users.action.mjs +3 -2
  46. package/dist/modules/projects/actions/list-available-users.action.mjs.map +1 -1
  47. package/dist/modules/projects/actions/list-project-users.action.mjs +3 -2
  48. package/dist/modules/projects/actions/list-project-users.action.mjs.map +1 -1
  49. package/dist/modules/projects/actions/remove-project-user.action.mjs +3 -2
  50. package/dist/modules/projects/actions/remove-project-user.action.mjs.map +1 -1
  51. package/dist/modules/projects/hooks/list-available-users.hook.mjs +8 -1
  52. package/dist/modules/projects/hooks/list-available-users.hook.mjs.map +1 -1
  53. package/dist/modules/projects/hooks/list-project-users.hook.mjs +8 -1
  54. package/dist/modules/projects/hooks/list-project-users.hook.mjs.map +1 -1
  55. package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs +3 -2
  56. package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs.map +1 -1
  57. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs +5 -1
  58. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs.map +1 -1
  59. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs +2 -1
  60. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs.map +1 -1
  61. package/dist/modules/users/action/find-user-by-id.action.mjs +3 -14
  62. package/dist/modules/users/action/find-user-by-id.action.mjs.map +1 -1
  63. package/dist/modules/users/action/list-messages.action.mjs +6 -2
  64. package/dist/modules/users/action/list-messages.action.mjs.map +1 -1
  65. package/dist/modules/users/action/mark-all-messages-as-read.action.mjs +3 -2
  66. package/dist/modules/users/action/mark-all-messages-as-read.action.mjs.map +1 -1
  67. package/dist/modules/users/action/mark-message-as-read.action.mjs +3 -2
  68. package/dist/modules/users/action/mark-message-as-read.action.mjs.map +1 -1
  69. package/dist/modules/users/hooks/messages.hook.mjs +11 -9
  70. package/dist/modules/users/hooks/messages.hook.mjs.map +1 -1
  71. package/dist/providers/auth.provider.mjs +9 -15
  72. package/dist/providers/auth.provider.mjs.map +1 -1
  73. package/dist/utils/safeServerAction.mjs +10 -16
  74. package/dist/utils/safeServerAction.mjs.map +1 -1
  75. package/dist/utils/withAction.mjs +13 -0
  76. package/dist/utils/withAction.mjs.map +1 -0
  77. package/package.json +1 -1
  78. package/src/components/account/hooks/useTwoFactorAuthForm.tsx +101 -114
  79. package/src/components/account/sections/MyProfileSection.tsx +29 -26
  80. package/src/components/account/sections/PreferencesSection.tsx +10 -16
  81. package/src/index.ts +1 -0
  82. package/src/modules/accounts/actions/account-management.action.ts +90 -261
  83. package/src/modules/accounts/actions/find-current-account.action.ts +5 -4
  84. package/src/modules/accounts/actions/list-currencies.action.ts +8 -7
  85. package/src/modules/accounts/actions/list-timezones.action.ts +8 -7
  86. package/src/modules/accounts/hooks/current-account.hook.tsx +6 -5
  87. package/src/modules/accounts/hooks/useAccountManagement.ts +85 -83
  88. package/src/modules/accounts/hooks/useCurrencies.ts +15 -14
  89. package/src/modules/accounts/hooks/useTimezones.ts +15 -14
  90. package/src/modules/auth/actions/logout.action.ts +8 -7
  91. package/src/modules/auth/actions/register.action.ts +9 -4
  92. package/src/modules/auth/actions/verify-two-factor.action.ts +8 -21
  93. package/src/modules/auth/hooks/logout.hook.tsx +2 -1
  94. package/src/modules/auth/hooks/register.hook.tsx +2 -1
  95. package/src/modules/auth/hooks/useUserQuery.ts +2 -2
  96. package/src/modules/auth/services/auth.service.ts +11 -6
  97. package/src/modules/ia-credits/actions/list-ia-credits.action.ts +8 -11
  98. package/src/modules/ia-credits/hooks/ia-credits.hook.ts +2 -1
  99. package/src/modules/projects/actions/add-project-user.action.ts +9 -8
  100. package/src/modules/projects/actions/list-available-users.action.ts +9 -8
  101. package/src/modules/projects/actions/list-project-users.action.ts +9 -8
  102. package/src/modules/projects/actions/remove-project-user.action.ts +9 -8
  103. package/src/modules/projects/hooks/list-available-users.hook.ts +33 -26
  104. package/src/modules/projects/hooks/list-project-users.hook.ts +33 -26
  105. package/src/modules/subscriptions/actions/list-subscriptions.action.ts +9 -11
  106. package/src/modules/subscriptions/hooks/find-active-subscription.hook.ts +19 -15
  107. package/src/modules/subscriptions/hooks/list-subscriptions.hook.ts +17 -15
  108. package/src/modules/users/action/find-user-by-id.action.ts +8 -25
  109. package/src/modules/users/action/list-messages.action.ts +12 -10
  110. package/src/modules/users/action/mark-all-messages-as-read.action.ts +8 -8
  111. package/src/modules/users/action/mark-message-as-read.action.ts +8 -8
  112. package/src/modules/users/hooks/messages.hook.ts +60 -58
  113. package/src/providers/auth.provider.tsx +9 -16
  114. package/src/utils/safeServerAction.ts +18 -24
  115. package/src/utils/withAction.ts +13 -0
@@ -1,58 +1,60 @@
1
- 'use client';
2
-
3
- import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
4
- import { listMessagesAction } from '../action/list-messages.action';
5
- import { markMessageAsReadAction } from '../action/mark-message-as-read.action';
6
- import { markAllMessagesAsReadAction } from '../action/mark-all-messages-as-read.action';
7
- import { safeServerAction } from '../../../utils/safeServerAction';
8
- import type { FindMessagesParams } from '../schema/messages.schema';
9
-
10
- export const MESSAGES_QUERY_KEY = ['messages'];
11
-
12
- const INFINITE_MESSAGES_PAGE_SIZE = 10;
13
-
14
- export function useMessages(params?: Partial<FindMessagesParams>) {
15
- return useQuery({
16
- queryKey: [...MESSAGES_QUERY_KEY, params],
17
- queryFn: () => safeServerAction(listMessagesAction, params),
18
- });
19
- }
20
-
21
- export function useInfiniteMessages(params?: Partial<Omit<FindMessagesParams, 'page' | 'limit'>>) {
22
- return useInfiniteQuery({
23
- queryKey: [...MESSAGES_QUERY_KEY, 'infinite', params],
24
- queryFn: ({ pageParam }) =>
25
- safeServerAction(listMessagesAction, {
26
- ...params,
27
- page: pageParam as number,
28
- limit: INFINITE_MESSAGES_PAGE_SIZE,
29
- }),
30
- getNextPageParam: (lastPage, allPages) => {
31
- const loaded = allPages.reduce((sum, p) => sum + p.data.length, 0);
32
- return loaded < lastPage.total ? allPages.length + 1 : undefined;
33
- },
34
- initialPageParam: 1,
35
- });
36
- }
37
-
38
- export function useMarkMessageAsRead() {
39
- const queryClient = useQueryClient();
40
-
41
- return useMutation({
42
- mutationFn: (messageId: string) => safeServerAction(markMessageAsReadAction, messageId),
43
- onSuccess: () => {
44
- queryClient.invalidateQueries({ queryKey: MESSAGES_QUERY_KEY });
45
- },
46
- });
47
- }
48
-
49
- export function useMarkAllMessagesAsRead() {
50
- const queryClient = useQueryClient();
51
-
52
- return useMutation({
53
- mutationFn: () => safeServerAction(markAllMessagesAsReadAction),
54
- onSuccess: () => {
55
- queryClient.invalidateQueries({ queryKey: MESSAGES_QUERY_KEY });
56
- },
57
- });
58
- }
1
+ 'use client';
2
+
3
+ import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
4
+ import { listMessagesAction } from '../action/list-messages.action';
5
+ import { markMessageAsReadAction } from '../action/mark-message-as-read.action';
6
+ import { markAllMessagesAsReadAction } from '../action/mark-all-messages-as-read.action';
7
+ import { withAction } from '../../../utils/withAction';
8
+ import type { FindMessagesParams } from '../schema/messages.schema';
9
+
10
+ export const MESSAGES_QUERY_KEY = ['messages'];
11
+
12
+ const INFINITE_MESSAGES_PAGE_SIZE = 10;
13
+
14
+ export function useMessages(params?: Partial<FindMessagesParams>) {
15
+ return useQuery({
16
+ queryKey: [...MESSAGES_QUERY_KEY, params],
17
+ queryFn: withAction(() => listMessagesAction(params)),
18
+ });
19
+ }
20
+
21
+ export function useInfiniteMessages(params?: Partial<Omit<FindMessagesParams, 'page' | 'limit'>>) {
22
+ return useInfiniteQuery({
23
+ queryKey: [...MESSAGES_QUERY_KEY, 'infinite', params],
24
+ queryFn: ({ pageParam }) =>
25
+ withAction(() =>
26
+ listMessagesAction({
27
+ ...params,
28
+ page: pageParam as number,
29
+ limit: INFINITE_MESSAGES_PAGE_SIZE,
30
+ })
31
+ )(),
32
+ getNextPageParam: (lastPage, allPages) => {
33
+ const loaded = allPages.reduce((sum, p) => sum + p.data.length, 0);
34
+ return loaded < lastPage.total ? allPages.length + 1 : undefined;
35
+ },
36
+ initialPageParam: 1,
37
+ });
38
+ }
39
+
40
+ export function useMarkMessageAsRead() {
41
+ const queryClient = useQueryClient();
42
+
43
+ return useMutation({
44
+ mutationFn: withAction(markMessageAsReadAction),
45
+ onSuccess: () => {
46
+ queryClient.invalidateQueries({ queryKey: MESSAGES_QUERY_KEY });
47
+ },
48
+ });
49
+ }
50
+
51
+ export function useMarkAllMessagesAsRead() {
52
+ const queryClient = useQueryClient();
53
+
54
+ return useMutation({
55
+ mutationFn: withAction(markAllMessagesAsReadAction),
56
+ onSuccess: () => {
57
+ queryClient.invalidateQueries({ queryKey: MESSAGES_QUERY_KEY });
58
+ },
59
+ });
60
+ }
@@ -7,10 +7,10 @@ import {
7
7
  useContext,
8
8
  useMemo,
9
9
  } from "react";
10
+ import { useQueryClient } from "@tanstack/react-query";
10
11
  import {
11
12
  useUserQuery,
12
13
  useSetUserData,
13
- useInvalidateUser,
14
14
  useUserValidateSession,
15
15
  } from "../modules/auth/hooks/useUserQuery";
16
16
  import { useLogin } from "../modules/auth/hooks/login.hook";
@@ -26,10 +26,7 @@ export type LoginResult =
26
26
  | { result: "success" }
27
27
  | { result: "two_factor_required"; cookie: string }
28
28
  | { result: "error"; message: string };
29
- import {
30
- useCurrentAccount,
31
- useInvalidateAccount,
32
- } from "../modules/accounts/hooks/current-account.hook";
29
+ import { useCurrentAccount } from "../modules/accounts/hooks/current-account.hook";
33
30
  import { useRouter } from "next/navigation";
34
31
 
35
32
  interface AuthContextProps extends AuthState {
@@ -51,9 +48,8 @@ export function AuthProvider({ children }: AuthProviderProps) {
51
48
  const { data: account, isLoading: isAccountLoading } = useCurrentAccount();
52
49
  const { isLoading: isSessionLoading } = useUserValidateSession();
53
50
 
51
+ const queryClient = useQueryClient();
54
52
  const setUserData = useSetUserData();
55
- const invalidateUser = useInvalidateUser();
56
- const invalidateAccount = useInvalidateAccount();
57
53
 
58
54
  const router = useRouter();
59
55
 
@@ -79,37 +75,34 @@ export function AuthProvider({ children }: AuthProviderProps) {
79
75
  return { result: "two_factor_required", cookie: data.cookie };
80
76
  }
81
77
 
78
+ queryClient.clear();
82
79
  setUserData(data.user);
83
80
 
84
- invalidateUser();
85
- invalidateAccount();
86
-
87
81
  return { result: "success" };
88
82
  },
89
- [setUserData, invalidateUser, invalidateAccount]
83
+ [queryClient, setUserData]
90
84
  );
91
85
 
92
86
  const register = useCallback(
93
87
  async (data: RegisterRequest): Promise<void> => {
94
88
  await registerMutate(data, {
95
89
  onSuccess: (data) => {
96
- invalidateUser();
97
- invalidateAccount();
90
+ queryClient.clear();
98
91
  setUserData(data.user);
99
92
  },
100
93
  });
101
94
  },
102
- [setUserData, invalidateUser, invalidateAccount]
95
+ [queryClient, setUserData]
103
96
  );
104
97
 
105
98
  const logout = useCallback(async (): Promise<void> => {
106
99
  await logoutMutate(undefined, {
107
100
  onSuccess: () => {
108
- invalidateUser();
101
+ queryClient.clear();
109
102
  router.push("/login");
110
103
  },
111
104
  });
112
- }, [setUserData]);
105
+ }, [queryClient, router]);
113
106
 
114
107
  const value: AuthContextProps = useMemo(
115
108
  () => ({
@@ -1,27 +1,21 @@
1
- export class ServerActionError extends Error {
2
- status: number;
1
+ import { isRedirectError } from 'next/dist/client/components/redirect-error';
2
+ import { ApiError } from '../infra/api/types';
3
+ import type { ActionResult } from '../infra/api/types';
3
4
 
4
- constructor(message: string, status: number) {
5
- super(message);
6
- this.name = 'ServerActionError';
7
- this.status = status;
5
+ export async function safeServerAction<T>(
6
+ fn: () => Promise<T>
7
+ ): Promise<ActionResult<T>> {
8
+ try {
9
+ const data = await fn();
10
+ return { success: true, data };
11
+ } catch (error) {
12
+ if (isRedirectError(error)) throw error;
13
+ const message =
14
+ error instanceof ApiError
15
+ ? error.message
16
+ : error instanceof Error
17
+ ? error.message
18
+ : 'Erro desconhecido';
19
+ return { success: false, error: message };
8
20
  }
9
21
  }
10
-
11
- export async function safeServerAction<
12
- TArgs extends any[],
13
- TResult extends Record<string, any>,
14
- >(
15
- action: (...args: TArgs) => Promise<TResult>,
16
- ...args: TArgs
17
- ): Promise<Omit<TResult, 'error'>> {
18
- const result: TResult = await action(...args);
19
-
20
- if (result.status === 0 || result.result === 'error' || result.error) {
21
- const msg = result.message || result.error || 'Erro na ação';
22
- throw new ServerActionError(msg, result.status || 0);
23
- }
24
-
25
- const { error, ...successData } = result;
26
- return successData as Omit<TResult, 'error'>;
27
- }
@@ -0,0 +1,13 @@
1
+ import type { ActionResult } from '../infra/api/types';
2
+
3
+ export function withAction<TArgs extends any[], TData>(
4
+ action: (...args: TArgs) => Promise<ActionResult<TData>>
5
+ ): (...args: TArgs) => Promise<TData> {
6
+ return async (...args) => {
7
+ const result = await action(...args);
8
+ if (!result.success) {
9
+ throw new Error(result.error ?? 'Erro desconhecido');
10
+ }
11
+ return result.data as TData;
12
+ };
13
+ }