Home / Function/ PolicyEditorModal() — supabase Function Reference

PolicyEditorModal() — supabase Function Reference

Architecture documentation for the PolicyEditorModal() function in index.tsx from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  331765b7_d5b8_b1a0_dc2a_d6b857a68adb["PolicyEditorModal()"]
  985f2d7f_95ed_bd41_d27b_4f31564dcf82["createSQLPolicy()"]
  331765b7_d5b8_b1a0_dc2a_d6b857a68adb -->|calls| 985f2d7f_95ed_bd41_d27b_4f31564dcf82
  c86aca53_24a5_5463_ea01_9f31aea46b8d["createPayloadForCreatePolicy()"]
  331765b7_d5b8_b1a0_dc2a_d6b857a68adb -->|calls| c86aca53_24a5_5463_ea01_9f31aea46b8d
  b39fdbdc_b73a_98ce_811b_e9c4bdd8c45f["createPayloadForUpdatePolicy()"]
  331765b7_d5b8_b1a0_dc2a_d6b857a68adb -->|calls| b39fdbdc_b73a_98ce_811b_e9c4bdd8c45f
  84c99679_0e25_0222_f391_4a90bae0289e["getGeneralPolicyTemplates()"]
  331765b7_d5b8_b1a0_dc2a_d6b857a68adb -->|calls| 84c99679_0e25_0222_f391_4a90bae0289e
  style 331765b7_d5b8_b1a0_dc2a_d6b857a68adb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/interfaces/Auth/Policies/PolicyEditorModal/index.tsx lines 41–234

const PolicyEditorModal = ({
  visible = false,
  schema = '',
  table = '',
  selectedPolicyToEdit = {},
  showAssistantPreview = false,
  onSelectCancel = noop,
  onCreatePolicy,
  onUpdatePolicy,
  onSaveSuccess = noop,
}: PolicyEditorModalProps) => {
  const { toggleFeaturePreviewModal } = useFeaturePreviewModal()

  const newPolicyTemplate: PolicyFormField = {
    schema,
    table,
    name: '',
    definition: '',
    check: '',
    command: null,
    roles: [],
  }

  const isNewPolicy = isEmpty(selectedPolicyToEdit)
  const initializedPolicyFormFields = isNewPolicy ? newPolicyTemplate : selectedPolicyToEdit

  // Mainly to decide which view to show when back from templates
  const [previousView, setPreviousView] = useState('')
  const [view, setView] = useState(POLICY_MODAL_VIEWS.EDITOR)

  const [policyFormFields, setPolicyFormFields] = useState<PolicyFormField>(
    initializedPolicyFormFields
  )
  const [policyStatementForReview, setPolicyStatementForReview] = useState<any>('')
  const [isDirty, setIsDirty] = useState(false)

  const { confirmOnClose, modalProps: closeConfirmationModalProps } = useConfirmOnClose({
    checkIsDirty: () => isDirty,
    onClose: () => {
      onSelectCancel()
      setIsDirty(false)
    },
  })

  const onViewIntro = useCallback(() => setView(POLICY_MODAL_VIEWS.SELECTION), [])
  const onViewEditor = useCallback(() => setView(POLICY_MODAL_VIEWS.EDITOR), [])
  const onViewTemplates = () => {
    setPreviousView(view)
    setView(POLICY_MODAL_VIEWS.TEMPLATES)
  }
  const onReviewPolicy = () => setView(POLICY_MODAL_VIEWS.REVIEW)
  const onSelectBackFromTemplates = () => setView(previousView)

  const isNewPolicyRef = useLatest(isNewPolicy)
  const initializedPolicyFormFieldsRef = useLatest(initializedPolicyFormFields)
  useEffect(() => {
    if (visible) {
      if (isNewPolicyRef.current) {
        onViewIntro()
      } else {
        onViewEditor()
      }
      setPolicyFormFields(initializedPolicyFormFieldsRef.current)
    }
  }, [
    onViewIntro,
    onViewEditor,
    isNewPolicyRef,
    initializedPolicyFormFieldsRef,
    // end of stable references
    visible,
  ])

  /* Methods that are for the UI */

  const onToggleFeaturePreviewModal = () => {
    toggleFeaturePreviewModal()
    onSelectCancel()
  }

  const onUseTemplate = (template: PolicyTemplate) => {
    setPolicyFormFields({
      ...policyFormFields,
      name: template.name,
      definition: template.definition,
      check: template.check,
      command: template.command,
      roles: template.roles,
    })
    onViewEditor()
  }

  const onUpdatePolicyFormFields = (field: Partial<PolicyFormField>) => {
    setIsDirty(true)
    if (field.name && field.name.length > 63) return
    setPolicyFormFields({ ...policyFormFields, ...field })
  }

  const validatePolicyFormFields = () => {
    const { name, definition, check, command } = policyFormFields

    if (name.length === 0) {
      return toast.error('Please provide a name for your policy')
    }
    if (!command) {
      return toast.error('Please select an operation for your policy')
    }
    if (['SELECT', 'DELETE'].includes(command) && !definition) {
      return toast.error('Please provide a USING expression for your policy')
    }
    if (command === 'INSERT' && !check) {
      return toast.error('Please provide a WITH CHECK expression for your policy')
    }
    if (command === 'UPDATE' && !definition && !check) {
      return toast.error(
        'Please provide either a USING, or WITH CHECK expression, or both for your policy'
      )
    }
    const policySQLStatement = createSQLPolicy(policyFormFields, selectedPolicyToEdit)
    setPolicyStatementForReview(policySQLStatement)
    onReviewPolicy()
  }

  const onReviewSave = () => {
    const payload = isNewPolicy
      ? createPayloadForCreatePolicy(policyFormFields)
      : createPayloadForUpdatePolicy(policyFormFields, selectedPolicyToEdit)
    onSavePolicy(payload)
    setIsDirty(false)
  }

  const onSavePolicy = async (
    payload: PostgresPolicyCreatePayload | PostgresPolicyUpdatePayload
  ) => {
    // @ts-ignore
    const hasError = isNewPolicy ? await onCreatePolicy(payload) : await onUpdatePolicy(payload)
    hasError ? onViewEditor() : onSaveSuccess()
  }

  return (
    <Modal
      hideFooter
      size={view === POLICY_MODAL_VIEWS.SELECTION ? 'medium' : 'xxlarge'}
      visible={visible}
      contentStyle={{ padding: 0 }}
      header={[
        <PolicyEditorModalTitle
          key="0"
          view={view}
          isNewPolicy={isNewPolicy}
          schema={schema}
          table={table}
          showAssistantPreview={showAssistantPreview}
          onSelectBackFromTemplates={onSelectBackFromTemplates}
          onToggleFeaturePreviewModal={onToggleFeaturePreviewModal}
        />,
      ]}
      onCancel={confirmOnClose}
    >
      <div>
        <CloseConfirmationModal {...closeConfirmationModalProps} />
        {view === POLICY_MODAL_VIEWS.SELECTION ? (
          <PolicySelection
            description="Write rules with PostgreSQL's policies to fit your unique business needs."
            onViewTemplates={onViewTemplates}
            onViewEditor={onViewEditor}
            showAssistantPreview={showAssistantPreview}
            onToggleFeaturePreviewModal={onToggleFeaturePreviewModal}
          />
        ) : view === POLICY_MODAL_VIEWS.EDITOR ? (
          <PolicyEditor
            isNewPolicy={isNewPolicy}
            policyFormFields={policyFormFields}
            onUpdatePolicyFormFields={onUpdatePolicyFormFields}
            onViewTemplates={onViewTemplates}
            onReviewPolicy={validatePolicyFormFields}
          />
        ) : view === POLICY_MODAL_VIEWS.TEMPLATES ? (
          <PolicyTemplates
            templates={getGeneralPolicyTemplates(schema, table).filter((policy) => !policy.preview)}
            templatesNote="* References a specific column in the table"
            onUseTemplate={onUseTemplate}
          />
        ) : view === POLICY_MODAL_VIEWS.REVIEW ? (
          <PolicyReview
            policy={policyStatementForReview}
            onSelectBack={onViewEditor}
            onSelectSave={onReviewSave}
          />
        ) : null}
      </div>
    </Modal>
  )
}

Subdomains

Frequently Asked Questions

What does PolicyEditorModal() do?
PolicyEditorModal() is a function in the supabase codebase.
What does PolicyEditorModal() call?
PolicyEditorModal() calls 4 function(s): createPayloadForCreatePolicy, createPayloadForUpdatePolicy, createSQLPolicy, getGeneralPolicyTemplates.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free