import React, { useMemo } from 'react'; import { useDispatch } from 'react-redux'; import IconEdit from 'components/Icons/IconEdit'; import { IconCode, IconDeviceFloppy } from '@tabler/icons'; import StyledWrapper from './StyledWrapper'; import { useTheme } from 'providers/Theme'; import TruncatedText from 'components/TruncatedText'; import { updateResponseExampleName, updateResponseExampleDescription } from 'providers/ReduxStore/slices/collections'; import get from 'lodash/get'; import Button from 'ui/Button'; const ResponseExampleTopBar = ({ item, collection, exampleUid, editMode, onEditToggle, onSave, onCancel, onGenerateCode }) => { const { theme } = useTheme(); const dispatch = useDispatch(); const example = useMemo(() => { return item.draft ? get(item, 'draft.examples', []).find((e) => e.uid === exampleUid) : get(item, 'examples', []).find((e) => e.uid === exampleUid); }, [item.draft, item.examples, item, exampleUid]); const handleGenerateCode = () => { if (onGenerateCode) { onGenerateCode({ ...example, isExample: true, exampleUid: exampleUid }); } }; const handleNameChange = (e) => { // Validate required fields before dispatching if (!item?.uid) { console.error('item.uid is missing'); return; } if (!collection?.uid) { console.error('collection.uid is missing'); return; } if (!exampleUid) { console.error('exampleUid is missing'); return; } dispatch(updateResponseExampleName({ itemUid: item.uid, collectionUid: collection.uid, exampleUid: exampleUid, name: e.target.value })); }; const handleDescriptionChange = (e) => { // Validate required fields before dispatching if (!item?.uid) { console.error('item.uid is missing'); return; } if (!collection?.uid) { console.error('collection.uid is missing'); return; } if (!exampleUid) { console.error('exampleUid is missing'); return; } dispatch(updateResponseExampleDescription({ itemUid: item.uid, collectionUid: collection.uid, exampleUid: exampleUid, description: e.target.value })); }; const handleSave = () => { // Call the parent save handler if (onSave) { onSave(); } }; const handleCancel = () => { if (onCancel) { onCancel(); } }; if (!example || !exampleUid) { return null; } if (editMode) { return (