Logs.SavedQueriesItem.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { useParams } from 'common'
  2. import { SqlEditor } from 'icons'
  3. import { Edit, Trash } from 'lucide-react'
  4. import { useRouter } from 'next/router'
  5. import { useState } from 'react'
  6. import { toast } from 'sonner'
  7. import { DropdownMenuItem, DropdownMenuSeparator } from 'ui'
  8. import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
  9. import { UpdateSavedQueryModal } from './Logs.UpdateSavedQueryModal'
  10. import { LogsSidebarItem } from './SidebarV2/SidebarItem'
  11. import { useContentDeleteMutation } from '@/data/content/content-delete-mutation'
  12. import { useContentUpsertMutation } from '@/data/content/content-upsert-mutation'
  13. interface SavedQueriesItemProps {
  14. item: {
  15. id: string
  16. name: string
  17. description?: string
  18. owner_id: number
  19. content: {
  20. sql: string
  21. }
  22. }
  23. }
  24. const SavedQueriesItem = ({ item }: SavedQueriesItemProps) => {
  25. const router = useRouter()
  26. const { ref } = useParams()
  27. const [showConfirmModal, setShowConfirmModal] = useState<boolean>(false)
  28. const [showUpdateModal, setShowUpdateModal] = useState<boolean>(false)
  29. const { mutate: deleteContent } = useContentDeleteMutation({
  30. onSuccess: () => {
  31. setShowConfirmModal(false)
  32. toast.success('Successfully deleted query')
  33. },
  34. onError: (error) => {
  35. toast.error(`Failed to delete saved query: ${error.message}`)
  36. },
  37. })
  38. const { mutateAsync: updateContent } = useContentUpsertMutation({
  39. onSuccess: () => {
  40. setShowUpdateModal(false)
  41. toast.success('Successfully updated query')
  42. },
  43. onError: (error) => {
  44. toast.error(`Failed to update query: ${error.message}`)
  45. },
  46. })
  47. const onConfirmDelete = async () => {
  48. if (!ref || typeof ref !== 'string') return console.error('Invalid project reference')
  49. deleteContent({ projectRef: ref, ids: [item.id] })
  50. }
  51. const onConfirmUpdate = async ({ name, description }: { name: string; description?: string }) => {
  52. if (!ref || typeof ref !== 'string') return console.error('Invalid project reference')
  53. await updateContent({
  54. projectRef: ref,
  55. payload: {
  56. ...item,
  57. name,
  58. description: description || undefined,
  59. type: 'log_sql',
  60. visibility: 'user',
  61. },
  62. })
  63. }
  64. const isActive = router.query.queryId === item.id
  65. return (
  66. <>
  67. <LogsSidebarItem
  68. label={item.name}
  69. icon={<SqlEditor size="15" />}
  70. href={`/project/${ref}/logs/explorer?queryId=${encodeURIComponent(item.id)}&q=${encodeURIComponent(item.content.sql)}`}
  71. isActive={isActive}
  72. dropdownItems={
  73. <>
  74. <DropdownMenuItem onClick={() => setShowUpdateModal(true)}>
  75. <Edit size={14} className="mr-2" />
  76. Edit query
  77. </DropdownMenuItem>
  78. <DropdownMenuSeparator />
  79. <DropdownMenuItem
  80. onClick={() => {
  81. setShowConfirmModal(true)
  82. }}
  83. >
  84. <Trash size={14} className="mr-2" />
  85. Delete query
  86. </DropdownMenuItem>
  87. </>
  88. }
  89. ></LogsSidebarItem>
  90. <ConfirmationModal
  91. variant="destructive"
  92. visible={showConfirmModal}
  93. confirmLabel="Delete query"
  94. title="Confirm to delete saved query"
  95. onCancel={() => {
  96. setShowConfirmModal(false)
  97. }}
  98. onConfirm={onConfirmDelete}
  99. >
  100. <p className="text-sm text-foreground-light">
  101. Are you sure you want to delete {item.name}?
  102. </p>
  103. </ConfirmationModal>
  104. <UpdateSavedQueryModal
  105. header="Update saved query"
  106. visible={showUpdateModal}
  107. initialValues={{ name: item.name, description: item.description }}
  108. onCancel={() => {
  109. setShowUpdateModal(false)
  110. }}
  111. onSubmit={onConfirmUpdate}
  112. />
  113. </>
  114. )
  115. }
  116. export default SavedQueriesItem