DisableRuleModal.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { useParams } from 'common'
  2. import { useRouter } from 'next/router'
  3. import { useState } from 'react'
  4. import { toast } from 'sonner'
  5. import {
  6. Button,
  7. Dialog,
  8. DialogContent,
  9. DialogFooter,
  10. DialogHeader,
  11. DialogSection,
  12. DialogSectionSeparator,
  13. DialogTitle,
  14. DialogTrigger,
  15. } from 'ui'
  16. import { LintInfo } from '../Linter/Linter.constants'
  17. import { lintInfoMap } from '../Linter/Linter.utils'
  18. import { useLintRuleCreateMutation } from '@/data/lint/create-lint-rule-mutation'
  19. interface DisableRuleModalProps {
  20. lint: LintInfo
  21. }
  22. export const DisableRuleModal = ({ lint }: DisableRuleModalProps) => {
  23. const { ref } = useParams()
  24. const router = useRouter()
  25. const routeCategory = router.pathname.split('/').pop()
  26. const [open, setOpen] = useState(false)
  27. const { mutate: createRule, isPending: isCreating } = useLintRuleCreateMutation({
  28. onSuccess: (_, vars) => {
  29. const ruleLint = vars.exception.lint_name
  30. const ruleLintMeta = lintInfoMap.find((x) => x.name === ruleLint)
  31. toast.success(`Successfully disabled the "${ruleLintMeta?.title}" rule`)
  32. if (ruleLintMeta) {
  33. if (!!routeCategory && routeCategory !== ruleLintMeta.category) {
  34. router.push(
  35. `/project/${ref}/advisors/rules/${ruleLintMeta.category}?lint=${ruleLintMeta.name}`
  36. )
  37. }
  38. }
  39. setOpen(false)
  40. },
  41. })
  42. const onCreateRule = () => {
  43. if (!ref) return console.error('Project ref is required')
  44. createRule({
  45. projectRef: ref,
  46. exception: {
  47. is_disabled: true,
  48. lint_category: undefined,
  49. lint_name: lint.name,
  50. assigned_to: undefined,
  51. },
  52. })
  53. }
  54. return (
  55. <Dialog open={open} onOpenChange={setOpen}>
  56. <DialogTrigger asChild>
  57. <Button type="default">Disable rule</Button>
  58. </DialogTrigger>
  59. <DialogContent size="small">
  60. <DialogHeader>
  61. <DialogTitle>Confirm to disable rule</DialogTitle>
  62. </DialogHeader>
  63. <DialogSectionSeparator />
  64. <DialogSection>
  65. <p className="text-sm">
  66. This will silence the "{lint.title}" by hiding this rule in the Advisor reports, as well
  67. omitting this rule from email notifications for this project.
  68. </p>
  69. </DialogSection>
  70. <DialogFooter>
  71. <Button disabled={isCreating} type="default" onClick={() => setOpen(false)}>
  72. Cancel
  73. </Button>
  74. <Button loading={isCreating} type="primary" onClick={onCreateRule}>
  75. Disable
  76. </Button>
  77. </DialogFooter>
  78. </DialogContent>
  79. </Dialog>
  80. )
  81. }