@@ -4,98 +4,144 @@ import {
44 Box ,
55 Button ,
66 Card ,
7- DescriptionList ,
8- DescriptionListProps ,
97 InlineStack ,
108 Text ,
119 Banner ,
1210 Badge ,
11+ Modal ,
1312} from "@shopify/polaris" ;
1413import { ProgressBar } from "./ProgressBar" ;
15- import { TrievePlan } from "trieve-ts-sdk" ;
14+ import { StripePlan } from "trieve-ts-sdk" ;
15+ import { useState } from "react" ;
16+ import { useQuery } from "@tanstack/react-query" ;
17+ import { organizationUsageQuery } from "app/queries/usage" ;
18+ import { useTrieve } from "app/context/trieveContext" ;
1619
17- export const PlanView = ( {
18- plan,
19- planItems,
20- setShowCancelModal,
21- usagePercentage,
22- } : {
23- plan : TrievePlan | null | undefined ;
24- planItems : DescriptionListProps [ "items" ] ;
25- setShowCancelModal : ( show : boolean ) => void ;
26- usagePercentage : number ;
27- } ) => {
20+ export const PlanView = ( ) => {
21+ const { organization, trieve, refetch : refetchTrieve } = useTrieve ( ) ;
22+ const [ showCancelModal , setShowCancelModal ] = useState ( false ) ;
2823 const submit = useSubmit ( ) ;
2924
25+ const { data : organizationUsage } = useQuery ( organizationUsageQuery ( trieve ) ) ;
26+
27+ let planItems = [ ] ;
28+
29+ if ( organization ?. plan ?. type === "flat" ) {
30+ planItems . push ( {
31+ term : "Message Usage" ,
32+ description : `${ organizationUsage ?. current_months_message_count ?. toLocaleString ( ) ?? 0 } / ${ ( ( organization ?. plan as StripePlan ) ?. messages_per_month ?? 1000 ) . toLocaleString ( ) } ` ,
33+ } ) ;
34+ }
35+
36+ const usagePercentage =
37+ ( ( organizationUsage ?. current_months_message_count ?? 0 ) /
38+ ( ( organization ?. plan as StripePlan ) ?. messages_per_month ?? 1000 ) ) *
39+ 100 ;
40+
3041 return (
31- < Card >
32- < BlockStack gap = "400" >
33- < Box paddingInline = "400" paddingBlockStart = "400" >
34- < InlineStack align = "space-between" >
35- < InlineStack gap = "400" align = "center" blockAlign = "center" >
42+ < >
43+ < Modal
44+ open = { showCancelModal }
45+ onClose = { ( ) => {
46+ setShowCancelModal ( false ) ;
47+ } }
48+ title = "Cancel Subscription"
49+ >
50+ < div className = "flex flex-col gap-4 p-4" >
51+ < Text as = "p" > Do you want to cancel your subscription?</ Text >
52+ < Button
53+ onClick = { ( ) => {
54+ submit (
55+ {
56+ action : "cancel" ,
57+ } ,
58+ {
59+ method : "post" ,
60+ } ,
61+ ) ;
62+ setShowCancelModal ( false ) ;
63+ setTimeout ( ( ) => {
64+ refetchTrieve ( ) ;
65+ } , 5000 ) ;
66+ } }
67+ >
68+ Cancel Subscription
69+ </ Button >
70+ </ div >
71+ </ Modal >
72+ < Card >
73+ < BlockStack >
74+ < div className = "pb-4" >
75+ < InlineStack align = "space-between" >
3676 < Text variant = "headingMd" as = "h2" >
3777 Plan Details
3878 </ Text >
39- < Badge > { plan ?. name } </ Badge >
40- </ InlineStack >
41- < InlineStack gap = "200" align = "center" blockAlign = "center" >
42- < Button
43- onClick = { ( ) => {
44- submit (
45- {
46- action : "modify" ,
47- } ,
48- {
49- method : "post" ,
50- } ,
51- ) ;
52- } }
53- >
54- Modify
55- </ Button >
56- < Button
57- onClick = { ( ) => {
58- setShowCancelModal ( true ) ;
59- } }
60- >
61- Cancel
62- </ Button >
79+ < Badge > { organization . plan ?. name } </ Badge >
6380 </ InlineStack >
64- </ InlineStack >
65- </ Box >
81+ </ div >
6682
67- < Box paddingInline = "400" paddingBlockEnd = "400" >
68- { usagePercentage >= 80 && usagePercentage < 90 && (
69- < Box paddingBlockEnd = "400" >
70- < Banner
71- title = { `You are at ${ usagePercentage . toPrecision ( 5 ) } % of your usage limit.` }
72- tone = "warning"
73- >
74- < p >
75- Consider upgrading your plan to avoid potential disruptions.
76- </ p >
77- </ Banner >
78- </ Box >
79- ) }
80- { usagePercentage >= 90 && (
81- < Box paddingBlockEnd = "400" >
82- < Banner
83- title = { `You have reached ${ usagePercentage . toPrecision ( 5 ) } % of your usage limit.` }
84- tone = "critical"
85- >
86- < p >
87- Upgrade your plan immediately to avoid service disruption.
88- </ p >
89- </ Banner >
90- </ Box >
91- ) }
92- < BlockStack >
93- < span className = "text-sm font-bold pb-1" > Usage</ span >
94- < ProgressBar progress = { usagePercentage } />
95- </ BlockStack >
96- < DescriptionList items = { planItems } />
97- </ Box >
98- </ BlockStack >
99- </ Card >
83+ < Box >
84+ { usagePercentage >= 80 && usagePercentage < 90 && (
85+ < Box >
86+ < Banner
87+ title = { `You are at ${ usagePercentage . toPrecision ( 5 ) } % of your usage limit.` }
88+ tone = "warning"
89+ >
90+ < p >
91+ Consider upgrading your plan to avoid potential disruptions.
92+ </ p >
93+ </ Banner >
94+ </ Box >
95+ ) }
96+ { usagePercentage >= 90 && (
97+ < Box >
98+ < Banner
99+ title = { `You have reached ${ usagePercentage . toPrecision ( 5 ) } % of your usage limit.` }
100+ tone = "critical"
101+ >
102+ < p >
103+ Upgrade your plan immediately to avoid service disruption.
104+ </ p >
105+ </ Banner >
106+ </ Box >
107+ ) }
108+ < BlockStack >
109+ < span className = "text-sm font-bold pb-1" > Usage</ span >
110+ < ProgressBar progress = { usagePercentage } />
111+ </ BlockStack >
112+ { planItems . map ( ( item ) => (
113+ < div className = "py-2" >
114+ < div className = "text-sm font-bold pb-1" > { item . term } </ div >
115+ < div > { item . description } </ div >
116+ </ div >
117+ ) ) }
118+ </ Box >
119+ </ BlockStack >
120+ < div className = "h-2" > </ div >
121+ < InlineStack gap = "200" align = "end" blockAlign = "center" >
122+ < Button
123+ onClick = { ( ) => {
124+ submit (
125+ {
126+ action : "modify" ,
127+ } ,
128+ {
129+ method : "post" ,
130+ } ,
131+ ) ;
132+ } }
133+ >
134+ Modify Plan
135+ </ Button >
136+ < Button
137+ onClick = { ( ) => {
138+ setShowCancelModal ( true ) ;
139+ } }
140+ >
141+ Cancel Plan
142+ </ Button >
143+ </ InlineStack >
144+ </ Card >
145+ </ >
100146 ) ;
101147} ;
0 commit comments