import { Tag } from 'antd'; import { List } from 'immutable'; import { Duration } from 'luxon'; import React from 'react'; import { connect } from 'react-redux'; import { toast } from 'react-toastify'; import { useStore } from 'App/mstore'; import { Note, TAGS, WriteNote, iTag, tagProps, } from 'App/services/NotesService'; import { fetchList as fetchSlack } from 'Duck/integrations/slack'; import { fetchList as fetchTeams } from 'Duck/integrations/teams'; import { addNote, updateNote } from 'Duck/sessions'; import { Button, Checkbox, Icon } from 'UI'; import Select from 'Shared/Select'; interface Props { time: number; addNote: (note: Note) => void; updateNote: (note: Note) => void; sessionId: string; isEdit?: boolean; editNote?: WriteNote; slackChannels: List>; teamsChannels: List>; fetchSlack: () => void; fetchTeams: () => void; hideModal: () => void; } function CreateNote({ time, sessionId, isEdit, editNote, updateNote, slackChannels, fetchSlack, teamsChannels, fetchTeams, hideModal, }: Props) { const [text, setText] = React.useState(''); const [slackChannel, setSlackChannel] = React.useState(''); const [teamsChannel, setTeamsChannel] = React.useState(''); const [isPublic, setPublic] = React.useState(false); const [tag, setTag] = React.useState(TAGS[0]); const [useTimestamp, setUseTs] = React.useState(true); const [useSlack, setSlack] = React.useState(false); const [useTeams, setTeams] = React.useState(false); const inputRef = React.createRef(); const { notesStore } = useStore(); React.useEffect(() => { if (isEdit && editNote) { setTag(editNote.tag); setText(editNote.message); setPublic(editNote.isPublic); if (editNote.timestamp > 0) { setUseTs(true); } } }, [isEdit]); React.useEffect(() => { if (inputRef.current) { inputRef.current.focus(); fetchSlack(); fetchTeams(); } }, []); const duration = Duration.fromMillis(time || 0).toFormat('mm:ss'); const cleanUp = () => { setText(''); setTag(TAGS[0]); hideModal(); }; const onSubmit = () => { if (text === '') return; const note: WriteNote = { message: text, tag, timestamp: useTimestamp ? Math.floor(isEdit && editNote ? editNote.timestamp : time) : -1, isPublic, }; const onSuccess = (noteId: string) => { if (slackChannel) { notesStore.sendSlackNotification(noteId, slackChannel); } if (teamsChannel) { notesStore.sendMsTeamsNotification(noteId, teamsChannel); } }; if (isEdit && editNote) { return notesStore .updateNote(editNote.noteId!, note) .then((r) => { toast.success('Note updated'); notesStore.fetchSessionNotes(sessionId).then(() => { onSuccess(editNote.noteId!); updateNote(r as Note); }); }) .catch((e) => { toast.error('Error updating note'); console.error(e); }) .finally(() => { cleanUp(); }); } else { return notesStore .addNote(sessionId, note) .then((r) => { onSuccess(r!.noteId as unknown as string); toast.success('Note added'); }) .catch((e) => { toast.error('Error adding note'); console.error(e); }) .finally(() => { cleanUp(); }); } }; const closeTooltip = () => { hideModal(); }; const tagActive = (noteTag: iTag) => tag === noteTag; const addTag = (tag: iTag) => { setTag(tag); }; const slackChannelsOptions = slackChannels .map(({ webhookId, name }) => ({ value: webhookId, label: name, })) .toJS() as unknown as { value: string; label: string }[]; const teamsChannelsOptions = teamsChannels .map(({ webhookId, name }) => ({ value: webhookId, label: name, })) .toJS() as unknown as { value: string; label: string }[]; slackChannelsOptions.unshift({ // @ts-ignore value: null, // @ts-ignore label:
Pick a channel
, disabled: true, }); teamsChannelsOptions.unshift({ // @ts-ignore value: null, // @ts-ignore label:
Pick a channel
, disabled: true, }); const changeSlackChannel = ({ value, }: { value: Record; name: string; }) => { if (value) { setSlackChannel(value.value); } }; const changeTeamsChannel = ({ value, }: { value: Record; name: string; }) => { if (value) { setTeamsChannel(value.value); } }; return (
e.stopPropagation()} >

{isEdit ? 'Edit Note' : 'Add Note'}

setUseTs(!useTimestamp)} > Add note at current time frame
{duration}
Note