Files
df52ea7ee0 feat: Replace all toasts with sonner (#28250)
* Update the design of the sonner toasts. Add the close button by default.

* Migrate studio and www apps to use the SonnerToaster.

* Migrate all toasts from studio.

* Migrate all leftover toasts in studio.

* Add a new toast component with progress. Use it in studio.

* Migrate the design-system app.

* Refactor the consent toast to use sonner.

* Switch docs to use the new sonner toasts.

* Remove toast examples from the design-system app.

* Remove all toast-related components and old code.

* Fix the progress bar in the toast progress component. Also make the bottom components vertically centered.

* Fix the width of the toast progress.

* Use text-foreground-lighter instead of muted for ToastProgress text

* Rename ToastProgress to SonnerProgress.

* Shorten the text in sonner progress.

* Use the correct classes for the close button. Add a const var for the default toast duration. Remove the custom width class from sonner.

* Set the position for all progress toasts to bottom right. Set the duration for all toasts to the default (when reusing a toast id from loading/progress toast, the duration is set to infinity).

* Fix the playwright tests.

* Refactor imports to use ui instead of @ui.

* Change all imports of react-hot-toast with sonner. These components were merged since the last commit to this branch.

* Remove react-hot-toast lib.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2024-08-31 07:50:51 +08:00

103 lines
3.4 KiB
TypeScript

import type { PostgresPublication, PostgresTable } from '@supabase/postgres-meta'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useState } from 'react'
import { Badge, Toggle } from 'ui'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import Table from 'components/to-be-cleaned/Table'
import { useDatabasePublicationUpdateMutation } from 'data/database-publications/database-publications-update-mutation'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { toast } from 'sonner'
interface PublicationsTableItemProps {
table: PostgresTable
selectedPublication: PostgresPublication
}
const PublicationsTableItem = ({ table, selectedPublication }: PublicationsTableItemProps) => {
const { project } = useProjectContext()
const enabledForAllTables = selectedPublication.tables == null
const [checked, setChecked] = useState(
selectedPublication.tables?.find((x: any) => x.id == table.id) != undefined
)
const canUpdatePublications = useCheckPermissions(
PermissionAction.TENANT_SQL_ADMIN_WRITE,
'publications'
)
const { mutate: updatePublications, isLoading } = useDatabasePublicationUpdateMutation()
const toggleReplicationForTable = async (
table: PostgresTable,
publication: PostgresPublication
) => {
if (project === undefined) return console.error('Project is required')
const originalChecked = checked
setChecked(!checked)
const publicationTables = publication?.tables ?? []
const exists = publicationTables.some((x: any) => x.id == table.id)
const tables = !exists
? [`${table.schema}.${table.name}`].concat(
publicationTables.map((t: any) => `${t.schema}.${t.name}`)
)
: publicationTables
.filter((x: any) => x.id != table.id)
.map((x: any) => `${x.schema}.${x.name}`)
updatePublications(
{
projectRef: project?.ref,
connectionString: project?.connectionString,
id: publication.id,
tables,
},
{
onSuccess: () => {
toast.success(
`Successfully ${checked ? 'disabled' : 'enabled'} replication for ${table.name}`
)
},
onError: (error) => {
toast.error(`Failed to toggle replication for ${table.name}: ${error.message}`)
setChecked(originalChecked)
},
}
)
}
return (
<Table.tr key={table.id}>
<Table.td className="whitespace-nowrap">{table.name}</Table.td>
<Table.td className="whitespace-nowrap">{table.schema}</Table.td>
<Table.td className="hidden max-w-sm truncate whitespace-nowrap lg:table-cell">
{table.comment}
</Table.td>
<Table.td className="px-4 py-3 pr-2">
<div className="flex justify-end gap-2">
{enabledForAllTables ? (
<Badge>
<span>Enabled</span>
<span className="hidden lg:inline-block">&nbsp;for all tables</span>
</Badge>
) : (
<Toggle
size="tiny"
align="right"
disabled={!canUpdatePublications || isLoading}
className="m-0 ml-2 mt-1 -mb-1 p-0"
checked={checked}
onChange={() => toggleReplicationForTable(table, selectedPublication)}
/>
)}
</div>
</Table.td>
</Table.tr>
)
}
export default PublicationsTableItem