1- import { AlertDialog , DataTable , EmptyState , Flex } from "@raystack/apsara" ;
1+ import { AlertDialog , Button , DataTable , EmptyState , Flex } from "@raystack/apsara" ;
22import type { DataTableQuery , DataTableSort } from "@raystack/apsara" ;
33import { PageTitle } from "~/admin/components/PageTitle" ;
44import styles from "./members.module.css" ;
55import { useContext , useEffect , useMemo , useState } from "react" ;
66import { getColumns } from "./columns" ;
7- import type { SearchOrganizationUsersResponse_OrganizationUser } from "@raystack/proton/frontier" ;
8- import { AdminServiceQueries } from "@raystack/proton/frontier" ;
7+ import type {
8+ Invitation ,
9+ SearchOrganizationUsersResponse_OrganizationUser ,
10+ } from "@raystack/proton/frontier" ;
11+ import {
12+ AdminServiceQueries ,
13+ FrontierServiceQueries ,
14+ ListOrganizationInvitationsRequestSchema ,
15+ } from "@raystack/proton/frontier" ;
16+ import { create } from "@bufbuild/protobuf" ;
917import {
1018 useInfiniteQuery ,
19+ useQuery ,
1120 createConnectQueryKey ,
1221 useTransport
1322} from '@connectrpc/connect-query' ;
@@ -24,9 +33,13 @@ import {
2433import { transformDataTableQueryToRQLRequest } from '~/utils/transform-query' ;
2534import { useDebouncedValue } from '~hooks' ;
2635import { useTerminology } from "~/admin/hooks/useTerminology" ;
36+ import { InvitedMembersDialog } from './invited-members-dialog' ;
2737
2838const updateRoleDialogHandle = AlertDialog . createHandle < UpdateRolePayload > ( ) ;
2939
40+ // Stable ref: a fresh [] each render would remount the invites table.
41+ const NO_INVITATIONS : Invitation [ ] = [ ] ;
42+
3043const DEFAULT_SORT : DataTableSort = { name : 'orgJoinedAt' , order : 'desc' } ;
3144const INITIAL_QUERY : DataTableQuery = {
3245 offset : 0 ,
@@ -103,6 +116,24 @@ export function OrganizationMembersView() {
103116 user : SearchOrganizationUsersResponse_OrganizationUser | null ;
104117 } > ( { isOpen : false , user : null } ) ;
105118
119+ const [ isInvitesDialogOpen , setIsInvitesDialogOpen ] = useState ( false ) ;
120+
121+ // Not in the dialog: the toolbar needs the count before it mounts.
122+ const {
123+ data : invitations = NO_INVITATIONS ,
124+ isLoading : isInvitationsLoading ,
125+ error : invitationsError ,
126+ } = useQuery (
127+ FrontierServiceQueries . listOrganizationInvitations ,
128+ create ( ListOrganizationInvitationsRequestSchema , {
129+ orgId : organizationId ,
130+ } ) ,
131+ {
132+ enabled : ! ! organizationId ,
133+ select : data => data ?. invitations || NO_INVITATIONS ,
134+ } ,
135+ ) ;
136+
106137 const title = `${ t . member ( { plural : true , case : "capital" } ) } | ${ organization ?. title } | ${ t . organization ( { plural : true , case : "capital" } ) } ` ;
107138
108139 const [ tableQuery , setTableQuery ] = useState < DataTableQuery > ( INITIAL_QUERY ) ;
@@ -155,6 +186,16 @@ export function OrganizationMembersView() {
155186 const showZeroState =
156187 ! isLoading && ! isError && ! hasActiveQuery && data . length === 0 ;
157188
189+ // DataTable.Toolbar's own rule: hidden in the zero state.
190+ const showToolbar = data . length > 0 || Boolean ( tableQuery . filters ?. length ) ;
191+ /*
192+ * - an org always has at least one member, so the toolbar-less branch below
193+ * is a safeguard, not a state to expect
194+ * - the trigger is the count: a failed fetch leaves nothing to label, so it
195+ * stays hidden and only logs
196+ */
197+ const showInvitesBtn = invitations . length > 0 ;
198+
158199 const onTableQueryChange = ( newQuery : DataTableQuery ) => {
159200 setTableQuery ( newQuery ) ;
160201 } ;
@@ -165,6 +206,15 @@ export function OrganizationMembersView() {
165206 }
166207 } ;
167208
209+ useEffect ( ( ) => {
210+ if ( invitationsError ) {
211+ console . error (
212+ "Failed to fetch organization invitations:" ,
213+ invitationsError ,
214+ ) ;
215+ }
216+ } , [ invitationsError ] ) ;
217+
168218 useEffect ( ( ) => {
169219 setSearchVisibility ( true ) ;
170220 return ( ) => {
@@ -230,6 +280,15 @@ export function OrganizationMembersView() {
230280 onClose = { closeRemoveMemberDialog }
231281 />
232282 ) : null }
283+
284+ { isInvitesDialogOpen ? (
285+ < InvitedMembersDialog
286+ organizationId = { organizationId }
287+ invitations = { invitations }
288+ isLoading = { isInvitationsLoading }
289+ onClose = { ( ) => setIsInvitesDialogOpen ( false ) }
290+ />
291+ ) : null }
233292 < Flex justify = "center" className = { styles [ "container" ] } >
234293 < PageTitle title = { title } />
235294 < DataTable
@@ -242,7 +301,32 @@ export function OrganizationMembersView() {
242301 onLoadMore = { fetchMore }
243302 query = { tableQuery } >
244303 < Flex direction = "column" style = { { width : "100%" } } >
245- < DataTable . Toolbar />
304+ { /* DataTable.Toolbar takes no children, so the row is rebuilt from
305+ its parts to seat the invites trigger left of Display. */ }
306+ { ( showToolbar || showInvitesBtn ) && (
307+ < Flex
308+ justify = { showToolbar ? "between" : "end" }
309+ align = "start"
310+ gap = { 3 }
311+ className = { styles [ "toolbar" ] } >
312+ { showToolbar && < DataTable . Filters /> }
313+ < Flex align = "center" gap = { 3 } >
314+ { showInvitesBtn && (
315+ < Button
316+ variant = "text"
317+ color = "neutral"
318+ size = "small"
319+ onClick = { ( ) => setIsInvitesDialogOpen ( true ) }
320+ data-test-id = "admin-org-members-invites" >
321+ { /* Expired invites come back too, so "Pending" would
322+ overstate the count — Status labels each row. */ }
323+ { `${ invitations . length } Invite${ invitations . length === 1 ? "" : "s" } ` }
324+ </ Button >
325+ ) }
326+ { showToolbar && < DataTable . DisplayControls /> }
327+ </ Flex >
328+ </ Flex >
329+ ) }
246330 < DataTable . Content
247331 emptyState = { showZeroState ? < ZeroState /> : isError ? < ErrorState /> : < NoMembers /> }
248332 classNames = { {
0 commit comments