Skip to content

Commit 6dc16f9

Browse files
feat: add customisable button slots to service account views (#1707)
1 parent adbb040 commit 6dc16f9

4 files changed

Lines changed: 81 additions & 32 deletions

File tree

‎web/sdk/client/views/service-accounts/components/add-token-form.tsx‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type ServiceUserToken
1414
} from '@raystack/proton/frontier';
1515
import { useFrontier } from '~/client/contexts/FrontierContext';
16+
import type { Slot } from '~/shared/types';
1617
import styles from '../service-account-details-view.module.css';
1718

1819
const tokenSchema = yup
@@ -26,9 +27,18 @@ type FormData = yup.InferType<typeof tokenSchema>;
2627
export interface AddTokenFormProps {
2728
serviceUserId: string;
2829
onAddToken: (token: ServiceUserToken) => void;
30+
generateKeyButton?: Slot<{
31+
onClick: () => void;
32+
loading: boolean;
33+
disabled: boolean;
34+
}>;
2935
}
3036

31-
export function AddTokenForm({ serviceUserId, onAddToken }: AddTokenFormProps) {
37+
export function AddTokenForm({
38+
serviceUserId,
39+
onAddToken,
40+
generateKeyButton
41+
}: AddTokenFormProps) {
3242
const { activeOrganization } = useFrontier();
3343
const orgId = activeOrganization?.id || '';
3444

@@ -78,24 +88,28 @@ export function AddTokenForm({ serviceUserId, onAddToken }: AddTokenFormProps) {
7888
error={errors.title && String(errors.title?.message)}
7989
className={styles.addTokenInput}
8090
>
81-
<Input
82-
{...register('title')}
83-
size="large"
84-
placeholder="Label Name"
85-
/>
91+
<Input {...register('title')} size="large" placeholder="Label Name" />
8692
</Field>
87-
<Button
88-
variant="solid"
89-
color="accent"
90-
size="normal"
91-
type="submit"
92-
loading={isSubmitting}
93-
disabled={isSubmitting}
94-
loaderText="Generating..."
95-
data-test-id="frontier-sdk-service-account-generate-key-btn"
96-
>
97-
Generate new key
98-
</Button>
93+
{generateKeyButton ? (
94+
generateKeyButton({
95+
onClick: handleSubmit(onSubmit),
96+
loading: isSubmitting,
97+
disabled: isSubmitting
98+
})
99+
) : (
100+
<Button
101+
variant="solid"
102+
color="accent"
103+
size="normal"
104+
type="submit"
105+
loading={isSubmitting}
106+
disabled={isSubmitting}
107+
loaderText="Generating..."
108+
data-test-id="frontier-sdk-service-account-generate-key-btn"
109+
>
110+
Generate new key
111+
</Button>
112+
)}
99113
</Flex>
100114
</form>
101115
);

‎web/sdk/client/views/service-accounts/service-account-details-view.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { PERMISSIONS, shouldShowComponent } from '~/utils';
3131
import { useServiceUserTokens } from './hooks/useServiceUserTokens';
3232
import { ViewContainer } from '~/client/components/view-container';
3333
import { ViewHeader } from '~/client/components/view-header';
34-
import { AddTokenForm } from './components/add-token-form';
34+
import { AddTokenForm, type AddTokenFormProps } from './components/add-token-form';
3535
import {
3636
RevokeTokenDialog,
3737
type RevokeTokenPayload
@@ -53,13 +53,17 @@ export interface ServiceAccountDetailsViewProps {
5353
serviceAccountsLabel?: string;
5454
onNavigateToServiceAccounts?: () => void;
5555
onDeleteSuccess?: () => void;
56+
slots?: {
57+
generateKeyButton?: AddTokenFormProps['generateKeyButton'];
58+
};
5659
}
5760

5861
export function ServiceAccountDetailsView({
5962
serviceAccountId,
6063
serviceAccountsLabel = 'Service accounts',
6164
onNavigateToServiceAccounts,
62-
onDeleteSuccess
65+
onDeleteSuccess,
66+
slots
6367
}: ServiceAccountDetailsViewProps) {
6468
const { activeOrganization: organization } = useFrontier();
6569
const t = useTerminology();
@@ -166,6 +170,7 @@ export function ServiceAccountDetailsView({
166170
<AddTokenForm
167171
serviceUserId={serviceAccountId}
168172
onAddToken={addToken}
173+
generateKeyButton={slots?.generateKeyButton}
169174
/>
170175
</>
171176
)}

‎web/sdk/client/views/service-accounts/service-accounts-view.tsx‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from '@raystack/apsara';
1717
import deleteIcon from '~/client/assets/delete.svg';
1818
import keyIcon from '~/client/assets/key.svg';
19-
import exclamationTriangleIcon from '~/client/assets/exclamation-triangle.svg';
2019
import { useQuery } from '@connectrpc/connect-query';
2120
import { create } from '@bufbuild/protobuf';
2221
import {
@@ -31,6 +30,7 @@ import { PERMISSIONS, shouldShowComponent } from '~/utils';
3130
import { DEFAULT_DATE_FORMAT } from '~/client/utils/constants';
3231
import { ViewContainer } from '~/client/components/view-container';
3332
import { ViewHeader } from '~/client/components/view-header';
33+
import type { Slot } from '~/shared/types';
3434
import {
3535
getColumns,
3636
type ServiceAccountMenuPayload
@@ -50,10 +50,14 @@ const manageAccessDialogHandle = Dialog.createHandle<string>();
5050

5151
export interface ServiceAccountsViewProps {
5252
onServiceAccountClick?: (id: string) => void;
53+
slots?: {
54+
addAccountButton?: Slot<{ onClick: () => void; disabled: boolean }>;
55+
};
5356
}
5457

5558
export function ServiceAccountsView({
56-
onServiceAccountClick
59+
onServiceAccountClick,
60+
slots
5761
}: ServiceAccountsViewProps) {
5862
const {
5963
activeOrganization: organization,
@@ -126,6 +130,8 @@ export function ServiceAccountsView({
126130
[dateFormat, canUpdateWorkspace]
127131
);
128132

133+
const handleAddServiceAccount = () => addDialogHandle.open(null);
134+
129135
const handleCreated = (serviceUserId: string) => {
130136
onServiceAccountClick?.(serviceUserId);
131137
};
@@ -167,15 +173,22 @@ export function ServiceAccountsView({
167173
heading="No Service Account Found"
168174
subHeading={`Create a new account to use the APIs of ${t.appName()} platform`}
169175
primaryAction={
170-
<Button
171-
variant="solid"
172-
color="accent"
173-
size="small"
174-
onClick={() => addDialogHandle.open(null)}
175-
data-test-id="frontier-sdk-new-service-account-btn"
176-
>
177-
Add service account
178-
</Button>
176+
slots?.addAccountButton ? (
177+
slots.addAccountButton({
178+
onClick: handleAddServiceAccount,
179+
disabled: false
180+
})
181+
) : (
182+
<Button
183+
variant="solid"
184+
color="accent"
185+
size="small"
186+
onClick={handleAddServiceAccount}
187+
data-test-id="frontier-sdk-new-service-account-btn"
188+
>
189+
Add service account
190+
</Button>
191+
)
179192
}
180193
/>
181194
) : (
@@ -207,6 +220,11 @@ export function ServiceAccountsView({
207220
</Flex>
208221
{isLoading ? (
209222
<Skeleton height="34px" width="160px" />
223+
) : slots?.addAccountButton ? (
224+
slots.addAccountButton({
225+
onClick: handleAddServiceAccount,
226+
disabled: !canUpdateWorkspace
227+
})
210228
) : (
211229
<Tooltip>
212230
<Tooltip.Trigger
@@ -216,7 +234,7 @@ export function ServiceAccountsView({
216234
<Button
217235
variant="solid"
218236
color="accent"
219-
onClick={() => addDialogHandle.open(null)}
237+
onClick={handleAddServiceAccount}
220238
disabled={!canUpdateWorkspace}
221239
data-test-id="frontier-sdk-add-service-account-btn"
222240
>

‎web/sdk/shared/types.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import React from 'react';
22
import { BasePlan } from '../src/types';
33
import { ThemeProviderProps } from '@raystack/apsara';
44

5+
/**
6+
* A render-prop slot: receives a context object (shape defined per view/slot)
7+
* and returns the node to render. Lets a consumer override an internal element
8+
* of a view while keeping the customisation on their side.
9+
*
10+
* @example
11+
* interface ViewSlots {
12+
* addButton?: Slot<{ onClick: () => void; disabled: boolean }>;
13+
* }
14+
*/
15+
export type Slot<TContext> = (context: TContext) => React.ReactNode;
16+
517
export interface FrontierClientBillingOptions {
618
supportEmail?: string;
719
successUrl?: string;

0 commit comments

Comments
 (0)