Files
supabase/apps/studio/components/interfaces/TableGridEditor/TableDefinition.tsx
T
Francesco SansalvadoreandJonathan Summers-Muir be9590c890 Multiple themes management (#18871)
* set up multiple themes in studio

* set up multiple themes in studio

* set up multiple themes in docs and www

* update all resolvedTheme to also include deep-dark

* update all resolvedTheme checks to also include deep-dark

* update tailwind.config.js tokens

* update tailwind.config.js tokens

* update leftover scale12 token

* update if resolvedTheme _doesn't_ include 'dark'

* update more styling tokens

* add dynamic themes to CmdK

* fix nav and footer for multi theme

* add data-theme selector output to transformTokens.js

* update code-hike.css to target data-theme css

* update tailwindcss to ^3.3.5

* ThemeImage with light and dark src for www and docs

* add brand-button styling token

* update old dark theme boolean

* update old dark theme boolean

* make homepage product visuals themeable

* update product page themed images

* update badge green with brand

* fix roles list appearance

* fix auth widget in auth page

* update more dark logic

* update more dark logic

* add button default bg and border

* update pricing page theme styling

* clean up Themeimage

* remove forceDark in homepage

* update dark:border-dark occurrences

* update dark:border-dark occurrences

* fix dark mode base colors

* remove foreground-strong

* fix notification badge bg

* remove some dark: selectors

* update dark: selectors

* update code-hike deep dark bg color

* fix comment typo

* update border-button-hover token

* fix customer story logo

* remove some more dark: selectors

* restore forceDark in www homepage

* fix auth react icon

* fix homepage product visuals

* remove theme

* add brand-link token

* fix checkbox bg

* npm install

* more visible EntityListItem active bg

* fix --background-alternative-default css vars

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2023-11-16 16:41:53 +00:00

135 lines
3.8 KiB
TypeScript

import Editor from '@monaco-editor/react'
import { useTheme } from 'next-themes'
import { useParams } from 'common'
import { observer } from 'mobx-react-lite'
import { useMemo, useRef } from 'react'
import { format } from 'sql-formatter'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
import { useTableDefinitionQuery } from 'data/database/table-definition-query'
import { useViewDefinitionQuery } from 'data/database/view-definition-query'
import { ENTITY_TYPE } from 'data/entity-types/entity-type-constants'
import useEntityType from 'hooks/misc/useEntityType'
import { timeout } from 'lib/helpers'
import Link from 'next/link'
import { Button } from 'ui'
export interface TableDefinitionProps {
id?: number
}
const TableDefinition = ({ id }: TableDefinitionProps) => {
const { ref } = useParams()
const editorRef = useRef(null)
const monacoRef = useRef(null)
const { resolvedTheme } = useTheme()
const entityType = useEntityType(id)
const { project } = useProjectContext()
const viewResult = useViewDefinitionQuery(
{
schema: entityType?.schema,
name: entityType?.name,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled:
entityType?.type === ENTITY_TYPE.VIEW || entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW,
}
)
const tableResult = useTableDefinitionQuery(
{
schema: entityType?.schema,
name: entityType?.name,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled: entityType?.type === ENTITY_TYPE.TABLE,
}
)
const { data: definition, isLoading } =
entityType?.type === ENTITY_TYPE.VIEW || entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW
? viewResult
: tableResult
const prepend =
entityType?.type === ENTITY_TYPE.VIEW
? `create view ${entityType.schema}.${entityType.name} as\n`
: entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW
? `create materialized view ${entityType.schema}.${entityType.name} as\n`
: ''
const formattedDefinition = useMemo(
() =>
definition
? format(prepend + definition, {
language: 'postgresql',
keywordCase: 'lower',
})
: undefined,
[definition]
)
const handleEditorOnMount = async (editor: any, monaco: any) => {
editorRef.current = editor
monacoRef.current = monaco
// add margin above first line
editor.changeViewZones((accessor: any) => {
accessor.addZone({
afterLineNumber: 0,
heightInPx: 4,
domNode: document.createElement('div'),
})
})
// when editor did mount, it will need a delay before focus() works properly
await timeout(500)
editor?.focus()
}
if (isLoading) {
return (
<div className="p-4">
<GenericSkeletonLoader />
</div>
)
}
return (
<div className="flex-grow overflow-y-auto border-t border-muted relative">
<Button asChild type="default" className="absolute top-2 right-5 z-10">
<Link
href={`/project/${ref}/sql/new?content=${encodeURIComponent(formattedDefinition ?? '')}`}
>
Open in SQL Editor
</Link>
</Button>
<Editor
className="monaco-editor"
theme={resolvedTheme?.includes('dark') ? 'vs-dark' : 'vs'}
onMount={handleEditorOnMount}
defaultLanguage="pgsql"
value={formattedDefinition}
path={''}
options={{
domReadOnly: true,
readOnly: true,
tabSize: 2,
fontSize: 13,
minimap: { enabled: false },
wordWrap: 'on',
fixedOverflowWidgets: true,
}}
/>
</div>
)
}
export default observer(TableDefinition)