UnshareSnippetModal.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { useParams } from 'common'
  2. import { EyeOffIcon } from 'lucide-react'
  3. import { toast } from 'sonner'
  4. import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
  5. import { getContentById } from '@/data/content/content-id-query'
  6. import { useContentUpsertMutation } from '@/data/content/content-upsert-mutation'
  7. import { Snippet } from '@/data/content/sql-folders-query'
  8. import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor-v2'
  9. import type { SqlSnippets } from '@/types'
  10. export const UnshareSnippetModal = ({
  11. snippet,
  12. onClose,
  13. onSuccess,
  14. }: {
  15. snippet?: Snippet
  16. onClose: () => void
  17. onSuccess?: () => void
  18. }) => {
  19. const { ref: projectRef } = useParams()
  20. const snapV2 = useSqlEditorV2StateSnapshot()
  21. const { mutate: upsertContent } = useContentUpsertMutation({
  22. onError: (error) => {
  23. toast.error(`Failed to update query: ${error.message}`)
  24. },
  25. })
  26. const onUnshareSnippet = async () => {
  27. if (!projectRef) return console.error('Project ref is required')
  28. if (!snippet) return console.error('Snippet ID is required')
  29. const storeSnippet = snapV2.snippets[snippet.id]
  30. let snippetContent = storeSnippet?.snippet?.content
  31. if (snippetContent === undefined) {
  32. const { content } = await getContentById({ projectRef, id: snippet.id })
  33. snippetContent = content as unknown as SqlSnippets.Content
  34. }
  35. // [Joshen] Just as a final check - to ensure that the content is minimally there (empty string is fine)
  36. if (snippetContent === undefined) {
  37. return toast.error('Unable to update snippet visibility: Content is missing')
  38. }
  39. upsertContent(
  40. {
  41. projectRef,
  42. payload: {
  43. ...snippet,
  44. visibility: 'user',
  45. folder_id: null,
  46. content: snippetContent,
  47. },
  48. },
  49. {
  50. onSuccess: () => {
  51. snapV2.updateSnippet({
  52. id: snippet.id,
  53. snippet: { visibility: 'user', folder_id: null },
  54. skipSave: true,
  55. })
  56. toast.success('Snippet is now unshared from the project')
  57. onSuccess?.()
  58. onClose()
  59. },
  60. }
  61. )
  62. }
  63. return (
  64. <ConfirmationModal
  65. size="medium"
  66. title={`Confirm to unshare query: ${snippet?.name}`}
  67. confirmLabel="Unshare query"
  68. confirmLabelLoading="Unsharing query"
  69. visible={snippet !== undefined}
  70. onCancel={() => onClose()}
  71. onConfirm={() => onUnshareSnippet()}
  72. alert={{
  73. title: 'This SQL query will no longer be public to all team members',
  74. description: 'Only you will have access to this query',
  75. }}
  76. >
  77. <ul
  78. data-testid="confirm-unshare-snippet-modal"
  79. className="text-sm text-foreground-light space-y-5"
  80. >
  81. <li className="flex gap-3">
  82. <EyeOffIcon />
  83. <span>Project members will no longer be able to view this query.</span>
  84. </li>
  85. </ul>
  86. </ConfirmationModal>
  87. )
  88. }