File size: 20,632 Bytes
ec4551b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | 'use client';
import { Slider } from '@gitroom/react/form/slider';
import React, { FC, useCallback, useEffect, useMemo, useState } from 'react';
import { Button } from '@gitroom/react/form/button';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { Subscription } from '@prisma/client';
import { useDebouncedCallback } from 'use-debounce';
import ReactLoading from '@gitroom/frontend/components/layout/loading';
import { deleteDialog } from '@gitroom/react/helpers/delete.dialog';
import { useToaster } from '@gitroom/react/toaster/toaster';
import dayjs from 'dayjs';
import clsx from 'clsx';
import { pricing } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/pricing';
import { FAQComponent } from '@gitroom/frontend/components/billing/faq.component';
import { useSWRConfig } from 'swr';
import { useUser } from '@gitroom/frontend/components/layout/user.context';
import { useRouter, useSearchParams } from 'next/navigation';
import { useVariables } from '@gitroom/react/helpers/variable.context';
import { useModals } from '@gitroom/frontend/components/layout/new-modal';
import { Textarea } from '@gitroom/react/form/textarea';
import { useFireEvents } from '@gitroom/helpers/utils/use.fire.events';
import { useUtmUrl } from '@gitroom/helpers/utils/utm.saver';
import { useTrack } from '@gitroom/react/helpers/use.track';
import { TrackEnum } from '@gitroom/nestjs-libraries/user/track.enum';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
import { FinishTrial } from '@gitroom/frontend/components/billing/finish.trial';
import { newDayjs } from '@gitroom/frontend/components/layout/set.timezone';
import { useDubClickId } from '@gitroom/frontend/components/layout/dubAnalytics';
import { LogoutComponent } from '@gitroom/frontend/components/layout/logout.component';
export const Prorate: FC<{
period: 'MONTHLY' | 'YEARLY';
pack: 'STANDARD' | 'PRO';
}> = (props) => {
const { period, pack } = props;
const t = useT();
const fetch = useFetch();
const [price, setPrice] = useState<number | false>(0);
const [loading, setLoading] = useState(false);
const calculatePrice = useDebouncedCallback(async () => {
setLoading(true);
setPrice(
(
await (
await fetch('/billing/prorate', {
method: 'POST',
body: JSON.stringify({
period,
billing: pack,
}),
})
).json()
).price
);
setLoading(false);
}, 500);
useEffect(() => {
setPrice(false);
calculatePrice();
}, [period, pack]);
if (loading) {
return (
<div className="pt-[12px]">
<ReactLoading type="spin" color="#fff" width={20} height={20} />
</div>
);
}
if (price === false) {
return null;
}
return (
<div className="text-[12px] flex pt-[12px]">
({t('pay_today', 'Pay Today')} ${(price < 0 ? 0 : price)?.toFixed(1)})
</div>
);
};
export const Features: FC<{
pack: 'FREE' | 'STANDARD' | 'PRO';
}> = (props) => {
const { pack } = props;
const features = useMemo(() => {
const currentPricing = pricing[pack];
const channelsOr = currentPricing.channel;
const list = [];
list.push(`${channelsOr} ${channelsOr === 1 ? 'channel' : 'channels'}`);
list.push(
`${
currentPricing.posts_per_month > 10000
? 'Unlimited'
: currentPricing.posts_per_month
} posts per month`
);
if (currentPricing.team_members) {
list.push(`Unlimited team members`);
}
if (currentPricing?.ai) {
list.push(`AI auto-complete`);
list.push(`AI copilots`);
list.push(`AI Autocomplete`);
}
list.push(`Advanced Picture Editor`);
if (currentPricing?.image_generator) {
list.push(
`${currentPricing?.image_generation_count} AI Images per month`
);
}
if (currentPricing?.generate_videos) {
list.push(`${currentPricing?.generate_videos} AI Videos per month`);
}
return list;
}, [pack]);
return (
<div className="flex flex-col gap-[10px] justify-center text-[16px] text-customColor18">
{features.map((feature) => (
<div key={feature} className="flex gap-[20px]">
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
>
<path
d="M16.2806 9.21937C16.3504 9.28903 16.4057 9.37175 16.4434 9.46279C16.4812 9.55384 16.5006 9.65144 16.5006 9.75C16.5006 9.84856 16.4812 9.94616 16.4434 10.0372C16.4057 10.1283 16.3504 10.211 16.2806 10.2806L11.0306 15.5306C10.961 15.6004 10.8783 15.6557 10.7872 15.6934C10.6962 15.7312 10.5986 15.7506 10.5 15.7506C10.4014 15.7506 10.3038 15.7312 10.2128 15.6934C10.1218 15.6557 10.039 15.6004 9.96938 15.5306L7.71938 13.2806C7.57865 13.1399 7.49959 12.949 7.49959 12.75C7.49959 12.551 7.57865 12.3601 7.71938 12.2194C7.86011 12.0786 8.05098 11.9996 8.25 11.9996C8.44903 11.9996 8.6399 12.0786 8.78063 12.2194L10.5 13.9397L15.2194 9.21937C15.289 9.14964 15.3718 9.09432 15.4628 9.05658C15.5538 9.01884 15.6514 8.99941 15.75 8.99941C15.8486 8.99941 15.9462 9.01884 16.0372 9.05658C16.1283 9.09432 16.211 9.14964 16.2806 9.21937ZM21.75 12C21.75 13.9284 21.1782 15.8134 20.1068 17.4168C19.0355 19.0202 17.5127 20.2699 15.7312 21.0078C13.9496 21.7458 11.9892 21.9389 10.0979 21.5627C8.20656 21.1865 6.46928 20.2579 5.10571 18.8943C3.74215 17.5307 2.81355 15.7934 2.43735 13.9021C2.06114 12.0108 2.25422 10.0504 2.99218 8.26884C3.73013 6.48726 4.97982 4.96451 6.58319 3.89317C8.18657 2.82183 10.0716 2.25 12 2.25C14.585 2.25273 17.0634 3.28084 18.8913 5.10872C20.7192 6.93661 21.7473 9.41498 21.75 12ZM20.25 12C20.25 10.3683 19.7661 8.77325 18.8596 7.41655C17.9531 6.05984 16.6646 5.00242 15.1571 4.37799C13.6497 3.75357 11.9909 3.59019 10.3905 3.90852C8.79017 4.22685 7.32016 5.01259 6.16637 6.16637C5.01259 7.32015 4.22685 8.79016 3.90853 10.3905C3.5902 11.9908 3.75358 13.6496 4.378 15.1571C5.00242 16.6646 6.05984 17.9531 7.41655 18.8596C8.77326 19.7661 10.3683 20.25 12 20.25C14.1873 20.2475 16.2843 19.3775 17.8309 17.8309C19.3775 16.2843 20.2475 14.1873 20.25 12Z"
fill="#06ff00"
/>
</svg>
</div>
<div>{feature}</div>
</div>
))}
</div>
);
};
const Accept: FC<{ resolve: (res: boolean) => void }> = ({ resolve }) => {
const [loading, setLoading] = useState(false);
const fetch = useFetch();
const toaster = useToaster();
const apply = useCallback(async () => {
setLoading(true);
await fetch('/billing/apply-discount', {
method: 'POST',
});
resolve(true);
toaster.show('50% discount applied successfully');
}, []);
return (
<div>
<div className="mb-[20px]">
Would you accept 50% discount for 3 months instead? 🙏🏻
</div>
<div className="flex gap-[10px]">
<Button loading={loading} onClick={apply}>
Apply 50% discount for 3 months
</Button>
<Button onClick={() => resolve(false)} className="!bg-red-800">
Cancel my subscription
</Button>
</div>
</div>
);
};
const Info: FC<{
proceed: (feedback: string) => void;
}> = (props) => {
const [feedback, setFeedback] = useState('');
const modal = useModals();
const events = useFireEvents();
const cancel = useCallback(() => {
props.proceed(feedback);
events('cancel_subscription');
modal.closeAll();
}, [modal, feedback]);
const t = useT();
return (
<div className="relative flex gap-[20px] flex-col flex-1 rounded-[4px]">
<div>
{t(
'would_you_mind_shortly_tell_us_what_we_could_have_done_better',
'Would you mind shortly tell us what we could have done better?'
)}
</div>
<div>
<Textarea
className="bg-newBgColorInner"
label={'Feedback'}
name="feedback"
disableForm={true}
value={feedback}
onChange={(e) => setFeedback(e.target.value)}
/>
</div>
<div>
<Button disabled={feedback.length < 20} onClick={cancel}>
{feedback.length < 20
? t('please_add_at_least', 'Please add at least 20 chars')
: t('cancel_subscription', 'Cancel Subscription')}
</Button>
</div>
</div>
);
};
export const MainBillingComponent: FC<{
sub?: Subscription;
}> = (props) => {
const { sub } = props;
const { isGeneral } = useVariables();
const { mutate } = useSWRConfig();
const fetch = useFetch();
const toast = useToaster();
const user = useUser();
const dub = useDubClickId();
const modal = useModals();
const router = useRouter();
const utm = useUtmUrl();
const track = useTrack();
const t = useT();
const queryParams = useSearchParams();
const [finishTrial, setFinishTrial] = useState(
!!queryParams.get('finishTrial')
);
const [subscription, setSubscription] = useState<Subscription | undefined>(
sub
);
const [loading, setLoading] = useState<boolean>(false);
const [period, setPeriod] = useState<'MONTHLY' | 'YEARLY'>(
subscription?.period || 'MONTHLY'
);
const [monthlyOrYearly, setMonthlyOrYearly] = useState<'on' | 'off'>(
period === 'MONTHLY' ? 'off' : 'on'
);
const [initialChannels, setInitialChannels] = useState(
sub?.totalChannels || 1
);
useEffect(() => {
if (initialChannels !== sub?.totalChannels) {
setInitialChannels(sub?.totalChannels || 1);
}
if (period !== sub?.period) {
setPeriod(sub?.period || 'MONTHLY');
setMonthlyOrYearly(
(sub?.period || 'MONTHLY') === 'MONTHLY' ? 'off' : 'on'
);
}
setSubscription(sub);
}, [sub]);
const updatePayment = useCallback(async () => {
const { portal } = await (await fetch('/billing/portal')).json();
window.location.href = portal;
}, []);
const currentPackage = useMemo(() => {
if (!subscription) {
return 'FREE';
}
if (period === 'YEARLY' && monthlyOrYearly === 'off') {
return '';
}
if (period === 'MONTHLY' && monthlyOrYearly === 'on') {
return '';
}
return subscription?.subscriptionTier;
}, [subscription, initialChannels, monthlyOrYearly, period]);
const moveToCheckout = useCallback(
(billing: 'STANDARD' | 'PRO' | 'FREE', reactivate = false) =>
async () => {
if (reactivate) {
setLoading(true);
const { cancel_at } = await (
await fetch('/billing/cancel', {
method: 'POST',
body: JSON.stringify({
feedback: '',
}),
headers: {
'Content-Type': 'application/json',
},
})
).json();
setSubscription((subs) => ({
...subs!,
cancelAt: cancel_at,
}));
toast.show('Subscription reactivated successfully');
setLoading(false);
return;
}
const messages = [];
if (
!pricing[billing].team_members &&
pricing[subscription?.subscriptionTier!]?.team_members
) {
messages.push(
`Your team members will be removed from your organization`
);
}
if (billing === 'FREE') {
if (
subscription?.cancelAt ||
(await deleteDialog(
`Are you sure you want to cancel your subscription?
${messages.join(', ')}`,
'Yes, cancel',
'Cancel Subscription'
))
) {
const checkDiscount = await (
await fetch('/billing/check-discount')
).json();
if (checkDiscount.offerCoupon) {
const info = await new Promise((res) => {
modal.openModal({
title: 'Before you cancel',
withCloseButton: true,
classNames: {
modal: 'bg-transparent text-textColor',
},
children: <Accept resolve={res} />,
});
});
modal.closeAll();
if (info) {
return;
}
}
const info = await new Promise((res) => {
modal.openModal({
title: t(
'we_are_sorry_to_see_you_go',
'We are sorry to see you go :('
),
withCloseButton: true,
classNames: {
modal: 'bg-transparent text-textColor',
},
children: <Info proceed={(e) => res(e)} />,
});
});
setLoading(true);
const { cancel_at } = await (
await fetch('/billing/cancel', {
method: 'POST',
body: JSON.stringify({
feedback: info,
}),
headers: {
'Content-Type': 'application/json',
},
})
).json();
setSubscription((subs) => ({
...subs!,
cancelAt: cancel_at,
}));
if (cancel_at)
toast.show('Subscription set to canceled successfully');
setLoading(false);
}
return;
}
if (
messages.length &&
!(await deleteDialog(messages.join(', '), 'Yes, continue'))
) {
return;
}
setLoading(true);
const { url, portal } = await (
await fetch('/billing/subscribe', {
method: 'POST',
body: JSON.stringify({
period: monthlyOrYearly === 'on' ? 'YEARLY' : 'MONTHLY',
utm,
billing,
...(dub ? { dub } : {}),
}),
})
).json();
if (url) {
await track(TrackEnum.InitiateCheckout, {
value:
pricing[billing][
monthlyOrYearly === 'on' ? 'year_price' : 'month_price'
],
});
window.location.href = url;
return;
}
if (portal) {
if (
await deleteDialog(
'We could not charge your credit card, please update your payment method',
'Update',
'Payment Method Required'
)
) {
window.open(portal);
}
} else {
setPeriod(monthlyOrYearly === 'on' ? 'YEARLY' : 'MONTHLY');
setSubscription((subs) => ({
...subs!,
subscriptionTier: billing,
cancelAt: null,
}));
mutate(
'/user/self',
{
...user,
tier: billing,
},
{
revalidate: false,
}
);
toast.show('Subscription updated successfully');
}
setLoading(false);
},
[monthlyOrYearly, subscription, user, utm]
);
if (user?.isLifetime) {
router.replace('/');
return null;
}
return (
<div className="flex flex-col gap-[16px]">
<div className="flex flex-row">
<div className="flex-1 text-[20px]">{t('plans', 'Plans')}</div>
<div className="flex items-center gap-[16px]">
<div>{t('monthly', 'MONTHLY')}</div>
<div>
<Slider value={monthlyOrYearly} onChange={setMonthlyOrYearly} />
</div>
<div>{t('yearly', 'YEARLY')}</div>
</div>
</div>
{finishTrial && <FinishTrial close={() => setFinishTrial(false)} />}
<div className="flex gap-[16px] [@media(max-width:1024px)]:flex-col [@media(max-width:1024px)]:text-center">
{Object.entries(pricing)
.filter((f) => !isGeneral || f[0] !== 'FREE')
.map(([name, values]) => (
<div
key={name}
className="flex-1 bg-sixth border border-customColor6 rounded-[4px] p-[24px] gap-[16px] flex flex-col [@media(max-width:1024px)]:items-center"
>
<div className="text-[18px]">{name}</div>
<div className="text-[38px] flex gap-[2px] items-center">
<div>
$
{monthlyOrYearly === 'on'
? values.year_price
: values.month_price}
</div>
<div className={`text-[14px] text-customColor18`}>
{monthlyOrYearly === 'on' ? '/year' : '/month'}
</div>
</div>
<div className="text-[14px] flex gap-[10px]">
{currentPackage === name.toUpperCase() &&
subscription?.cancelAt ? (
<div className="gap-[3px] flex flex-col">
<div>
<Button
onClick={moveToCheckout('FREE', true)}
loading={loading}
>
{t(
'reactivate_subscription',
'Reactivate subscription'
)}
</Button>
</div>
</div>
) : (
<Button
loading={loading}
disabled={
(!!subscription?.cancelAt &&
name.toUpperCase() === 'FREE') ||
currentPackage === name.toUpperCase()
}
className={clsx(
subscription &&
name.toUpperCase() === 'FREE' &&
'!bg-red-500'
)}
onClick={moveToCheckout(
name.toUpperCase() as 'STANDARD' | 'PRO'
)}
>
{currentPackage === name.toUpperCase()
? 'Current Plan'
: name.toUpperCase() === 'FREE'
? subscription?.cancelAt
? `Downgrade on ${dayjs
.utc(subscription?.cancelAt)
.local()
.format('D MMM, YYYY')}`
: 'Cancel subscription'
: // @ts-ignore
(user?.tier === 'FREE' ||
user?.tier?.current === 'FREE') &&
user.allowTrial
? t('start_7_days_free_trial', 'Start 7 days free trial')
: 'Purchase'}
</Button>
)}
{subscription &&
currentPackage !== name.toUpperCase() &&
name !== 'FREE' &&
!!name && (
<Prorate
period={monthlyOrYearly === 'on' ? 'YEARLY' : 'MONTHLY'}
pack={name.toUpperCase() as 'STANDARD' | 'PRO'}
/>
)}
</div>
<Features
pack={name.toUpperCase() as 'FREE' | 'STANDARD' | 'PRO'}
/>
</div>
))}
</div>
{!!subscription?.id && (
<div className="flex justify-center mt-[20px] gap-[10px]">
<Button onClick={updatePayment}>
{t(
'update_payment_method_invoices_history',
'Update Payment Method / Invoices History'
)}
</Button>
{isGeneral && !subscription?.cancelAt && (
<Button
className="bg-red-500"
loading={loading}
onClick={moveToCheckout('FREE')}
>
{t('cancel_subscription_1', 'Cancel subscription')}
</Button>
)}
</div>
)}
{subscription?.cancelAt && isGeneral && (
<div className="text-center">
{t(
'your_subscription_will_be_canceled_at',
'Your subscription will be canceled at'
)}{' '}
{newDayjs(subscription.cancelAt).local().format('D MMM, YYYY')}
<br />
{t(
'you_will_never_be_charged_again',
'You will never be charged again'
)}
</div>
)}
<FAQComponent />
<div className="flex justify-center mt-[20px]">
<LogoutComponent />
</div>
</div>
);
};
|