11"use client" ;
22
3- import { useEffect , useMemo , useState } from "react" ;
3+ import { useMemo , useState } from "react" ;
44import { DropdownMenu } from "radix-ui" ;
5+ import BogBanner from "@/components/bog/BogBanner/BogBanner" ;
56import BogButton from "@/components/bog/BogButton/BogButton" ;
67import BogIcon from "@/components/bog/BogIcon/BogIcon" ;
78import BogTextInput from "@/components/bog/BogTextInput/BogTextInput" ;
@@ -12,8 +13,12 @@ import { cn } from "@/lib/utils";
1213import authClient from "@/lib/authClient" ;
1314import { useActiveOrganization } from "@/lib/hooks/useActiveOrganization" ;
1415import { isAdmin } from "@/lib/clientUtils" ;
16+ import { useEmailRecipients } from "@/lib/hooks/useEmailRecipients" ;
1517import SendEmailModal from "@/components/SendEmailModal" ;
16- import api from "@/lib/api" ;
18+ import {
19+ EMAIL_SENT_MESSAGE ,
20+ postOrganizationEmail ,
21+ } from "@/lib/organizationEmail" ;
1722
1823type FilterValue = "all" | NotificationType ;
1924
@@ -83,6 +88,7 @@ export default function InboxPage() {
8388 const [ search , setSearch ] = useState ( "" ) ;
8489 const [ filterType , setFilterType ] = useState < FilterValue > ( "all" ) ;
8590 const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
91+ const [ emailSent , setEmailSent ] = useState ( false ) ;
8692
8793 const {
8894 allNotifications,
@@ -112,32 +118,8 @@ export default function InboxPage() {
112118 } , [ notifications , search ] ) ;
113119
114120 const organizationId = organization ?. data ?. id ;
115- const [ recipients , setRecipients ] = useState < { id : string ; name : string } [ ] > (
116- [ ] ,
117- ) ;
118-
119- useEffect ( ( ) => {
120- if ( ! organizationId ) return ;
121- api . members
122- . $get ( { query : { page : "1" , pageSize : "100" } } )
123- . then ( ( res ) => {
124- if ( res . ok ) {
125- return res . json ( ) ;
126- }
127- throw new Error ( "Failed to fetch members" ) ;
128- } )
129- . then ( ( resJson ) => {
130- if ( "data" in resJson ) {
131- setRecipients (
132- resJson . data . map ( ( m : { userId : string ; name : string } ) => ( {
133- id : m . userId ,
134- name : m . name ,
135- } ) ) ,
136- ) ;
137- }
138- } )
139- . catch ( ( err ) => console . error ( err ) ) ;
140- } , [ organizationId ] ) ;
121+ const { recipients, recipientsLoading, recipientsError, retryRecipients } =
122+ useEmailRecipients ( organizationId , isModalOpen && isAuthorizedAdmin ) ;
141123
142124 if ( isLoading ) {
143125 return (
@@ -172,13 +154,25 @@ export default function InboxPage() {
172154 < BogButton
173155 variant = "primary"
174156 size = "medium"
175- onClick = { ( ) => setIsModalOpen ( true ) }
157+ onClick = { ( ) => {
158+ setEmailSent ( false ) ;
159+ setIsModalOpen ( true ) ;
160+ } }
176161 >
177162 New +
178163 </ BogButton >
179164 ) }
180165 </ div >
181166
167+ { emailSent && (
168+ < BogBanner
169+ type = "success"
170+ variant = "surface"
171+ role = "status"
172+ content = { < span > { EMAIL_SENT_MESSAGE } </ span > }
173+ />
174+ ) }
175+
182176 < div className = "flex flex-col gap-3 mobile:flex-row mobile:items-center mobile:justify-between" >
183177 < div className = "flex" >
184178 < button
@@ -237,7 +231,7 @@ export default function InboxPage() {
237231 { FILTER_OPTIONS . map ( ( option ) => (
238232 < DropdownMenu . Item
239233 key = { option . value }
240- className = "flex cursor-pointer items-center gap-3 px-4 py-2 text-paragraph-2 text-grey-text-strong outline-none data-[ highlighted] :bg-grey-fill-weaker"
234+ className = "flex cursor-pointer items-center gap-3 px-4 py-2 text-paragraph-2 text-grey-text-strong outline-none data-highlighted:bg-grey-fill-weaker"
241235 onSelect = { ( ) => setFilterType ( option . value ) }
242236 >
243237 < span
@@ -311,20 +305,19 @@ export default function InboxPage() {
311305 </ div >
312306 </ div >
313307 < SendEmailModal
314- isOpen = { isModalOpen }
308+ key = { organizationId }
309+ isOpen = { isModalOpen && isAuthorizedAdmin }
315310 onClose = { ( ) => setIsModalOpen ( false ) }
316311 recipients = { recipients }
317- onSend = { async ( { subject, subtitle, body, footer, recipientIds } ) => {
318- if ( ! organizationId ) return ;
319- const res = await api . emails . $post ( {
320- json : { subject, subtitle, body, footer, recipientIds } ,
321- } ) ;
322- if ( ! res . ok ) {
323- const data = await res . json ( ) . catch ( ( ) => ( { } ) ) ;
324- throw new Error (
325- ( data as { error ?: string } ) . error ?? "Failed to send email" ,
326- ) ;
312+ recipientsLoading = { recipientsLoading }
313+ recipientsError = { recipientsError }
314+ onRetryRecipients = { retryRecipients }
315+ onSend = { async ( values ) => {
316+ if ( ! organizationId ) {
317+ throw new Error ( "Failed to send email" ) ;
327318 }
319+ await postOrganizationEmail ( values ) ;
320+ setEmailSent ( true ) ;
328321 } }
329322 />
330323 </ div >
0 commit comments