Files
supabase/apps/studio/components/interfaces/Docs/Description.tsx
Charis 39ec777d22 studio: SafeSql stragglers + remaining tests (6/7) (#46006)
## Summary

Part 6 of 7 in the SafeSql migration stack. Picks up the few remaining
files that didn't fit cleanly into earlier batches:

- `components/Docs/Description.tsx` — comment statements built via
`safeSql`.
-
`components/Integrations/IntegrationOverviewTabV2/InstallIntegrationSheet.test.tsx`
— test fixture updated to `SafeSqlFragment`.
- `lib/ai/tools/studio-tools.test.ts` — test fixture updated.
- `lib/api/generate-v4.test.ts` — test fixture updated.

Sets up PR 7, which flips the `executeSql` signature itself.

## Test plan

- [x] `pnpm typecheck` passes
- [x] Specific Studio unit tests run on top of the stack
(`Policies.utils.test.ts`, `SidePanelEditor.utils.createTable.test.ts`,
`useQueryInsightsIssues.utils.test.ts`)
- [x] Dev-server smoke: Docs panel renders / accepts edits

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Tests**
  * Updated test suites to reflect internal type definition changes.

* **Refactor**
* Internal code improvements to enhance type safety and consistency
across the codebase.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46006)

<!-- review_stack_entry_end -->

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-15 15:56:57 -04:00

124 lines
3.9 KiB
TypeScript

import { ident, literal, safeSql, type SafeSqlFragment } from '@supabase/pg-meta/src/pg-format'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { noop } from 'lodash'
import { Loader } from 'lucide-react'
import { useState } from 'react'
import { toast } from 'sonner'
import { Button, ExpandingTextArea } from 'ui'
import { executeSql } from '@/data/sql/execute-sql-query'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { timeout } from '@/lib/helpers'
// Removes some auto-generated Postgrest text
// Ideally PostgREST wouldn't add this if there is already a comment
const temp_removePostgrestText = (content: string) => {
const postgrestTextPk = `Note:\nThis is a Primary Key.<pk/>`
const postgrestTextFk = `Note:\nThis is a Foreign Key to`
const pkTextPos = content.lastIndexOf(postgrestTextPk)
const fkTextPos = content.lastIndexOf(postgrestTextFk)
let cleansed = content
if (pkTextPos >= 0) cleansed = cleansed.substring(0, pkTextPos)
if (fkTextPos >= 0) cleansed = cleansed.substring(0, fkTextPos)
return cleansed
}
interface DescrptionProps {
content: string
metadata: { table?: string; column?: string; rpc?: string }
onChange: (value: string) => void
}
const Description = ({ content, metadata, onChange = noop }: DescrptionProps) => {
const contentText = temp_removePostgrestText(content || '').trim()
const [value, setValue] = useState(contentText)
const [isUpdating, setIsUpdating] = useState(false)
const { data: project } = useSelectedProjectQuery()
const { table, column, rpc } = metadata
const hasChanged = value != contentText
const animateCss = `transition duration-150`
const { can: canUpdateDescription } = useAsyncCheckPermissions(
PermissionAction.TENANT_SQL_QUERY,
'*'
)
const updateDescription = async () => {
if (isUpdating || !canUpdateDescription) return false
setIsUpdating(true)
let query: SafeSqlFragment | undefined
if (table && column)
query = safeSql`comment on column ${ident('public')}.${ident(table)}.${ident(column)} is ${literal(value)};`
if (table && !column)
query = safeSql`comment on table ${ident('public')}.${ident(table)} is ${literal(value)};`
if (rpc) query = safeSql`comment on function ${ident(rpc)} is ${literal(value)};`
if (query) {
try {
await executeSql({
projectRef: project?.ref,
connectionString: project?.connectionString,
sql: query,
})
// [Joshen] Temp fix, immediately refreshing the docs fetches stale state
await timeout(500)
toast.success(`Successfully updated description`)
} catch (error: any) {
toast.error(`Failed to update description: ${error.message}`)
}
}
onChange(value)
setIsUpdating(false)
}
if (!canUpdateDescription) {
return (
<span className={`block text-sm ${value ? 'text-foreground' : ''}`}>
{value || 'No description'}
</span>
)
}
return (
<div className="space-y-2 px-0.5">
<ExpandingTextArea
className="w-full min-h-auto"
placeholder="Click to edit."
value={value}
onChange={(e: any) => setValue(e.target.value)}
/>
<div
className={`flex items-center gap-2 ${
hasChanged ? 'opacity-100' : 'h-0 cursor-default opacity-0'
} ${animateCss}`}
>
<Button
type="default"
disabled={!hasChanged}
onClick={() => {
setValue(contentText)
setIsUpdating(false)
}}
>
Cancel
</Button>
<Button disabled={!hasChanged} onClick={updateDescription}>
{isUpdating ? (
<Loader className="mx-auto animate-spin" size={14} strokeWidth={2} />
) : (
<span>Save</span>
)}
</Button>
</div>
</div>
)
}
export default Description