|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Flex, |
| 4 | + FormControl, |
| 5 | + FormLabel, |
| 6 | + IconButton, |
| 7 | + Input, |
| 8 | + Popover, |
| 9 | + PopoverBody, |
| 10 | + PopoverContent, |
| 11 | + PopoverTrigger, |
| 12 | + Text, |
| 13 | + useDisclosure, |
| 14 | +} from "@chakra-ui/react" |
| 15 | +import { PiBracketsCurly } from "@react-icons/all-files/pi/PiBracketsCurly" |
| 16 | +import { PiX } from "@react-icons/all-files/pi/PiX" |
| 17 | +import { type FC, useCallback, useState } from "react" |
| 18 | +import { generateVariableName } from "../../variables" |
| 19 | + |
| 20 | +const MAX_LABEL_LENGTH = 64 |
| 21 | + |
| 22 | +/** |
| 23 | + * Field types that can be variabilized (single scalar value). |
| 24 | + */ |
| 25 | +const SUPPORTED_FIELD_TYPES = new Set([ |
| 26 | + "input", |
| 27 | + "textarea", |
| 28 | + "switch", |
| 29 | + "slider", |
| 30 | + "color-picker", |
| 31 | + "select", |
| 32 | + "select-creatable", |
| 33 | + "radio-card", |
| 34 | + "checkbox-card", |
| 35 | +]) |
| 36 | + |
| 37 | +export function canBeVariable(fieldType: string | undefined): boolean { |
| 38 | + return !!fieldType && SUPPORTED_FIELD_TYPES.has(fieldType) |
| 39 | +} |
| 40 | + |
| 41 | +interface Props { |
| 42 | + fieldLabel: string |
| 43 | + takenNames: Iterable<string> |
| 44 | + onCreate: (variable: { name: string; label: string }) => void |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * A small button shown next to field labels in the sidebar. Clicking opens |
| 49 | + * a popover to name the variable. On save, it generates a collision-free |
| 50 | + * name and calls onCreate. |
| 51 | + */ |
| 52 | +export const VariableMarkerButton: FC<Props> = ({ |
| 53 | + fieldLabel, |
| 54 | + takenNames, |
| 55 | + onCreate, |
| 56 | +}) => { |
| 57 | + const { isOpen, onOpen, onClose } = useDisclosure() |
| 58 | + const [label, setLabel] = useState(fieldLabel) |
| 59 | + |
| 60 | + const handleOpen = useCallback(() => { |
| 61 | + setLabel(fieldLabel) |
| 62 | + onOpen() |
| 63 | + }, [fieldLabel, onOpen]) |
| 64 | + |
| 65 | + const handleSave = () => { |
| 66 | + const trimmed = label.trim() || fieldLabel |
| 67 | + const name = generateVariableName(trimmed, takenNames) |
| 68 | + onCreate({ name, label: trimmed }) |
| 69 | + onClose() |
| 70 | + } |
| 71 | + |
| 72 | + return ( |
| 73 | + <Popover isOpen={isOpen} onClose={onClose} placement="bottom-start" isLazy> |
| 74 | + <PopoverTrigger> |
| 75 | + <IconButton |
| 76 | + aria-label="Make variable" |
| 77 | + icon={<PiBracketsCurly />} |
| 78 | + size="xs" |
| 79 | + variant="ghost" |
| 80 | + opacity={0} |
| 81 | + _groupHover={{ opacity: 1 }} |
| 82 | + onClick={handleOpen} |
| 83 | + /> |
| 84 | + </PopoverTrigger> |
| 85 | + <PopoverContent w="260px"> |
| 86 | + <PopoverBody p={3}> |
| 87 | + <Flex justify="space-between" align="center" mb={2}> |
| 88 | + <Text fontSize="sm" fontWeight="600"> |
| 89 | + Create variable |
| 90 | + </Text> |
| 91 | + <IconButton |
| 92 | + aria-label="Close" |
| 93 | + icon={<PiX />} |
| 94 | + size="xs" |
| 95 | + variant="ghost" |
| 96 | + onClick={onClose} |
| 97 | + /> |
| 98 | + </Flex> |
| 99 | + <FormControl> |
| 100 | + <FormLabel fontSize="xs">Label</FormLabel> |
| 101 | + <Input |
| 102 | + size="sm" |
| 103 | + value={label} |
| 104 | + onChange={(e) => setLabel(e.target.value.slice(0, MAX_LABEL_LENGTH))} |
| 105 | + placeholder="e.g. Headline text" |
| 106 | + onKeyDown={(e) => { |
| 107 | + if (e.key === "Enter") handleSave() |
| 108 | + }} |
| 109 | + /> |
| 110 | + </FormControl> |
| 111 | + <Button |
| 112 | + size="sm" |
| 113 | + colorScheme="blue" |
| 114 | + mt={3} |
| 115 | + w="full" |
| 116 | + onClick={handleSave} |
| 117 | + isDisabled={!label.trim()} |
| 118 | + > |
| 119 | + Create |
| 120 | + </Button> |
| 121 | + </PopoverBody> |
| 122 | + </PopoverContent> |
| 123 | + </Popover> |
| 124 | + ) |
| 125 | +} |
0 commit comments