Skip to content

Commit 30a5425

Browse files
committed
refactor: update webhook management to use 'create' and 'update' modes
- Changed the webhook input mode from 'edit' to 'update' for consistency across the application. - Refactored related components and schemas to align with the new mode terminology. - Introduced a new UpsertWebhookDialog component to streamline the webhook creation and update process. - Updated the webhooks page to utilize the new dialog, enhancing the user experience in managing webhooks. These changes aim to improve clarity and maintainability in the webhook management interface.
1 parent 52bca58 commit 30a5425

7 files changed

Lines changed: 38 additions & 38 deletions

File tree

src/core/modules/webhooks/repository.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type WebhooksRepositoryDeps = {
1515
export type WebhooksScope = TeamRequestScope
1616

1717
export interface UpsertWebhookInput {
18-
mode: 'create' | 'edit'
18+
mode: 'create' | 'update'
1919
webhookId?: string
2020
name: string
2121
url: string
@@ -69,7 +69,7 @@ export function createWebhooksRepository(
6969
},
7070
async upsertWebhook(input) {
7171
const response =
72-
input.mode === 'edit'
72+
input.mode === 'update'
7373
? await deps.infraClient.PATCH('/events/webhooks/{webhookID}', {
7474
headers: {
7575
...deps.authHeaders(scope.accessToken, scope.teamId),

src/core/server/api/routers/webhooks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const webhooksRouter = createTRPCRouter({
4646
input
4747

4848
const result = await ctx.webhooksRepository.upsertWebhook({
49-
mode: mode === 'add' ? 'create' : 'edit',
49+
mode,
5050
webhookId: webhookId ?? undefined,
5151
name,
5252
url,
@@ -59,7 +59,7 @@ export const webhooksRouter = createTRPCRouter({
5959
l.error(
6060
{
6161
key:
62-
mode === 'edit'
62+
mode === 'update'
6363
? 'update_webhook_trpc:error'
6464
: 'create_webhook_trpc:error',
6565
status: result.error.status,
@@ -68,7 +68,7 @@ export const webhooksRouter = createTRPCRouter({
6868
user_id: ctx.session.user.id,
6969
context: { mode, name, url, events },
7070
},
71-
`Failed to ${mode === 'edit' ? 'update' : 'create'} webhook: ${result.error.status}: ${result.error.message}`
71+
`Failed to ${mode} webhook: ${result.error.status}: ${result.error.message}`
7272
)
7373

7474
throwTRPCErrorFromRepoError(result.error)

src/core/server/functions/webhooks/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const WebhookSecretSchema = z
88

99
export const UpsertWebhookInputSchema = z
1010
.object({
11-
mode: z.enum(['add', 'edit']),
11+
mode: z.enum(['create', 'update']),
1212
webhookId: z.uuid().optional(),
1313
name: z.string().min(1, 'Name is required').trim(),
1414
url: WebhookUrlSchema,
@@ -18,7 +18,7 @@ export const UpsertWebhookInputSchema = z
1818
})
1919
.refine(
2020
(data) => {
21-
if (data.mode === 'add') {
21+
if (data.mode === 'create') {
2222
return !!data.signatureSecret
2323
}
2424
return true

src/features/dashboard/settings/webhooks/table-row.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import {
2121
import { TableCell, TableRow } from '@/ui/primitives/table'
2222
import { useDashboard } from '../../context'
2323
import { TeamAvatar } from '../../sidebar/team-avatar'
24-
import WebhookAddEditDialog from './add-edit-dialog'
2524
import { WEBHOOK_EVENT_LABELS, WEBHOOK_EVENTS } from './constants'
2625
import WebhookDeleteDialog from './delete-dialog'
2726
import WebhookEditSecretDialog from './edit-secret-dialog'
2827
import type { Webhook } from './types'
28+
import { UpsertWebhookDialog } from './upsert-webhook-dialog'
2929

3030
type WebhookRowProps = {
3131
webhook: Webhook
@@ -60,11 +60,11 @@ const WebhookRowActions = ({ webhook }: WebhookRowActionsProps) => {
6060
</DropdownMenuTrigger>
6161
<DropdownMenuContent>
6262
<DropdownMenuGroup>
63-
<WebhookAddEditDialog mode="edit" webhook={webhook}>
63+
<UpsertWebhookDialog mode="update" webhook={webhook}>
6464
<DropdownMenuItem inset onSelect={(e) => e.preventDefault()}>
6565
<EditIcon className={actionIconClassName} /> Edit
6666
</DropdownMenuItem>
67-
</WebhookAddEditDialog>
67+
</UpsertWebhookDialog>
6868
<WebhookEditSecretDialog webhook={webhook}>
6969
<DropdownMenuItem inset onSelect={(e) => e.preventDefault()}>
7070
<PrivateIcon className={actionIconClassName} /> Rotate Secret

src/features/dashboard/settings/webhooks/add-edit-dialog-steps.tsx renamed to src/features/dashboard/settings/webhooks/upsert-webhook-dialog-steps.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
type WebhookEvent,
2727
} from './constants'
2828

29-
type WebhookAddEditDialogStepsProps = {
29+
type UpsertWebhookDialogStepsProps = {
3030
currentStep: number
3131
form: UseFormReturn<UpsertWebhookInput>
3232
isLoading: boolean
@@ -35,7 +35,7 @@ type WebhookAddEditDialogStepsProps = {
3535
allEventsSelected: boolean
3636
handleAllToggle: () => void
3737
handleEventToggle: (event: string) => void
38-
mode: 'add' | 'edit'
38+
mode: 'create' | 'update'
3939
hasCopied: boolean
4040
onCopied: () => void
4141
}
@@ -75,7 +75,7 @@ const WebhookExamplePayload = ({ eventType }: { eventType: WebhookEvent }) => (
7575
</div>
7676
)
7777

78-
export function WebhookAddEditDialogSteps({
78+
export function UpsertWebhookDialogSteps({
7979
currentStep,
8080
form,
8181
isLoading,
@@ -87,7 +87,7 @@ export function WebhookAddEditDialogSteps({
8787
mode,
8888
hasCopied,
8989
onCopied,
90-
}: WebhookAddEditDialogStepsProps) {
90+
}: UpsertWebhookDialogStepsProps) {
9191
const [secretType, setSecretType] = useState<'pre-generated' | 'custom'>(
9292
'pre-generated'
9393
)
@@ -111,10 +111,10 @@ export function WebhookAddEditDialogSteps({
111111
return () => window.clearTimeout(id)
112112
}, [secretType])
113113

114-
// sync secret with form state and validation - only in 'add' mode
115-
// in 'edit' mode, we should never touch the signature secret
114+
// sync secret with form state and validation - only in create mode
115+
// in update mode, we should never touch the signature secret
116116
useEffect(() => {
117-
if (mode !== 'add') return
117+
if (mode !== 'create') return
118118

119119
if (secretType === 'pre-generated') {
120120
// set pre-generated secret and trigger validation to clear any errors

src/features/dashboard/settings/webhooks/add-edit-dialog.tsx renamed to src/features/dashboard/settings/webhooks/upsert-webhook-dialog.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,28 @@ import { Form } from '@/ui/primitives/form'
2727
import { AddIcon, CheckIcon } from '@/ui/primitives/icons'
2828
import { Loader } from '@/ui/primitives/loader'
2929
import { useDashboard } from '../../context'
30-
import { WebhookAddEditDialogSteps } from './add-edit-dialog-steps'
3130
import { WEBHOOK_EVENTS, type WebhookEvent } from './constants'
3231
import { FinishWebhookSetupDialog } from './finish-webhook-setup-dialog'
3332
import type { Webhook } from './types'
33+
import { UpsertWebhookDialogSteps } from './upsert-webhook-dialog-steps'
3434

35-
type WebhookAddEditDialogProps =
35+
type UpsertWebhookDialogProps =
3636
| {
3737
children: React.ReactNode
38-
mode: 'add'
38+
mode: 'create'
3939
webhook?: undefined
4040
}
4141
| {
4242
children: React.ReactNode
43-
mode: 'edit'
43+
mode: 'update'
4444
webhook: Webhook
4545
}
4646

47-
export default function WebhookAddEditDialog({
47+
export function UpsertWebhookDialog({
4848
children: trigger,
4949
mode,
5050
webhook,
51-
}: WebhookAddEditDialogProps) {
51+
}: UpsertWebhookDialogProps) {
5252
'use no memo'
5353

5454
const { team } = useDashboard()
@@ -61,20 +61,20 @@ export default function WebhookAddEditDialog({
6161
const [hasCopied, setHasCopied] = useState(false)
6262
const [finishSetupDialogOpen, setFinishSetupDialogOpen] = useState(false)
6363

64-
const isEditMode = mode === 'edit'
65-
const totalSteps = isEditMode ? 1 : 2
64+
const isUpdateMode = mode === 'update'
65+
const totalSteps = isUpdateMode ? 1 : 2
6666

6767
const listQueryKey = trpc.webhooks.list.queryOptions({
6868
teamSlug: team.slug,
6969
}).queryKey
7070

7171
const defaultValues: UpsertWebhookInput = {
72-
webhookId: isEditMode ? webhook?.id : undefined,
72+
webhookId: isUpdateMode ? webhook?.id : undefined,
7373
mode,
7474
name: webhook?.name || '',
7575
url: webhook?.url || '',
7676
events: webhook?.events || [],
77-
...(isEditMode ? {} : { signatureSecret: '' }),
77+
...(isUpdateMode ? {} : { signatureSecret: '' }),
7878
}
7979

8080
const form = useForm<UpsertWebhookInput>({
@@ -90,14 +90,14 @@ export default function WebhookAddEditDialog({
9090
onSuccess: () => {
9191
toast(
9292
defaultSuccessToast(
93-
isEditMode
93+
isUpdateMode
9494
? 'Webhook updated successfully'
9595
: 'Webhook created successfully'
9696
)
9797
)
9898
void queryClient.invalidateQueries({ queryKey: listQueryKey })
9999

100-
if (!isEditMode) {
100+
if (!isUpdateMode) {
101101
handleDialogChange(false)
102102
setFinishSetupDialogOpen(true)
103103
return
@@ -109,7 +109,7 @@ export default function WebhookAddEditDialog({
109109
toast(
110110
defaultErrorToast(
111111
err.message ||
112-
(isEditMode
112+
(isUpdateMode
113113
? 'Failed to update webhook'
114114
: 'Failed to create webhook')
115115
)
@@ -211,9 +211,9 @@ export default function WebhookAddEditDialog({
211211
<DialogContent className="flex flex-col gap-4 px-6 pt-5 pb-6 h-[685px] max-h-[calc(100svh-2rem)] overflow-hidden">
212212
<DialogHeader className="gap-0.5">
213213
<DialogTitle>
214-
{isEditMode ? 'Edit Webhook' : 'Add Webhook'}
214+
{isUpdateMode ? 'Edit Webhook' : 'Add Webhook'}
215215
</DialogTitle>
216-
{!isEditMode && (
216+
{!isUpdateMode && (
217217
<div className="flex items-center gap-2">
218218
<span className="text-fg-tertiary prose-label uppercase">
219219
Step {currentStep} / {totalSteps}
@@ -230,7 +230,7 @@ export default function WebhookAddEditDialog({
230230
<input type="hidden" {...form.register('mode')} />
231231

232232
<div className="flex flex-1 flex-col gap-4 min-w-0 min-h-0 overflow-y-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
233-
<WebhookAddEditDialogSteps
233+
<UpsertWebhookDialogSteps
234234
currentStep={currentStep}
235235
form={form}
236236
isLoading={isLoading}
@@ -250,10 +250,10 @@ export default function WebhookAddEditDialog({
250250
<div className="flex items-center justify-center py-2 gap-2 w-full">
251251
<Loader variant="slash" size="sm" />
252252
<span className="prose-body text-fg-secondary">
253-
{isEditMode ? 'Saving Changes...' : 'Adding Webhook...'}
253+
{isUpdateMode ? 'Saving Changes...' : 'Adding Webhook...'}
254254
</span>
255255
</div>
256-
) : isEditMode ? (
256+
) : isUpdateMode ? (
257257
<Button
258258
type="submit"
259259
className="w-full"

src/features/dashboard/settings/webhooks/webhooks-page-content.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { useTRPC } from '@/trpc/client'
88
import { Button } from '@/ui/primitives/button'
99
import { AddIcon, SearchIcon } from '@/ui/primitives/icons'
1010
import { Input } from '@/ui/primitives/input'
11-
import WebhookAddEditDialog from './add-edit-dialog'
1211
import WebhooksTable from './table'
12+
import { UpsertWebhookDialog } from './upsert-webhook-dialog'
1313

1414
interface WebhooksPageContentProps {
1515
teamSlug: string
@@ -104,7 +104,7 @@ export const WebhooksPageContent = ({
104104
value={query}
105105
/>
106106

107-
<WebhookAddEditDialog mode="add">
107+
<UpsertWebhookDialog mode="create">
108108
<Button
109109
className="w-full font-sans normal-case prose-body-highlight md:w-auto md:self-start"
110110
type="button"
@@ -113,7 +113,7 @@ export const WebhooksPageContent = ({
113113
<AddIcon aria-hidden className="size-4" />
114114
Add a webhook
115115
</Button>
116-
</WebhookAddEditDialog>
116+
</UpsertWebhookDialog>
117117
</div>
118118

119119
<div className="text-fg-tertiary flex flex-col gap-1 text-sm lg:flex-row lg:items-start lg:justify-between">

0 commit comments

Comments
 (0)