Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Show confirm to close modal if changes made

Joshen Lima30db5393924
diff --git a/studio/components/interfaces/Database/Hooks/EditHookPanel.tsx b/studio/components/interfaces/Database/Hooks/EditHookPanel.tsx
index ddcc6327f8c..583ab66294b 100644
--- a/studio/components/interfaces/Database/Hooks/EditHookPanel.tsx
+++ b/studio/components/interfaces/Database/Hooks/EditHookPanel.tsx
@@ -1,7 +1,7 @@
import Image from 'next/image'
import { useState, useRef, useEffect } from 'react'
import { PostgresTrigger } from '@supabase/postgres-meta'
-import { Button, SidePanel, Form, Input, Listbox, Checkbox, Radio, Badge } from 'ui'
+import { Button, SidePanel, Form, Input, Listbox, Checkbox, Radio, Badge, Modal } from 'ui'
import { useStore } from 'hooks'
import { tryParseJson, uuidv4 } from 'lib/helpers'
@@ -12,6 +12,7 @@ import { FormSection, FormSectionLabel, FormSectionContent } from 'components/ui
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { useDatabaseTriggerCreateMutation } from 'data/database-triggers/database-trigger-create-mutation'
import { useDatabaseTriggerUpdateMutation } from 'data/database-triggers/database-trigger-update-transaction-mutation'
+import ConfirmationModal from 'components/ui/ConfirmationModal'
export interface EditHookPanelProps {
visible: boolean
@@ -25,7 +26,9 @@ const EditHookPanel = ({ visible, selectedHook, onClose }: EditHookPanelProps) =
// [Joshen] Need to change to use RQ once Alaister's PR goes in
const { meta, ui } = useStore()
const submitRef: any = useRef()
+ const [isEdited, setIsEdited] = useState(false)
const [isSubmitting, setIsSubmitting] = useState(false)
+ const [isClosingPanel, setIsClosingPanel] = useState(false)
// [Joshen] There seems to be some bug between Checkbox.Group within the Form component
// hence why this external state as a temporary workaround
@@ -40,9 +43,21 @@ const EditHookPanel = ({ visible, selectedHook, onClose }: EditHookPanelProps) =
const { mutateAsync: createDatabaseTrigger } = useDatabaseTriggerCreateMutation()
const { mutateAsync: updateDatabaseTrigger } = useDatabaseTriggerUpdateMutation()
+ const tables = meta.tables.list()
+ const initialValues = {
+ name: selectedHook?.name ?? '',
+ table_id: selectedHook?.table_id ?? '',
+ function_name: selectedHook?.function_name ?? 'http_request',
+ http_url: selectedHook?.function_args?.[0] ?? '',
+ http_method: selectedHook?.function_args?.[1] ?? 'GET',
+ }
+
useEffect(() => {
if (visible) {
+ setIsEdited(false)
setIsSubmitting(false)
+ setIsClosingPanel(false)
+
// Reset form fields outside of the Form context
if (selectedHook !== undefined) {
setEvents(selectedHook.events)
@@ -68,15 +83,9 @@ const EditHookPanel = ({ visible, selectedHook, onClose }: EditHookPanelProps) =
}
}, [visible, selectedHook])
- const tables = meta.tables.list()
-
- const initialValues = {
- name: selectedHook?.name ?? '',
- table_id: selectedHook?.table_id ?? '',
- function_name: selectedHook?.function_name ?? 'http_request',
-
- http_url: selectedHook?.function_args?.[0] ?? '',
- http_method: selectedHook?.function_args?.[1] ?? 'GET',
+ const onClosePanel = () => {
+ if (isEdited) setIsClosingPanel(true)
+ else onClose()
}
const onUpdateSelectedEvents = (event: string) => {
@@ -111,6 +120,7 @@ const EditHookPanel = ({ visible, selectedHook, onClose }: EditHookPanelProps) =
// For Supabase Edge Functions
}
+ if (JSON.stringify(values) !== JSON.stringify(initialValues)) setIsEdited(true)
return errors
}
@@ -215,206 +225,229 @@ const EditHookPanel = ({ visible, selectedHook, onClose }: EditHookPanelProps) =
}
return (
- <SidePanel
- size="xlarge"
- visible={visible}
- header={
- selectedHook === undefined ? (
- 'Create a new database webhook'
- ) : (
- <>
- Update webhook <code className="text-sm">{selectedHook.name}</code>
- </>
- )
- }
- className="hooks-sidepanel mr-0 transform transition-all duration-300 ease-in-out"
- onConfirm={() => {}}
- onCancel={() => onClose()}
- customFooter={
- <div className="flex w-full justify-end space-x-3 border-t border-scale-500 px-3 py-4">
- <Button
- size="tiny"
- type="default"
- htmlType="button"
- onClick={onClose}
- disabled={isSubmitting}
- >
- Cancel
- </Button>
- <Button
- size="tiny"
- type="primary"
- htmlType="button"
- disabled={isSubmitting}
- loading={isSubmitting}
- onClick={() => submitRef?.current?.click()}
- >
- {selectedHook === undefined ? 'Create webhook' : 'Update webhook'}
- </Button>
- </div>
- }
- >
- <Form validateOnBlur initialValues={initialValues} onSubmit={onSubmit} validate={validate}>
- {() => {
- return (
- <div>
- <FormSection
- header={<FormSectionLabel className="lg:!col-span-4">General</FormSectionLabel>}
- >
- <FormSectionContent loading={false} className="lg:!col-span-8">
- <Input
- id="name"
- name="name"
- label="Name"
- descriptionText="Do not use spaces/whitespaces"
- />
- </FormSectionContent>
- </FormSection>
- <SidePanel.Separator />
- <FormSection
- header={
- <FormSectionLabel
- className="lg:!col-span-4"
- description={
- <p className="text-sm text-scale-1000">
- Select which table and events will trigger your function hook
- </p>
- }
- >
- Conditions to fire hook
- </FormSectionLabel>
- }
- >
- <FormSectionContent loading={false} className="lg:!col-span-8">
- <Listbox
- size="medium"
- id="table_id"
- name="table_id"
- label="Table"
- descriptionText="This is the table the trigger will watch for changes. You can only select 1 table for a trigger."
- >
- <Listbox.Option
- key={'table-no-selection'}
- id={'table-no-selection'}
- label={'---'}
- value={'no-selection'}
+ <>
+ <SidePanel
+ size="xlarge"
+ visible={visible}
+ header={
+ selectedHook === undefined ? (
+ 'Create a new database webhook'
+ ) : (
+ <>
+ Update webhook <code className="text-sm">{selectedHook.name}</code>
+ </>
+ )
+ }
+ className="hooks-sidepanel mr-0 transform transition-all duration-300 ease-in-out"
+ onConfirm={() => {}}
+ onCancel={() => onClosePanel()}
+ customFooter={
+ <div className="flex w-full justify-end space-x-3 border-t border-scale-500 px-3 py-4">
+ <Button
+ size="tiny"
+ type="default"
+ htmlType="button"
+ onClick={onClosePanel}
+ disabled={isSubmitting}
+ >
+ Cancel
+ </Button>
+ <Button
+ size="tiny"
+ type="primary"
+ htmlType="button"
+ disabled={isSubmitting}
+ loading={isSubmitting}
+ onClick={() => submitRef?.current?.click()}
+ >
+ {selectedHook === undefined ? 'Create webhook' : 'Update webhook'}
+ </Button>
+ </div>
+ }
+ >
+ <Form validateOnBlur initialValues={initialValues} onSubmit={onSubmit} validate={validate}>
+ {() => {
+ return (
+ <div>
+ <FormSection
+ header={<FormSectionLabel className="lg:!col-span-4">General</FormSectionLabel>}
+ >
+ <FormSectionContent loading={false} className="lg:!col-span-8">
+ <Input
+ id="name"
+ name="name"
+ label="Name"
+ descriptionText="Do not use spaces/whitespaces"
+ />
+ </FormSectionContent>
+ </FormSection>
+ <SidePanel.Separator />
+ <FormSection
+ header={
+ <FormSectionLabel
+ className="lg:!col-span-4"
+ description={
+ <p className="text-sm text-scale-1000">
+ Select which table and events will trigger your function hook
+ </p>
+ }
+ >
+ Conditions to fire hook
+ </FormSectionLabel>
+ }
+ >
+ <FormSectionContent loading={false} className="lg:!col-span-8">
+ <Listbox
+ size="medium"
+ id="table_id"
+ name="table_id"
+ label="Table"
+ descriptionText="This is the table the trigger will watch for changes. You can only select 1 table for a trigger."
>
- ---
- </Listbox.Option>
- {tables.map((table) => (
<Listbox.Option
- key={table.id}
- id={table.id.toString()}
- value={table.id}
- label={table.name}
+ key={'table-no-selection'}
+ id={'table-no-selection'}
+ label={'---'}
+ value={'no-selection'}
>
- <div className="flex items-center space-x-2">
- <p>{table.name}</p>
- <p className="text-scale-1100">{table.schema}</p>
- </div>
+ ---
</Listbox.Option>
- ))}
- </Listbox>
- <Checkbox.Group
- id="events"
- name="events"
- label="Events"
- error={eventsError}
- descriptionText="These are the events that are watched by the webhook, only the events selected above will fire the webhook on the table you've selected."
- >
- {HOOK_EVENTS.map((event) => (
- <Checkbox
- key={event.value}
- value={event.value}
- label={event.label}
- description={event.description}
- checked={events.includes(event.value)}
- onChange={() => onUpdateSelectedEvents(event.value)}
- />
- ))}
- </Checkbox.Group>
- </FormSectionContent>
- </FormSection>
- <SidePanel.Separator />
- <FormSection
- header={
- <FormSectionLabel className="lg:!col-span-4">Hook configuration</FormSectionLabel>
- }
- >
- <FormSectionContent loading={false} className="lg:!col-span-8">
- <Radio.Group
- id="function_name"
- name="function_name"
- label="Type of hook"
- type="cards"
- >
- {AVAILABLE_WEBHOOK_TYPES.map((webhook) => (
- <Radio
- disabled={webhook.disabled}
- key={webhook.value}
- id={webhook.value}
- value={webhook.value}
- label=""
- beforeLabel={
- <div className="flex items-center space-x-5">
- <Image src={webhook.icon} layout="fixed" width="32" height="32" />
- <div className="flex-col space-y-0">
- <div className="flex space-x-2">
- <p className="text-scale-1200">{webhook.label}</p>
- {webhook.disabled ? (
- <Badge color="amber">Coming soon</Badge>
- ) : (
- <Badge color="green">Alpha</Badge>
- )}
+ {tables.map((table) => (
+ <Listbox.Option
+ key={table.id}
+ id={table.id.toString()}
+ value={table.id}
+ label={table.name}
+ >
+ <div className="flex items-center space-x-2">
+ <p>{table.name}</p>
+ <p className="text-scale-1100">{table.schema}</p>
+ </div>
+ </Listbox.Option>
+ ))}
+ </Listbox>
+ <Checkbox.Group
+ id="events"
+ name="events"
+ label="Events"
+ error={eventsError}
+ descriptionText="These are the events that are watched by the webhook, only the events selected above will fire the webhook on the table you've selected."
+ >
+ {HOOK_EVENTS.map((event) => (
+ <Checkbox
+ key={event.value}
+ value={event.value}
+ label={event.label}
+ description={event.description}
+ checked={events.includes(event.value)}
+ onChange={() => onUpdateSelectedEvents(event.value)}
+ />
+ ))}
+ </Checkbox.Group>
+ </FormSectionContent>
+ </FormSection>
+ <SidePanel.Separator />
+ <FormSection
+ header={
+ <FormSectionLabel className="lg:!col-span-4">
+ Hook configuration
+ </FormSectionLabel>
+ }
+ >
+ <FormSectionContent loading={false} className="lg:!col-span-8">
+ <Radio.Group
+ id="function_name"
+ name="function_name"
+ label="Type of hook"
+ type="cards"
+ >
+ {AVAILABLE_WEBHOOK_TYPES.map((webhook) => (
+ <Radio
+ disabled={webhook.disabled}
+ key={webhook.value}
+ id={webhook.value}
+ value={webhook.value}
+ label=""
+ beforeLabel={
+ <div className="flex items-center space-x-5">
+ <Image src={webhook.icon} layout="fixed" width="32" height="32" />
+ <div className="flex-col space-y-0">
+ <div className="flex space-x-2">
+ <p className="text-scale-1200">{webhook.label}</p>
+ {webhook.disabled ? (
+ <Badge color="amber">Coming soon</Badge>
+ ) : (
+ <Badge color="green">Alpha</Badge>
+ )}
+ </div>
+ <p className="text-scale-1000">{webhook.description}</p>
</div>
- <p className="text-scale-1000">{webhook.description}</p>
</div>
- </div>
- }
- />
- ))}
- </Radio.Group>
- </FormSectionContent>
- </FormSection>
- <SidePanel.Separator />
+ }
+ />
+ ))}
+ </Radio.Group>
+ </FormSectionContent>
+ </FormSection>
+ <SidePanel.Separator />
- <HTTPRequestFields
- httpHeaders={httpHeaders}
- httpParameters={httpParameters}
- onAddHeader={() =>
- setHttpHeaders(httpHeaders.concat({ id: uuidv4(), name: '', value: '' }))
- }
- onUpdateHeader={(idx, property, value) =>
- setHttpHeaders(
- httpHeaders.map((header, i) => {
- if (idx === i) return { ...header, [property]: value }
- else return header
- })
- )
- }
- onRemoveHeader={(idx) => setHttpHeaders(httpHeaders.filter((_, i) => idx !== i))}
- onAddParameter={() =>
- setHttpParameters(httpParameters.concat({ id: uuidv4(), name: '', value: '' }))
- }
- onUpdateParameter={(idx, property, value) =>
- setHttpParameters(
- httpParameters.map((param, i) => {
- if (idx === i) return { ...param, [property]: value }
- else return param
- })
- )
- }
- onRemoveParameter={(idx) =>
- setHttpParameters(httpParameters.filter((_, i) => idx !== i))
- }
- />
+ <HTTPRequestFields
+ httpHeaders={httpHeaders}
+ httpParameters={httpParameters}
+ onAddHeader={() =>
+ setHttpHeaders(httpHeaders.concat({ id: uuidv4(), name: '', value: '' }))
+ }
+ onUpdateHeader={(idx, property, value) =>
+ setHttpHeaders(
+ httpHeaders.map((header, i) => {
+ if (idx === i) return { ...header, [property]: value }
+ else return header
+ })
+ )
+ }
+ onRemoveHeader={(idx) => setHttpHeaders(httpHeaders.filter((_, i) => idx !== i))}
+ onAddParameter={() =>
+ setHttpParameters(httpParameters.concat({ id: uuidv4(), name: '', value: '' }))
+ }
+ onUpdateParameter={(idx, property, value) =>
+ setHttpParameters(
+ httpParameters.map((param, i) => {
+ if (idx === i) return { ...param, [property]: value }
+ else return param
+ })
+ )
+ }
+ onRemoveParameter={(idx) =>
+ setHttpParameters(httpParameters.filter((_, i) => idx !== i))
+ }
+ />
- <button ref={submitRef} type="submit" className="hidden" />
- </div>
- )
+ <button ref={submitRef} type="submit" className="hidden" />
+ </div>
+ )
+ }}
+ </Form>
+ </SidePanel>
+ <ConfirmationModal
+ visible={isClosingPanel}
+ header="Confirm to close"
+ buttonLabel="Confirm"
+ onSelectCancel={() => setIsClosingPanel(false)}
+ onSelectConfirm={() => {
+ setIsClosingPanel(false)
+ setIsEdited(false)
+ onClose()
}}
- </Form>
- </SidePanel>
+ children={
+ <Modal.Content>
+ <p className="py-4 text-sm text-scale-1100">
+ There are unsaved changes. Are you sure you want to close the panel? Your changes will
+ be lost.
+ </p>
+ </Modal.Content>
+ }
+ />
+ </>
)
}