Files
supabase/apps/studio/components/ui/CardButton.tsx
Joshen Lim 8deb0865c5 Chore/rls assistant iteration templates (#21447)
* Add templates to RLS assistant

* Update button

* lint

* Chore/rls assistant iteration templates styling (#21459)

* Update index.tsx

* update icon pack

* add new classes

* expose hover card

* add slight shadow to scroll area

* monaco editor updated

* tabs border-b updated

* message styling updated to use circle AI icon

* templates side panel updated to use new tailwind classes and ScrollArea

* Updated tab label to "Assistant" and adjusted background colors

* header updated to use simpler open/close button

* divide-y styling added to chat

* fix tabs bg state

* shadcn tooltip updated to use correct bg color and added default delay duration

* updated styling of codeblocks in chat

* updated open / close button. also added a tooltip

* divide border styling

* add 'slide in' animation option for Hover Card

* update code block

* switch to Card Button

* Update CardButton to expose props and allow any props to be applied

* init

* Apply suggestions from code review

* Add entry step when clicking on create new policy for rls ai

* templates code replacement state highlight selected template

* Ai message code replacement state highlight selected message

* Opt to leave templates panel open

* Fix templates to use template name in policy sql

* Fixes

* fix

* Remove unused imports

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2024-02-28 14:44:17 +08:00

173 lines
4.0 KiB
TypeScript

import Link from 'next/link'
import React, { PropsWithChildren } from 'react'
import { IconChevronRight, IconLoader, cn } from 'ui'
interface CardButtonProps {
title?: string | React.ReactNode
description?: string
footer?: React.ReactNode
url?: string
linkHref?: string
imgUrl?: string
imgAlt?: string
onClick?: () => void
icon?: React.ReactNode
loading?: boolean
className?: string
fixedHeight?: boolean
hideChevron?: boolean
titleClass?: string
}
// Define separate interfaces for each type of container
interface LinkContainerProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'title'> {
href: string
}
interface UrlContainerProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'title'> {
href: string
}
interface NonLinkContainerProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {}
interface ButtonContainerProps
extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'title'> {}
// Union of all container props
type ContainerProps =
| LinkContainerProps
| UrlContainerProps
| NonLinkContainerProps
| ButtonContainerProps
const CardButton = ({
title,
description,
children,
footer,
url = '',
linkHref = '',
imgUrl,
imgAlt,
icon,
className,
loading = false,
fixedHeight = true,
hideChevron = false,
titleClass = '',
...props
}: PropsWithChildren<CardButtonProps & ContainerProps>) => {
const isLink = url || linkHref || props.onClick
let Container: React.ElementType
let containerProps: ContainerProps = {}
if (props.onClick) {
Container = 'button'
containerProps = props
} else if (linkHref) {
Container = Link
containerProps = {
href: linkHref,
...props,
}
} else if (url) {
Container = 'a'
containerProps = {
href: url,
...props,
}
} else {
Container = 'div'
containerProps = props
}
let containerClasses = [
'group relative text-left',
'bg-surface-100',
'border border-surface',
'rounded-md p-5 flex flex-row',
'transition ease-in-out duration-150',
]
if (isLink) {
containerClasses = [
...containerClasses,
'cursor-pointer',
'hover:bg-surface-200',
'hover:border-control',
]
}
if (fixedHeight) {
containerClasses = [...containerClasses, 'h-32']
}
const ImageContainer = ({ children }: { children: React.ReactNode }) => {
return <div className="mr-4 flex flex-col">{children}</div>
}
const contents = (
<>
{imgUrl && (
<ImageContainer>
<img
className="
transition-all
group-hover:scale-110
"
src={`${imgUrl}`}
alt={`${imgAlt}`}
width="26"
/>
</ImageContainer>
)}
{icon && <ImageContainer>{icon}</ImageContainer>}
<div className="flex h-full w-full flex-col space-y-2">
{typeof title === 'string' ? (
<h5 className={`text-foreground ${titleClass}`}>{title}</h5>
) : (
title
)}
{(children || description) && (
<div className="flex w-full flex-1 flex-col">
<p className="text-sm text-foreground-light">{description}</p>
<div className="w-full">{children && children}</div>
</div>
)}
{footer && <div className="w-full !mt-auto">{footer}</div>}
</div>
{isLink && (
<div
className="
absolute
right-4
top-4
text-foreground-lighter
transition-all
duration-200
group-hover:right-3
group-hover:text-foreground
"
>
{loading ? (
<IconLoader className="animate-spin" />
) : !hideChevron ? (
<IconChevronRight />
) : (
<></>
)}
</div>
)}
</>
)
return (
<Container {...containerProps} className={cn(containerClasses, className)}>
{contents}
</Container>
)
}
export default CardButton