import React, { useState } from 'react';
import { useMedia } from 'react-use';
import { Rating } from '@100mslive/types-prebuilt/elements/feedback';
import { useHMSActions } from '@100mslive/react-sdk';
import { CheckIcon, CrossIcon } from '@100mslive/react-icons';
import { Button } from '../../../Button';
import { Checkbox } from '../../../Checkbox';
import { Label } from '../../../Label';
import { Flex } from '../../../Layout';
import { Dialog } from '../../../Modal';
import { Sheet } from '../../../Sheet';
import { Text } from '../../../Text';
import { TextArea } from '../../../TextArea';
import { config as cssConfig } from '../../../Theme';
import { useHMSPrebuiltContext } from '../../AppContext';
import { useRoomLayoutLeaveScreen } from '../../provider/roomLayoutProvider/hooks/useRoomLayoutScreen';
export const FEEBACK_INDEX = {
THANK_YOU: -10,
INIT: -1,
};
export const FeedbackModal = ({
ratings,
index,
setIndex,
}: {
ratings: Rating[];
index: number;
setIndex: (index: number) => void;
}) => {
const isMobile = useMedia(cssConfig.media.md);
const onOpenChange = () => {
setIndex(FEEBACK_INDEX.INIT);
};
const avoidDefaultDomBehavior = (e: Event) => {
e.preventDefault();
};
if (isMobile) {
return (
);
}
return (
);
};
export const FeedbackContent = ({
ratings,
indexSelected,
setIndex,
}: {
ratings: Rating[];
indexSelected: number;
setIndex: (index: number) => void;
}) => {
const { feedback } = useRoomLayoutLeaveScreen();
const { endpoints } = useHMSPrebuiltContext();
const isMobile = useMedia(cssConfig.media.md);
const hmsActions = useHMSActions();
const [comment, setComment] = useState('');
const [selectedReasons, setSelectedReasons] = useState(new Set());
const handleCheckedChange = (checked: boolean | string, index: number) => {
const newSelected = new Set(selectedReasons);
if (checked) {
newSelected.add(index);
} else {
newSelected.delete(index);
}
setSelectedReasons(newSelected);
};
const submitFeedback = async () => {
if (indexSelected < 0 || ratings.length <= indexSelected) {
return;
}
try {
const reasons = [...selectedReasons].map((value: number) => ratings[indexSelected]?.reasons?.[value] || '');
await hmsActions.submitSessionFeedback(
{
question: `${feedback?.title} | ${ratings[indexSelected].question || ''}`,
rating: ratings[indexSelected].value || 1,
min_rating: 1,
max_rating: ratings.length,
reasons: selectedReasons.size === 0 ? [] : reasons,
comment: comment,
},
endpoints?.event,
);
} catch (e) {
console.error(e);
}
// always submit and take it to thankyou page
setIndex(FEEBACK_INDEX.THANK_YOU);
};
return (
{
setSelectedReasons(new Set());
setIndex(value);
}}
/>
);
};
export const FeedbackHeader = ({
onEmojiClicked,
ratings,
indexSelected = FEEBACK_INDEX.INIT,
}: {
onEmojiClicked: (index: number) => void;
ratings: Rating[];
indexSelected?: number;
}) => {
const isMobile = useMedia(cssConfig.media.md);
const { feedback } = useRoomLayoutLeaveScreen();
return (
<>
{feedback?.title || 'How was your experience?'}
{feedback?.sub_title || 'Your answers help us improve the quality.'}
{indexSelected !== FEEBACK_INDEX.INIT ? (
onEmojiClicked(FEEBACK_INDEX.INIT)} />
) : null}
{ratings.map((element, index) => {
return (
onEmojiClicked(index)}
key={`${index}`}
>
{element.emoji}
{element.label}
);
})}
>
);
};
export const FeedbackForm = ({
rating,
comment,
setComment,
selectedReasons,
handleCheckedChange,
}: {
rating: Rating;
comment: string;
setComment: (value: string) => void;
selectedReasons: Set;
handleCheckedChange: (checked: string | boolean, index: number) => void;
}) => {
const { feedback } = useRoomLayoutLeaveScreen();
return (
<>
{rating.reasons && rating.reasons.length > 0 && (
{rating.question || 'What do you like/dislike here?'}
{rating.reasons.map((option: string, index: number) => {
return (
handleCheckedChange(checked, index)}
css={{
cursor: 'pointer',
flexShrink: 0,
bg: '$on_secondary_low',
border: '1px solid $border_bright',
}}
>
);
})}
)}
{feedback?.comment && (
{feedback?.comment.label || 'Additional comments (optional)'}
)}
>
);
};