import { Button, Checkbox, Flex, Grid, Popover, PopoverAnchor, PopoverArrow, PopoverBody, PopoverCloseButton, PopoverContent, PopoverTrigger, Slider, SliderFilledTrack, SliderThumb, SliderTrack, Spacer, Tooltip, useBoolean, useColorMode, useColorModeValue, useId, } from "@chakra-ui/react"; import { FlagIcon, QuestionMarkCircleIcon } from "@heroicons/react/20/solid"; import { useState } from "react"; import poster from "src/lib/poster"; import { colors } from "styles/Theme/colors"; import useSWRMutation from "swr/mutation"; interface textFlagLabels { attributeName: string; labelText: string; additionalExplanation?: string; } export const FlaggableElement = (props) => { const [isEditing, setIsEditing] = useBoolean(); const flaggable_labels = props.flaggable_labels; const TEXT_LABEL_FLAGS = flaggable_labels.valid_labels.map((valid_label) => { return { attributeName: valid_label.name, labelText: valid_label.display_text, additionalExplanation: valid_label.help_text, }; }); const { trigger } = useSWRMutation("/api/set_label", poster, { onSuccess: () => { setIsEditing.off; }, }); const submitResponse = () => { const label_map: Map = new Map(); TEXT_LABEL_FLAGS.forEach((flag, i) => { if (checkboxValues[i]) { label_map.set(flag.attributeName, sliderValues[i]); } }); trigger({ message_id: props.message_id, post_id: props.post_id, label_map: Object.fromEntries(label_map), text: props.text, }); }; const [checkboxValues, setCheckboxValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(false)); const [sliderValues, setSliderValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(1)); const handleCheckboxState = (isChecked, idx) => { setCheckboxValues( checkboxValues.map((val, i) => { return i == idx ? isChecked : val; }) ); }; const handleSliderState = (newVal, idx) => { setSliderValues( sliderValues.map((val, i) => { return i == idx ? newVal : val; }) ); }; return ( {props.children}
{TEXT_LABEL_FLAGS.map((option, i) => ( ))}
); }; export function FlagCheckbox(props: { option: textFlagLabels; idx: number; checkboxValues: boolean[]; sliderValues: number[]; checkboxHandler: (newVal: boolean, idx: number) => void; sliderHandler: (newVal: number, idx: number) => void; }): JSX.Element { let AdditionalExplanation = null; if (props.option.additionalExplanation) { AdditionalExplanation = ( ); } const id = useId(); const { colorMode } = useColorMode(); const labelTextClass = colorMode === "light" ? `text-${colors.light.text} hover:text-blue-700 float-left` : `text-${colors.dark.text} hover:text-blue-400 float-left`; return ( { props.checkboxHandler(e.target.checked, props.idx); }} /> { props.sliderHandler(val / 100, props.idx); }} > ); }