@greatapps/common 1.1.237 → 1.1.239

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 (73) hide show
  1. package/dist/components/modals/BuyCreditsModal.mjs +277 -0
  2. package/dist/components/modals/BuyCreditsModal.mjs.map +1 -0
  3. package/dist/components/navigation/subcomponents/CreditsCard.mjs +5 -2
  4. package/dist/components/navigation/subcomponents/CreditsCard.mjs.map +1 -1
  5. package/dist/components/ui/data-display/CardBrandIcons.mjs +484 -0
  6. package/dist/components/ui/data-display/CardBrandIcons.mjs.map +1 -0
  7. package/dist/components/ui/data-display/CardItem.mjs +67 -0
  8. package/dist/components/ui/data-display/CardItem.mjs.map +1 -0
  9. package/dist/components/ui/data-display/PaymentInfoCard.mjs +161 -0
  10. package/dist/components/ui/data-display/PaymentInfoCard.mjs.map +1 -0
  11. package/dist/components/ui/feedback/Skeleton.mjs +9 -0
  12. package/dist/components/ui/feedback/Skeleton.mjs.map +1 -0
  13. package/dist/index.mjs +57 -29
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/modules/cards/actions/list-cards.action.mjs +10 -0
  16. package/dist/modules/cards/actions/list-cards.action.mjs.map +1 -0
  17. package/dist/modules/cards/hooks/cards.hook.mjs +16 -0
  18. package/dist/modules/cards/hooks/cards.hook.mjs.map +1 -0
  19. package/dist/modules/cards/services/cards.service.mjs +29 -0
  20. package/dist/modules/cards/services/cards.service.mjs.map +1 -0
  21. package/dist/modules/cards/types.mjs +27 -0
  22. package/dist/modules/cards/types.mjs.map +1 -0
  23. package/dist/modules/subscriptions/actions/calculate-subscription.action.mjs +27 -0
  24. package/dist/modules/subscriptions/actions/calculate-subscription.action.mjs.map +1 -0
  25. package/dist/modules/subscriptions/actions/update-subscription-plan.action.mjs +21 -0
  26. package/dist/modules/subscriptions/actions/update-subscription-plan.action.mjs.map +1 -0
  27. package/dist/modules/subscriptions/constants/addons.constants.mjs +13 -1
  28. package/dist/modules/subscriptions/constants/addons.constants.mjs.map +1 -1
  29. package/dist/modules/subscriptions/hooks/calculate-subscription.hook.mjs +13 -0
  30. package/dist/modules/subscriptions/hooks/calculate-subscription.hook.mjs.map +1 -0
  31. package/dist/modules/subscriptions/hooks/update-subscription-plan.hook.mjs +22 -0
  32. package/dist/modules/subscriptions/hooks/update-subscription-plan.hook.mjs.map +1 -0
  33. package/dist/modules/subscriptions/services/subscriptions.service.mjs +36 -0
  34. package/dist/modules/subscriptions/services/subscriptions.service.mjs.map +1 -1
  35. package/dist/modules/subscriptions/types/billing-period.type.mjs +1 -0
  36. package/dist/modules/subscriptions/types/billing-period.type.mjs.map +1 -0
  37. package/dist/modules/subscriptions/types/calculate-subscription.type.mjs +72 -0
  38. package/dist/modules/subscriptions/types/calculate-subscription.type.mjs.map +1 -0
  39. package/dist/modules/subscriptions/utils/build-plan-extras.mjs +34 -0
  40. package/dist/modules/subscriptions/utils/build-plan-extras.mjs.map +1 -0
  41. package/dist/modules/subscriptions/utils/periodicity.mjs +48 -0
  42. package/dist/modules/subscriptions/utils/periodicity.mjs.map +1 -0
  43. package/dist/modules/subscriptions/utils/resolve-plan-extras-prices.mjs +14 -0
  44. package/dist/modules/subscriptions/utils/resolve-plan-extras-prices.mjs.map +1 -0
  45. package/dist/server.mjs +10 -0
  46. package/dist/server.mjs.map +1 -1
  47. package/dist/store/useBuyCreditsModal.mjs +18 -0
  48. package/dist/store/useBuyCreditsModal.mjs.map +1 -0
  49. package/package.json +1 -1
  50. package/src/components/modals/BuyCreditsModal.tsx +304 -0
  51. package/src/components/navigation/subcomponents/CreditsCard.tsx +5 -2
  52. package/src/components/ui/data-display/CardBrandIcons.tsx +336 -0
  53. package/src/components/ui/data-display/CardItem.tsx +75 -0
  54. package/src/components/ui/data-display/PaymentInfoCard.tsx +186 -0
  55. package/src/components/ui/feedback/Skeleton.tsx +5 -0
  56. package/src/index.ts +21 -2
  57. package/src/modules/cards/actions/list-cards.action.ts +8 -0
  58. package/src/modules/cards/hooks/cards.hook.ts +14 -0
  59. package/src/modules/cards/services/cards.service.ts +32 -0
  60. package/src/modules/cards/types.ts +32 -0
  61. package/src/modules/subscriptions/actions/calculate-subscription.action.ts +32 -0
  62. package/src/modules/subscriptions/actions/update-subscription-plan.action.ts +26 -0
  63. package/src/modules/subscriptions/constants/addons.constants.ts +18 -0
  64. package/src/modules/subscriptions/hooks/calculate-subscription.hook.ts +13 -0
  65. package/src/modules/subscriptions/hooks/update-subscription-plan.hook.ts +25 -0
  66. package/src/modules/subscriptions/services/subscriptions.service.ts +52 -1
  67. package/src/modules/subscriptions/types/billing-period.type.ts +1 -0
  68. package/src/modules/subscriptions/types/calculate-subscription.type.ts +75 -0
  69. package/src/modules/subscriptions/utils/build-plan-extras.ts +53 -0
  70. package/src/modules/subscriptions/utils/periodicity.ts +52 -0
  71. package/src/modules/subscriptions/utils/resolve-plan-extras-prices.ts +23 -0
  72. package/src/server.ts +5 -0
  73. package/src/store/useBuyCreditsModal.ts +25 -0
@@ -0,0 +1,18 @@
1
+ import { useModalManager } from "./useModalManager";
2
+ const useBuyCreditsModal = () => {
3
+ const { activeModal, modalData, openModal, closeModal } = useModalManager();
4
+ const isOpen = activeModal === "buyCreditsModal";
5
+ const data = modalData;
6
+ return {
7
+ open: isOpen,
8
+ onConfirm: data?.onConfirm,
9
+ initialActiveCardId: data?.initialActiveCardId,
10
+ initialSelectedValue: data?.initialSelectedValue,
11
+ openModal: (onConfirm) => openModal("buyCreditsModal", { onConfirm }),
12
+ closeModal
13
+ };
14
+ };
15
+ export {
16
+ useBuyCreditsModal
17
+ };
18
+ //# sourceMappingURL=useBuyCreditsModal.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/store/useBuyCreditsModal.ts"],"sourcesContent":["import { useModalManager } from './useModalManager';\n\ninterface BuyCreditsModalData {\n onConfirm?: (creditAmount: number, price: number) => void;\n initialActiveCardId?: string;\n initialSelectedValue?: number;\n}\n\nexport const useBuyCreditsModal = () => {\n const { activeModal, modalData, openModal, closeModal } = useModalManager();\n\n const isOpen = activeModal === 'buyCreditsModal';\n const data = modalData as BuyCreditsModalData;\n\n return {\n open: isOpen,\n onConfirm: data?.onConfirm,\n initialActiveCardId: data?.initialActiveCardId,\n initialSelectedValue: data?.initialSelectedValue,\n\n openModal: (onConfirm?: (creditAmount: number, price: number) => void) =>\n openModal('buyCreditsModal', { onConfirm }),\n closeModal,\n };\n};\n"],"mappings":"AAAA,SAAS,uBAAuB;AAQzB,MAAM,qBAAqB,MAAM;AACtC,QAAM,EAAE,aAAa,WAAW,WAAW,WAAW,IAAI,gBAAgB;AAE1E,QAAM,SAAS,gBAAgB;AAC/B,QAAM,OAAO;AAEb,SAAO;AAAA,IACL,MAAM;AAAA,IACN,WAAW,MAAM;AAAA,IACjB,qBAAqB,MAAM;AAAA,IAC3B,sBAAsB,MAAM;AAAA,IAE5B,WAAW,CAAC,cACV,UAAU,mBAAmB,EAAE,UAAU,CAAC;AAAA,IAC5C;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@greatapps/common",
3
- "version": "1.1.237",
3
+ "version": "1.1.239",
4
4
  "description": "Shared library for GreatApps frontend applications",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./src/index.ts",
@@ -0,0 +1,304 @@
1
+ 'use client';
2
+
3
+ import {
4
+ Dialog,
5
+ DialogContent,
6
+ DialogHeader,
7
+ DialogTitle,
8
+ } from '../ui/overlay/Dialog';
9
+ import { Button } from '../ui/buttons/Button';
10
+ import { Separator } from '../ui/data-display/Separator';
11
+ import { Toast } from '../ui/feedback/Toast';
12
+ import { SelectField } from '../ui/form/SelectField';
13
+ import { Skeleton } from '../ui/feedback/Skeleton';
14
+ import { PaymentInfoCard } from '../ui/data-display/PaymentInfoCard';
15
+ import { ADDON_IDS } from '../../modules/subscriptions/constants/addons.constants';
16
+ import {
17
+ AI_CREDIT_OPTIONS,
18
+ AI_CREDIT_VALUES,
19
+ } from '../../modules/subscriptions/constants/addons.constants';
20
+ import { useBuyCreditsModal } from '../../store/useBuyCreditsModal';
21
+ import { useModalManager } from '../../store/useModalManager';
22
+ import { useCards } from '../../modules/cards/hooks/cards.hook';
23
+ import { useActiveSubscription } from '../../modules/subscriptions/hooks/find-active-subscription.hook';
24
+ import { useCalculateSubscription } from '../../modules/subscriptions/hooks/calculate-subscription.hook';
25
+ import { useUpdateSubscriptionPlan } from '../../modules/subscriptions/hooks/update-subscription-plan.hook';
26
+ import { buildPlanExtras } from '../../modules/subscriptions/utils/build-plan-extras';
27
+ import { getPriceFromCalculatedData } from '../../modules/subscriptions/utils/periodicity';
28
+ import { IconInfoCircle, IconX, IconLoader2 } from '@tabler/icons-react';
29
+ import { useCallback, useEffect, useMemo } from 'react';
30
+ import { useForm } from 'react-hook-form';
31
+ import { zodResolver } from '@hookform/resolvers/zod';
32
+ import z from 'zod';
33
+ import { toast } from 'sonner';
34
+
35
+ const buyCreditSchema = z.object({
36
+ creditOption: z.number().refine((val) => AI_CREDIT_VALUES.has(val), {
37
+ message: 'Selecione uma opção de créditos',
38
+ }),
39
+ selectedCardId: z.string().min(1, 'Selecione um cartão'),
40
+ });
41
+
42
+ type BuyCreditFormData = z.infer<typeof buyCreditSchema>;
43
+
44
+ export default function BuyCreditsModal() {
45
+ const { open, closeModal } = useBuyCreditsModal();
46
+ const { openModal } = useModalManager();
47
+ const { data: cardsData } = useCards();
48
+ const { data: subscriptionData } = useActiveSubscription();
49
+ const subscription = subscriptionData?.data?.[0] ?? null;
50
+
51
+ const {
52
+ mutate: calculate,
53
+ isPending: isCalculatePending,
54
+ data: calculateData,
55
+ } = useCalculateSubscription();
56
+ const updatePlanMutation = useUpdateSubscriptionPlan();
57
+
58
+ const aiItem = useMemo(
59
+ () => subscription?.items?.find((item) => item.id_addon === ADDON_IDS.AI),
60
+ [subscription]
61
+ );
62
+
63
+ const currentCredits = aiItem?.quantity ?? 0;
64
+ const subscriptionId = subscription?.id;
65
+ const planId = subscription?.id_plan;
66
+
67
+ const form = useForm<BuyCreditFormData>({
68
+ resolver: zodResolver(buyCreditSchema),
69
+ mode: 'onChange',
70
+ defaultValues: {
71
+ creditOption: currentCredits,
72
+ selectedCardId: cardsData?.data?.[0]?.id.toString() ?? '',
73
+ },
74
+ });
75
+
76
+ useEffect(() => {
77
+ if (open) {
78
+ form.reset({
79
+ creditOption: currentCredits,
80
+ selectedCardId:
81
+ form.getValues('selectedCardId') || cardsData?.data?.[0]?.id.toString() || '',
82
+ });
83
+
84
+ calculate({
85
+ id_product: 1,
86
+ ...(planId != null && { id_plan: planId }),
87
+ ...(subscriptionId != null && { id_subscription: subscriptionId }),
88
+ plan_extras: buildPlanExtras(subscription?.items ?? [], [
89
+ {
90
+ id_addon: ADDON_IDS.AI,
91
+ quantity: currentCredits,
92
+ currency: 'BRL',
93
+ },
94
+ ]),
95
+ });
96
+ }
97
+ }, [open, subscription, currentCredits, cardsData, form, calculate, planId, subscriptionId]);
98
+
99
+ const creditOption = form.watch('creditOption');
100
+ const selectedCardId = form.watch('selectedCardId');
101
+ const { isDirty, isSubmitting, isValid } = form.formState;
102
+
103
+ const subscriptionItems = useMemo(() => subscription?.items ?? [], [subscription]);
104
+
105
+ const handleCreditOptionChange = useCallback(
106
+ (value: string | number | boolean) => {
107
+ const credits = Number(value);
108
+
109
+ form.setValue('creditOption', credits, {
110
+ shouldValidate: true,
111
+ shouldDirty: true,
112
+ });
113
+
114
+ const option = AI_CREDIT_OPTIONS.find((opt) => opt.credits === credits);
115
+ if (!option) return;
116
+
117
+ calculate({
118
+ id_product: 1,
119
+ ...(planId != null && { id_plan: planId }),
120
+ ...(subscriptionId != null && { id_subscription: subscriptionId }),
121
+ plan_extras: buildPlanExtras(subscriptionItems, [
122
+ {
123
+ id_addon: ADDON_IDS.AI,
124
+ quantity: option.credits,
125
+ currency: 'BRL',
126
+ },
127
+ ]),
128
+ });
129
+ },
130
+ [form, subscriptionId, planId, subscriptionItems, calculate]
131
+ );
132
+
133
+ const calculatedData = calculateData?.data ?? null;
134
+
135
+ const totalPrice = useMemo(() => {
136
+ if (!calculatedData) return null;
137
+ return getPriceFromCalculatedData(calculatedData, subscription?.periodicity);
138
+ }, [calculatedData, subscription]);
139
+
140
+ const formatPrice = useCallback((price: number) => {
141
+ return price.toLocaleString('pt-BR', {
142
+ style: 'currency',
143
+ currency: 'BRL',
144
+ });
145
+ }, []);
146
+
147
+ const isConfirmDisabled = !isDirty || !isValid || isCalculatePending || isSubmitting;
148
+
149
+ const handleClose = useCallback(() => {
150
+ form.reset();
151
+ closeModal();
152
+ }, [form, closeModal]);
153
+
154
+ const handleOpenAddCardModal = useCallback(() => {
155
+ openModal('addCardModal', {
156
+ returnToBuyCredits: true,
157
+ buyCreditsState: {
158
+ activeCardId: selectedCardId,
159
+ selectedValue: creditOption,
160
+ },
161
+ });
162
+ }, [openModal, selectedCardId, creditOption]);
163
+
164
+ const selectOptions = useMemo(() => {
165
+ return AI_CREDIT_OPTIONS.map((opt) => ({
166
+ value: opt.credits,
167
+ label: (
168
+ <div className="flex items-center justify-between gap-2 w-full">
169
+ <span className="paragraph-small-medium text-zinc-950 flex-1">
170
+ {opt.credits.toLocaleString('pt-BR')} créditos de IA
171
+ </span>
172
+ <span className="paragraph-small-medium text-zinc-500 opacity-70">{opt.priceLabel}</span>
173
+ </div>
174
+ ),
175
+ displayValue: `${opt.credits.toLocaleString('pt-BR')} créditos de IA`,
176
+ }));
177
+ }, []);
178
+
179
+ const onSubmit = useCallback(
180
+ async (data: BuyCreditFormData) => {
181
+ if (!subscriptionId) return;
182
+
183
+ const option = AI_CREDIT_OPTIONS.find((opt) => opt.credits === data.creditOption);
184
+ if (!option) return;
185
+
186
+ await updatePlanMutation.mutateAsync({
187
+ subscriptionId,
188
+ data: {
189
+ plan_extras: [
190
+ {
191
+ id_addon: ADDON_IDS.AI,
192
+ quantity: option.credits,
193
+ },
194
+ ],
195
+ },
196
+ });
197
+
198
+ toast.custom((t) => (
199
+ <Toast
200
+ variant="success"
201
+ message={`Créditos de IA alterados para ${data.creditOption.toLocaleString('pt-BR')}`}
202
+ toastId={t}
203
+ />
204
+ ));
205
+ form.reset();
206
+ closeModal();
207
+ },
208
+ [subscriptionId, updatePlanMutation, form, closeModal]
209
+ );
210
+
211
+ const renderPriceDisplay = useCallback(() => {
212
+ if (isCalculatePending) {
213
+ return <Skeleton className="h-7 w-24" />;
214
+ }
215
+ if (totalPrice !== null) {
216
+ return (
217
+ <span className="paragraph-xlarge-semibold text-zinc-950">
218
+ {totalPrice === 0 ? 'Incluso' : formatPrice(totalPrice)}
219
+ </span>
220
+ );
221
+ }
222
+ return <span className="paragraph-xlarge-semibold text-zinc-400">--</span>;
223
+ }, [isCalculatePending, totalPrice, formatPrice]);
224
+
225
+ return (
226
+ <Dialog open={open} onOpenChange={handleClose}>
227
+ <DialogContent
228
+ showCloseButton={false}
229
+ className="flex flex-col p-0! gap-0! max-w-full! border-0 rounded-t-2xl rounded-b-none h-dvh top-0! bottom-0! left-0! right-0! translate-x-0! translate-y-0! lg:max-w-[433px]! lg:h-auto! lg:max-h-[90vh] lg:rounded-lg lg:border lg:top-[50%]! lg:left-[50%]! lg:right-auto! lg:bottom-auto! lg:translate-x-[-50%]! lg:translate-y-[-50%]! overflow-hidden lg:overflow-y-auto"
230
+ >
231
+ <DialogHeader className="px-4 py-3 border-b border-zinc-200 shrink-0 relative">
232
+ <DialogTitle className="paragraph-medium-semibold text-zinc-950 text-center">
233
+ Comprar créditos de IA
234
+ </DialogTitle>
235
+ <button
236
+ type="button"
237
+ onClick={handleClose}
238
+ className="absolute right-3 top-1/2 -translate-y-1/2 size-8 flex items-center justify-center rounded-lg cursor-pointer hover:bg-zinc-100 transition-colors"
239
+ >
240
+ <IconX size={18} className="text-zinc-400" />
241
+ </button>
242
+ </DialogHeader>
243
+
244
+ <form
245
+ onSubmit={form.handleSubmit(onSubmit)}
246
+ className="flex flex-col flex-1 lg:flex-none overflow-hidden"
247
+ >
248
+ <div className="flex flex-col gap-6 lg:gap-8 p-4 lg:p-5 flex-1 lg:flex-none overflow-y-auto">
249
+ <div className="flex flex-col gap-4">
250
+ <SelectField
251
+ options={selectOptions}
252
+ value={creditOption}
253
+ onChange={handleCreditOptionChange}
254
+ className="h-12!"
255
+ />
256
+
257
+ <PaymentInfoCard
258
+ selectedCardId={selectedCardId}
259
+ onCardSelect={(cardId) =>
260
+ form.setValue('selectedCardId', cardId, { shouldValidate: true })
261
+ }
262
+ onOpenAddCard={handleOpenAddCardModal}
263
+ />
264
+
265
+ <div className="flex items-center gap-3 p-4 bg-cyan-50 rounded-lg">
266
+ <IconInfoCircle className="size-4 text-zinc-950 shrink-0" />
267
+ <span className="paragraph-small-regular text-zinc-950">
268
+ As alterações entrarão em vigor a partir da sua próxima fatura.
269
+ </span>
270
+ </div>
271
+ </div>
272
+
273
+ <Separator className="bg-zinc-200 hidden lg:block" />
274
+
275
+ <div className="hidden lg:flex items-center justify-between">
276
+ <span className="paragraph-small-medium text-zinc-500">Valor total do plano</span>
277
+ {renderPriceDisplay()}
278
+ </div>
279
+
280
+ <Button
281
+ type="submit"
282
+ disabled={isConfirmDisabled}
283
+ className="w-full hidden lg:inline-flex"
284
+ >
285
+ {isSubmitting && <IconLoader2 className="size-4 animate-spin" />}
286
+ Confirmar e pagar
287
+ </Button>
288
+ </div>
289
+
290
+ <div className="flex flex-col gap-4 p-4 border-t border-zinc-200 shrink-0 lg:hidden">
291
+ <div className="flex items-center justify-between">
292
+ <span className="paragraph-small-medium text-zinc-500">Valor total do plano</span>
293
+ {renderPriceDisplay()}
294
+ </div>
295
+ <Button type="submit" disabled={isConfirmDisabled} className="w-full">
296
+ {isSubmitting && <IconLoader2 className="size-4 animate-spin" />}
297
+ Confirmar e pagar
298
+ </Button>
299
+ </div>
300
+ </form>
301
+ </DialogContent>
302
+ </Dialog>
303
+ );
304
+ }
@@ -2,6 +2,7 @@
2
2
 
3
3
  import { useAppNavigationContext } from '../AppNavigationContext';
4
4
  import { Progress } from '../../../index';
5
+ import { useBuyCreditsModal } from '../../../store/useBuyCreditsModal';
5
6
 
6
7
  export interface CreditsCardProps {
7
8
  used: number;
@@ -11,14 +12,16 @@ export interface CreditsCardProps {
11
12
 
12
13
  export function CreditsCard({ used, total, onClick }: CreditsCardProps) {
13
14
  const { breakpoint } = useAppNavigationContext();
15
+ const { openModal } = useBuyCreditsModal();
16
+ const handleClick = onClick ?? (() => openModal());
14
17
 
15
18
  // Only renders in mobile overlay
16
19
  if (breakpoint !== 'mobile') return null;
17
20
 
18
21
  return (
19
22
  <div
20
- className={`flex flex-col bg-gray-950 rounded-lg gap-4 w-full px-4 pt-4 pb-[18px] ${onClick ? 'cursor-pointer' : ''}`}
21
- onClick={onClick}
23
+ className="flex flex-col bg-gray-950 rounded-lg gap-4 w-full px-4 pt-4 pb-[18px] cursor-pointer"
24
+ onClick={handleClick}
22
25
  >
23
26
  <div className="flex items-center justify-between">
24
27
  <span className="paragraph-xsmall-semibold text-white">Créditos usados</span>