Files
supabase/apps/studio/components/interfaces/Database/Triggers/ChooseFunctionForm.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

201 lines
6.0 KiB
TypeScript

import { Transition } from '@headlessui/react'
import { Dictionary } from 'components/grid'
import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
import InformationBox from 'components/ui/InformationBox'
import SqlEditor from 'components/ui/SqlEditor'
import { map as lodashMap, uniqBy } from 'lodash'
import { observer, useLocalObservable } from 'mobx-react-lite'
import { useRouter } from 'next/router'
import { createContext, useContext, useState } from 'react'
import { Button, IconChevronDown, IconHelpCircle, IconTerminal, SidePanel } from 'ui'
interface IChooseFunctionFormStore {
functionSchemas: string[]
triggerFunctions: Dictionary<any>[]
}
class ChooseFunctionFormStore implements IChooseFunctionFormStore {
triggerFunctions = []
get functionSchemas() {
return lodashMap(uniqBy(this.triggerFunctions, 'schema'), 'schema')
}
}
const ChooseFunctionFormContext = createContext<IChooseFunctionFormStore | null>(null)
export interface ChooseFunctionFormProps {
triggerFunctions: Dictionary<any>[]
visible: boolean
onChange: (id: number) => void
setVisible: (value: boolean) => void
}
const ChooseFunctionForm = ({
triggerFunctions,
visible,
onChange,
setVisible,
}: ChooseFunctionFormProps) => {
const _localState = useLocalObservable(() => new ChooseFunctionFormStore())
_localState.triggerFunctions = triggerFunctions as any
function selectFunction(id: number) {
onChange(id)
setVisible(!visible)
}
const hasPublicSchemaFunctions = _localState.triggerFunctions.length >= 1
return (
<SidePanel
size="large"
header="Pick a function"
visible={visible}
onCancel={() => setVisible(!visible)}
className="hooks-sidepanel"
>
<div className="py-6">
{hasPublicSchemaFunctions ? (
<ChooseFunctionFormContext.Provider value={_localState}>
<div className="space-y-6">
<NoticeBox />
{_localState.functionSchemas.map((schema: string) => (
<SchemaFunctionGroup key={schema} schema={schema} selectFunction={selectFunction} />
))}
</div>
</ChooseFunctionFormContext.Provider>
) : (
<NoFunctionsState />
)}
</div>
</SidePanel>
)
}
export default observer(ChooseFunctionForm)
const NoticeBox = ({}) => {
const router = useRouter()
const { ref } = router.query
return (
<div className="px-6">
<InformationBox
icon={<IconHelpCircle size="large" strokeWidth={1.5} />}
title="Only functions that return a trigger will be displayed below"
description={`You can make functions by using the Database Functions`}
button={
<Button
type="default"
onClick={() => {
router.push(`/project/${ref}/database/functions`)
}}
>
Go to Functions
</Button>
}
/>
</div>
)
}
const NoFunctionsState = ({}) => {
// for the empty 'no tables' state link
const router = useRouter()
const { ref } = router.query
return (
<ProductEmptyState
title="No Trigger Functions found in database"
ctaButtonLabel="Create a trigger function"
onClickCta={() => {
router.push(`/project/${ref}/database/functions`)
}}
>
<p className="text-sm text-foreground-light">
You will need to create a trigger based function before you can add it to your trigger.
</p>
</ProductEmptyState>
)
}
export interface SchemaFunctionGroupProps {
schema: string
selectFunction: (id: number) => void
}
const SchemaFunctionGroup = observer(({ schema, selectFunction }: SchemaFunctionGroupProps) => {
const _pageState = useContext(ChooseFunctionFormContext)
const _functions = _pageState!.triggerFunctions.filter((x) => x.schema == schema)
return (
<div className="space-y-4">
<div className="sticky top-0 flex items-center space-x-1 px-6 backdrop-blur backdrop-filter">
<h5 className="text-foreground-light">schema</h5>
<h5>{schema}</h5>
</div>
<div className="space-y-0 divide-y border-t border-b border-default">
{_functions.map((x) => (
<Function
id={x.id}
key={x.id}
completeStatement={x.complete_statement}
name={x.name}
onClick={selectFunction}
/>
))}
</div>
</div>
)
})
export interface FunctionProps {
id: number
completeStatement: string
name: string
onClick: (id: number) => void
}
const Function = ({ id, completeStatement, name, onClick }: FunctionProps) => {
const [visible, setVisible] = useState(false)
return (
<div
className="cursor-pointer rounded p-3 px-6 hover:bg-background"
onClick={() => onClick(id)}
>
<div className="flex items-center justify-between space-x-3">
<div className="flex items-center space-x-3">
<div className="flex items-center justify-center rounded bg-foreground p-1 text-background">
<IconTerminal strokeWidth={2} size={14} />
</div>
<p className="mb-0 text-sm">{name}</p>
</div>
<Button
type="text"
onClick={(e) => {
e.stopPropagation()
setVisible(!visible)
}}
icon={
<IconChevronDown className={visible ? 'rotate-180 transform' : 'rotate-0 transform'} />
}
>
{visible ? 'Hide definition' : 'View definition'}
</Button>
</div>
<Transition
show={visible}
enter="transition ease-out duration-300"
enterFrom="transform opacity-0"
enterTo="transform opacity-100"
leave="transition ease-in duration-75"
leaveFrom="transform opacity-100"
leaveTo="transform opacity-0"
>
<div className="mt-4 h-64 border border-default">
<SqlEditor defaultValue={completeStatement} readOnly={true} contextmenu={false} />
</div>
</Transition>
</div>
)
}