11import { FormattedMessage , useIntl } from "react-intl" ;
22import { useNavigate } from "react-router-dom" ;
33
4+ import { Box } from "components/ui/Box" ;
45import { Button } from "components/ui/Button" ;
56
6- import { useCurrentWorkspace , useDeleteWorkspace } from "core/api" ;
7+ import { useCurrentOrganizationId } from "area/organization/utils" ;
8+ import {
9+ useCurrentWorkspace ,
10+ useDeleteWorkspace ,
11+ useIsLastWorkspaceInOrganization ,
12+ useGetOrganizationSubscriptionInfo ,
13+ useCancelSubscription ,
14+ } from "core/api" ;
15+ import { OrganizationPaymentConfigReadSubscriptionStatus } from "core/api/types/AirbyteClient" ;
16+ import { useOrganizationSubscriptionStatus } from "core/utils/useOrganizationSubscriptionStatus" ;
717import { useConfirmationModalService } from "hooks/services/ConfirmationModal" ;
818import { useNotificationService } from "hooks/services/Notification" ;
919import { RoutePaths } from "pages/routePaths" ;
1020
21+ import styles from "./DeleteWorkspace.module.scss" ;
22+
1123export const DeleteWorkspace : React . FC = ( ) => {
1224 const workspace = useCurrentWorkspace ( ) ;
13- const { mutateAsync : deleteWorkspace , isLoading : isDeletingWorkspace } = useDeleteWorkspace ( ) ;
25+ const organizationId = useCurrentOrganizationId ( ) ;
26+ const { mutateAsync : cancelSubscription , isLoading : isCancellingSubscription } = useCancelSubscription (
27+ organizationId || ""
28+ ) ;
1429 const { registerNotification } = useNotificationService ( ) ;
1530 const navigate = useNavigate ( ) ;
16- const { formatMessage } = useIntl ( ) ;
31+ const { formatMessage, formatDate } = useIntl ( ) ;
1732 const { openConfirmationModal, closeConfirmationModal } = useConfirmationModalService ( ) ;
1833 const redirectPathAfterDeletion = `/${ RoutePaths . Organization } /${ workspace . organizationId } ` ;
34+ const { subscriptionStatus, isStandardPlan } = useOrganizationSubscriptionStatus ( ) ;
35+
36+ // Check if this is the last workspace in the organization
37+ const { isLastWorkspace, isLoading : isCheckingLastWorkspace } = useIsLastWorkspaceInOrganization (
38+ organizationId ,
39+ workspace . workspaceId
40+ ) ;
41+
42+ // Fetch subscription info only if this is the last workspace
43+ const { data : subscriptionInfo } = useGetOrganizationSubscriptionInfo ( organizationId || "" , isLastWorkspace ) ;
44+ const hasActiveSubscription = subscriptionStatus === OrganizationPaymentConfigReadSubscriptionStatus . subscribed ;
45+ const shouldShowCancelSubscriptionWarning = isLastWorkspace && hasActiveSubscription && isStandardPlan ;
46+
47+ // Handle subscription cancellation after workspace deletion
48+ const handleCancelSubscription = async ( ) => {
49+ // Only attempt subscription cancellation if this is the last workspace, the organization has an active subscription, and is on Standard plan
50+ if ( organizationId && shouldShowCancelSubscriptionWarning ) {
51+ try {
52+ await cancelSubscription ( ) ;
53+ registerNotification ( {
54+ id : "settings.workspace.delete.success" ,
55+ text : formatMessage ( {
56+ id : "settings.workspaceSettings.delete.success.withSubscriptionCancellation" ,
57+ } ) ,
58+ type : "success" ,
59+ } ) ;
60+ } catch ( subscriptionError ) {
61+ registerNotification ( {
62+ id : "settings.workspace.delete.subscription.cancellation.failed" ,
63+ text : formatMessage ( {
64+ id : "settings.workspaceSettings.delete.warning.subscriptionCancellationFailed" ,
65+ } ) ,
66+ type : "warning" ,
67+ } ) ;
68+ }
69+ } else {
70+ registerNotification ( {
71+ id : "settings.workspace.delete.success" ,
72+ text : formatMessage ( { id : "settings.workspaceSettings.delete.success" } ) ,
73+ type : "success" ,
74+ } ) ;
75+ }
76+ } ;
77+
78+ const { mutateAsync : deleteWorkspace , isLoading : isDeletingWorkspace } = useDeleteWorkspace ( ) ;
79+
80+ const cancellationDate = subscriptionInfo ?. upcomingInvoice ?. dueDate
81+ ? formatDate ( new Date ( subscriptionInfo . upcomingInvoice . dueDate ) , { dateStyle : "medium" } )
82+ : "" ;
83+
84+ const onRemoveWorkspaceClick = ( ) => {
85+ const modalText = shouldShowCancelSubscriptionWarning ? (
86+ < Box className = { styles . warningBox } >
87+ < Box pb = "md" >
88+ < FormattedMessage id = "settings.workspaceSettings.deleteWorkspace.confirmation.text" />
89+ </ Box >
90+
91+ < FormattedMessage
92+ id = "settings.workspaceSettings.deleteLastWorkspace.confirmation"
93+ values = { { planName : subscriptionInfo ?. name ?? "" , date : cancellationDate } }
94+ />
95+ </ Box >
96+ ) : (
97+ "settings.workspaceSettings.deleteWorkspace.confirmation.text"
98+ ) ;
1999
20- const onRemoveWorkspaceClick = ( ) =>
21100 openConfirmationModal ( {
22- text : `settings.workspaceSettings.deleteWorkspace.confirmation.text` ,
101+ text : modalText ,
23102 title : (
24103 < FormattedMessage
25104 id = "settings.workspaceSettings.deleteWorkspace.confirmation.title"
@@ -30,19 +109,20 @@ export const DeleteWorkspace: React.FC = () => {
30109 confirmationText : workspace . name ,
31110 onSubmit : async ( ) => {
32111 await deleteWorkspace ( workspace . workspaceId ) ;
33- registerNotification ( {
34- id : "settings.workspace.delete.success" ,
35- text : formatMessage ( { id : "settings.workspaceSettings.delete.success" } ) ,
36- type : "success" ,
37- } ) ;
112+ await handleCancelSubscription ( ) ;
38113 navigate ( redirectPathAfterDeletion ) ;
39114 closeConfirmationModal ( ) ;
40115 } ,
41116 submitButtonDataId : "reset" ,
42117 } ) ;
118+ } ;
43119
44120 return (
45- < Button isLoading = { isDeletingWorkspace } variant = "danger" onClick = { onRemoveWorkspaceClick } >
121+ < Button
122+ isLoading = { isCheckingLastWorkspace || isDeletingWorkspace || isCancellingSubscription }
123+ variant = "danger"
124+ onClick = { onRemoveWorkspaceClick }
125+ >
46126 < FormattedMessage id = "settings.workspaceSettings.deleteLabel" />
47127 </ Button >
48128 ) ;
0 commit comments