Files
supabase/apps/studio/components/interfaces/Settings/Logs/Logs.SavedQueriesItem.tsx
Alaister YoungandJoshen Lim f6d9617413 chore: paginate user content (#30675)
* chore: paginate user content

* progress

* loading states

* add load more buttons to private snippets

* working pagination

* fix some types

* always show snippet counts

* support new api parameters

* favorite snippets

* progress

* searching

* paginate root folder

* fix renaming snippets

* fix ts

* removed unused prop

* Shift sharing/unsharing query logic outside of valtio to leverage on RQ only

* Fix invalidation on an unsaved snippet

* Clean up

* Fix

* Clean up

* Update API type

* Update API

* fix duplicate snippets error after moving a snippet

* add currently selected snippet

* Fix unsharing a snippet that has yet to be opened

* i'm dumb

* fix sharing a snippet

* fix sharing and unsharing

* show favorite or shared snippet in list even if it's in another page

* Fix wrong import for debounce

* Fix false positive toast error when creating custom report

* Update API type

* Change create new snippet CTA to link back to /new with skip flag

* Fix saving logs explorer query

* Bump page number

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-12-19 16:08:14 +08:00

127 lines
3.8 KiB
TypeScript

import { useRouter } from 'next/router'
import { useState } from 'react'
import { toast } from 'sonner'
import { useParams } from 'common'
import { useContentDeleteMutation } from 'data/content/content-delete-mutation'
import { useContentUpsertMutation } from 'data/content/content-upsert-mutation'
import { DropdownMenuItem, DropdownMenuSeparator } from 'ui'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import { UpdateSavedQueryModal } from './Logs.UpdateSavedQueryModal'
import { Edit, Trash } from 'lucide-react'
import { SqlEditor } from 'icons'
import { LogsSidebarItem } from './SidebarV2/SidebarItem'
interface SavedQueriesItemProps {
item: {
id: string
name: string
description?: string
owner_id: number
content: {
sql: string
}
}
}
const SavedQueriesItem = ({ item }: SavedQueriesItemProps) => {
const router = useRouter()
const { ref } = useParams()
const [showConfirmModal, setShowConfirmModal] = useState<boolean>(false)
const [showUpdateModal, setShowUpdateModal] = useState<boolean>(false)
const { mutate: deleteContent } = useContentDeleteMutation({
onSuccess: () => {
setShowConfirmModal(false)
toast.success('Successfully deleted query')
},
onError: (error) => {
toast.error(`Failed to delete saved query: ${error.message}`)
},
})
const { mutate: updateContent } = useContentUpsertMutation({
onSuccess: () => {
setShowUpdateModal(false)
toast.success('Successfully updated query')
},
onError: (error) => {
toast.error(`Failed to update query: ${error.message}`)
},
})
const onConfirmDelete = async () => {
if (!ref || typeof ref !== 'string') return console.error('Invalid project reference')
deleteContent({ projectRef: ref, ids: [item.id] })
}
const onConfirmUpdate = async ({ name, description }: { name: string; description?: string }) => {
if (!ref || typeof ref !== 'string') return console.error('Invalid project reference')
updateContent({
projectRef: ref,
payload: {
...item,
name,
description: description || undefined,
type: 'log_sql',
visibility: 'user',
},
})
}
const isActive = router.query.queryId === item.id
return (
<>
<LogsSidebarItem
label={item.name}
icon={<SqlEditor size="15" />}
href={`/project/${ref}/logs/explorer?queryId=${encodeURIComponent(item.id)}&q=${encodeURIComponent(item.content.sql)}`}
isActive={isActive}
dropdownItems={
<>
<DropdownMenuItem onClick={() => setShowUpdateModal(true)}>
<Edit size={14} className="mr-2" />
Edit query
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => {
setShowConfirmModal(true)
}}
>
<Trash size={14} className="mr-2" />
Delete query
</DropdownMenuItem>
</>
}
></LogsSidebarItem>
<ConfirmationModal
variant="destructive"
visible={showConfirmModal}
confirmLabel="Delete query"
title="Confirm to delete saved query"
onCancel={() => {
setShowConfirmModal(false)
}}
onConfirm={onConfirmDelete}
>
<p className="text-sm text-foreground-light">
Are you sure you want to delete {item.name}?
</p>
</ConfirmationModal>
<UpdateSavedQueryModal
visible={showUpdateModal}
initialValues={{ name: item.name, description: item.description }}
onCancel={() => {
setShowUpdateModal(false)
}}
onSubmit={(newValues) => {
onConfirmUpdate(newValues)
}}
/>
</>
)
}
export default SavedQueriesItem