import React from 'react'; import { Icon, Button, Checkbox } from 'UI'; import { Duration } from 'luxon'; import { connect } from 'react-redux'; import { WriteNote, tagProps, TAGS, iTag, Note } from 'App/services/NotesService'; import { setCreateNoteTooltip, addNote, updateNote } from 'Duck/sessions'; import stl from './styles.module.css'; import { useStore } from 'App/mstore'; import { toast } from 'react-toastify'; import { fetchList as fetchSlack } from 'Duck/integrations/slack'; import { fetchList as fetchTeams } from 'Duck/integrations/teams'; import Select from 'Shared/Select'; import { TeamBadge } from 'Shared/SessionListContainer/components/Notes'; import { List } from 'immutable'; interface Props { isVisible: boolean; time: number; setCreateNoteTooltip: (state: any) => void; addNote: (note: Note) => void; updateNote: (note: Note) => void; sessionId: string; isEdit: string; editNote: WriteNote; slackChannels: List>; teamsChannels: List>; fetchSlack: () => void; fetchTeams: () => void; } function CreateNote({ isVisible, time, setCreateNoteTooltip, sessionId, addNote, isEdit, editNote, updateNote, slackChannels, fetchSlack, teamsChannels, fetchTeams, }: 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) { setTag(editNote.tag); setText(editNote.message); setPublic(editNote.isPublic); if (editNote.timestamp > 0) { setUseTs(true); } } }, [isEdit]); React.useEffect(() => { if (inputRef.current && isVisible) { inputRef.current.focus(); if (teamsChannels.size === 0 || slackChannels.size === 0) { fetchSlack(); fetchTeams(); } } }, [isVisible]); const duration = Duration.fromMillis(time).toFormat('mm:ss'); const cleanUp = () => { setCreateNoteTooltip({ isVisible: false, time: 0 }); setText(''); setTag(TAGS[0]); } const onSubmit = () => { if (text === '') return; const note: WriteNote = { message: text, tag, timestamp: useTimestamp ? (isEdit ? editNote.timestamp : time) : -1, isPublic, }; const onSuccess = (noteId: string) => { if (slackChannel) { notesStore.sendSlackNotification(noteId, slackChannel); } if (teamsChannel) { notesStore.sendMsTeamsNotification(noteId, teamsChannel); } }; if (isEdit) { 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() }); } return notesStore .addNote(sessionId, note) .then((r) => { onSuccess(r!.noteId as unknown as string); toast.success('Note added'); notesStore.fetchSessionNotes(sessionId).then(() => { addNote(r as Note); }); }) .catch((e) => { toast.error('Error adding note'); console.error(e); }) .finally(() => { cleanUp() }); }; const closeTooltip = () => { cleanUp() setCreateNoteTooltip({ isVisible: false, time: 100 }); }; 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 }[]; // @ts-ignore slackChannelsOptions.unshift({ value: null, label: 'Pick a channel' }); // @ts-ignore teamsChannelsOptions.unshift({ value: null, label: 'Pick a channel' }); const changeSlackChannel = ({ value }: { value: Record; name: string }) => { setSlackChannel(value.value); }; const changeTeamsChannel = ({ value }: { value: Record; name: string }) => { setTeamsChannel(value.value); }; return (
e.stopPropagation()} >

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

setUseTs(!useTimestamp)}> {`at ${duration}`}