-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathOrganizationListPage.tsx
225 lines (202 loc) · 7.43 KB
/
OrganizationListPage.tsx
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
import { useOrganizationList, useUser } from '@clerk/shared/react';
import { useContext, useState } from 'react';
import { useEnvironment, useOrganizationListContext } from '../../contexts';
import { SessionTasksContext } from '../../contexts/components/SessionTasks';
import { Box, Col, descriptors, Flex, localizationKeys, Spinner } from '../../customizables';
import { Action, Actions, Card, Header, useCardState, withCardStateProvider } from '../../elements';
import { useInView } from '../../hooks';
import { Add } from '../../icons';
import { CreateOrganizationForm } from '../CreateOrganization/CreateOrganizationForm';
import { PreviewListItems, PreviewListSpinner } from './shared';
import { InvitationPreview } from './UserInvitationList';
import { MembershipPreview, PersonalAccountPreview } from './UserMembershipList';
import { SuggestionPreview } from './UserSuggestionList';
import { organizationListParams } from './utils';
const useOrganizationListInView = () => {
const { userMemberships, userInvitations, userSuggestions } = useOrganizationList(organizationListParams);
const { ref } = useInView({
threshold: 0,
onChange: inView => {
if (!inView) {
return;
}
if (userMemberships.hasNextPage) {
userMemberships.fetchNext?.();
} else if (userInvitations.hasNextPage) {
userInvitations.fetchNext?.();
} else {
userSuggestions.fetchNext?.();
}
},
});
return {
userMemberships,
userInvitations,
userSuggestions,
ref,
};
};
const CreateOrganizationButton = ({
onCreateOrganizationClick,
}: {
onCreateOrganizationClick: React.MouseEventHandler;
}) => {
const { user } = useUser();
if (!user?.createOrganizationEnabled) {
return null;
}
return (
<Action
elementDescriptor={descriptors.organizationListCreateOrganizationActionButton}
icon={Add}
label={localizationKeys('organizationList.action__createOrganization')}
onClick={onCreateOrganizationClick}
sx={t => ({
borderTopWidth: t.borderWidths.$normal,
borderTopStyle: t.borderStyles.$solid,
borderTopColor: t.colors.$neutralAlpha100,
padding: `${t.space.$5} ${t.space.$5}`,
})}
iconSx={t => ({
width: t.sizes.$9,
height: t.sizes.$6,
})}
/>
);
};
export const OrganizationListPage = withCardStateProvider(() => {
const card = useCardState();
const { userMemberships, userSuggestions, userInvitations } = useOrganizationListInView();
const isLoading = userMemberships?.isLoading || userInvitations?.isLoading || userSuggestions?.isLoading;
const hasAnyData = !!(userMemberships?.count || userInvitations?.count || userSuggestions?.count);
const { hidePersonal } = useOrganizationListContext();
return (
<Card.Root>
<Card.Content sx={t => ({ padding: `${t.space.$8} ${t.space.$none} ${t.space.$none}` })}>
<Card.Alert sx={t => ({ margin: `${t.space.$none} ${t.space.$5}` })}>{card.error}</Card.Alert>
{isLoading && (
<Flex
direction={'row'}
align={'center'}
justify={'center'}
sx={t => ({
height: '100%',
minHeight: t.sizes.$60,
})}
>
<Spinner
size={'lg'}
colorScheme={'primary'}
elementDescriptor={descriptors.spinner}
/>
</Flex>
)}
{!isLoading && <OrganizationListFlows showListInitially={!(hidePersonal && !hasAnyData)} />}
</Card.Content>
<Card.Footer />
</Card.Root>
);
});
const OrganizationListFlows = ({ showListInitially }: { showListInitially: boolean }) => {
const { navigateAfterCreateOrganization, skipInvitationScreen, hideSlug } = useOrganizationListContext();
const [isCreateOrganizationFlow, setCreateOrganizationFlow] = useState(!showListInitially);
const sessionTasksContext = useContext(SessionTasksContext);
return (
<>
{!isCreateOrganizationFlow && (
<OrganizationListPageList onCreateOrganizationClick={() => setCreateOrganizationFlow(true)} />
)}
{isCreateOrganizationFlow && (
<Box
sx={t => ({
padding: `${t.space.$none} ${t.space.$5} ${t.space.$5}`,
})}
>
<CreateOrganizationForm
flow='organizationList'
onComplete={sessionTasksContext?.nextTask}
startPage={{ headerTitle: localizationKeys('organizationList.createOrganization') }}
skipInvitationScreen={skipInvitationScreen}
navigateAfterCreateOrganization={org =>
navigateAfterCreateOrganization(org).then(() => setCreateOrganizationFlow(false))
}
onCancel={
showListInitially && isCreateOrganizationFlow ? () => setCreateOrganizationFlow(false) : undefined
}
hideSlug={hideSlug}
/>
</Box>
)}
</>
);
};
const OrganizationListPageList = (props: { onCreateOrganizationClick: () => void }) => {
const environment = useEnvironment();
const { ref, userMemberships, userSuggestions, userInvitations } = useOrganizationListInView();
const { hidePersonal } = useOrganizationListContext();
const isLoading = userMemberships?.isLoading || userInvitations?.isLoading || userSuggestions?.isLoading;
const hasNextPage = userMemberships?.hasNextPage || userInvitations?.hasNextPage || userSuggestions?.hasNextPage;
const onCreateOrganizationClick = () => {
props.onCreateOrganizationClick();
};
// Solve weird bug with swr while running unit tests
const userInvitationsData = userInvitations.data?.filter(a => !!a);
const userSuggestionsData = userSuggestions.data?.filter(a => !!a);
return (
<>
<Header.Root
sx={t => ({
padding: `${t.space.$none} ${t.space.$8}`,
})}
>
<Header.Title
localizationKey={localizationKeys(
!hidePersonal ? 'organizationList.title' : 'organizationList.titleWithoutPersonal',
)}
/>
<Header.Subtitle
localizationKey={localizationKeys('organizationList.subtitle', {
applicationName: environment.displayConfig.applicationName,
})}
/>
</Header.Root>
<Col elementDescriptor={descriptors.main}>
<PreviewListItems>
<Actions role='menu'>
<PersonalAccountPreview />
{(userMemberships.count || 0) > 0 &&
userMemberships.data?.map(inv => {
return (
<MembershipPreview
key={inv.id}
{...inv}
/>
);
})}
{!userMemberships.hasNextPage &&
userInvitationsData?.map(inv => {
return (
<InvitationPreview
key={inv.id}
{...inv}
/>
);
})}
{!userMemberships.hasNextPage &&
!userInvitations.hasNextPage &&
userSuggestionsData?.map(inv => {
return (
<SuggestionPreview
key={inv.id}
{...inv}
/>
);
})}
{(hasNextPage || isLoading) && <PreviewListSpinner ref={ref} />}
<CreateOrganizationButton onCreateOrganizationClick={onCreateOrganizationClick} />
</Actions>
</PreviewListItems>
</Col>
</>
);
};