Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Simplify Generator controls #184

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions src/views/Generator/GeneraterControls/GeneratorControls.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
import { useState } from 'react'
import { Button, ButtonProps, Tooltip } from '@radix-ui/themes'
import {
Button,
ButtonProps,
DropdownMenu,
IconButton,
Tooltip,
} from '@radix-ui/themes'

import { useScriptPreview } from '@/hooks/useScriptPreview'
import { exportScript } from '../Generator.utils'
import { ValidatorDialog } from './ValidatorDialog'
import { CheckCircledIcon, DotsVerticalIcon } from '@radix-ui/react-icons'
import { useGeneratorParams } from '../Generator.hooks'
import { useNavigate } from 'react-router-dom'
import { getRoutePath } from '@/routeMap'

interface GeneratorControlsProps {
onSave: () => void
isDirty: boolean
}

export function GeneratorControls({ onSave, isDirty }: GeneratorControlsProps) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

const { fileName } = useGeneratorParams()
const [isDialogOpen, setIsDialogOpen] = useState(false)
const { preview, hasError } = useScriptPreview()
const navigate = useNavigate()
const tooltip = hasError ? 'Invalid script. Please check your rules' : ''

const handleDeleteGenerator = async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a confirmation dialog. I'll add a reusable one in a separate PR - we'll need it in other views and the sidebar as well

await window.studio.ui.deleteFile(fileName)

navigate(getRoutePath('home'))
}

return (
<>
{!!preview && (
<>
<ButtonWithTooltip
variant="outline"
variant="ghost"
color="gray"
disabled={hasError}
tooltip={tooltip}
onClick={() => {
setIsDialogOpen(true)
}}
>
<CheckCircledIcon />
Validate script
</ButtonWithTooltip>
<ButtonWithTooltip
variant="outline"
disabled={hasError}
tooltip={tooltip}
onClick={exportScript}
>
Export script
</ButtonWithTooltip>
<ValidatorDialog
script={preview}
open={isDialogOpen}
Expand All @@ -53,6 +65,22 @@ export function GeneratorControls({ onSave, isDirty }: GeneratorControlsProps) {
>
Save
</ButtonWithTooltip>
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<IconButton variant="ghost" color="gray">
<DotsVerticalIcon />
</IconButton>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Item onSelect={exportScript} disabled={hasError}>
Export script
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item onSelect={handleDeleteGenerator} color="red">
Delete generator
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
</>
)
}
Expand Down
5 changes: 3 additions & 2 deletions src/views/Generator/NewRuleMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { PlusCircledIcon } from '@radix-ui/react-icons'
import { Button, DropdownMenu } from '@radix-ui/themes'
import { ComponentProps } from 'react'

import { useGeneratorStore } from '@/store/generator'
import { TestRule } from '@/types/rules'
import { createEmptyRule } from '@/utils/rules'

export function NewRuleMenu() {
export function NewRuleMenu(props: ComponentProps<typeof Button>) {
const addRule = useGeneratorStore((store) => store.addRule)

const createRule = (type: TestRule['type']) => {
Expand All @@ -16,7 +17,7 @@ export function NewRuleMenu() {
return (
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button variant="ghost" size="1" color="orange" mr="2">
<Button variant="ghost" size="1" color="gray" mr="2" {...props}>
<PlusCircledIcon />
Add rule
</Button>
Expand Down
2 changes: 2 additions & 0 deletions src/views/Generator/TestRuleContainer/TestRule/TestRule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export function TestRuleItem({
css={css`
cursor: grab;
`}
width={16}
height={16}
aria-hidden
/>
</IconButton>
Expand Down
31 changes: 28 additions & 3 deletions src/views/Generator/TestRuleContainer/TestRuleContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useGeneratorStore } from '@/store/generator'
import { Flex, Heading, ScrollArea } from '@radix-ui/themes'
import { Flex, Heading, ScrollArea, Text } from '@radix-ui/themes'
import { NewRuleMenu } from '../NewRuleMenu'
import { SortableRuleList } from './SortableRuleList'
import { css } from '@emotion/react'
import { TestOptions } from '../TestOptions'
import grotIllustration from '@/assets/grot.svg'

export function TestRuleContainer() {
const rules = useGeneratorStore((store) => store.rules)
Expand All @@ -16,7 +17,7 @@ export function TestRuleContainer() {
align="center"
top="0"
pr="2"
gap="4"
gap="2"
css={css`
background-color: var(--color-background);
z-index: 1;
Expand All @@ -33,11 +34,35 @@ export function TestRuleContainer() {
>
Test rules ({rules.length})
</Heading>
<TestOptions />
<NewRuleMenu />
<TestOptions />
</Flex>

<SortableRuleList rules={rules} onSwapRules={swapRules} />
<Flex
py="3"
px="6"
align={rules.length === 0 ? 'center' : 'start'}
direction="column"
gap="3"
>
{rules.length === 0 ? (
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

<>
<img
src={grotIllustration}
css={css`
max-height: 200px;
`}
/>
<Text size="1" color="gray">
Start configuring your test logic by adding a new rule
</Text>
<NewRuleMenu variant="solid" size="2" />
</>
) : (
<NewRuleMenu />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

)}
</Flex>
</ScrollArea>
)
}
Loading