Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 119 additions & 119 deletions src/renderer/src/components/Discussions/CommentEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Box,
Button,
TextField,
Tooltip,
Expand All @@ -23,11 +24,15 @@ const RowDiv = styled('div')(() => ({
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
flexWrap: 'wrap',
minWidth: 0,
}));

const ColumnDiv = styled('div')(() => ({
display: 'flex',
flexDirection: 'column',
minWidth: 0,
width: '100%',
}));

const StatusMessage = styled(Typography)<TypographyProps>(({ theme }) => ({
Expand All @@ -49,6 +54,7 @@ interface IProps extends IStateProps {
onCancel?: () => void;
setCanSaveRecording: (canSave: boolean) => void;
onTextChange: (txt: string) => void;
onAudioDraftChange?: (hasDraft: boolean) => void;
}
export const CommentEditor = (props: IProps) => {
const {
Expand All @@ -63,6 +69,7 @@ export const CommentEditor = (props: IProps) => {
onCancel,
setCanSaveRecording,
onTextChange,
onAudioDraftChange,
} = props;
const {
playing,
Expand All @@ -85,6 +92,7 @@ export const CommentEditor = (props: IProps) => {
const doRecordRef = useRef(false);
const [recording, setRecording] = useState(false);
const [myChanged, setMyChanged] = useState(false);
const [showRecorder, setShowRecorder] = useState(false);
const { commentId } = useArtifactType();

const {
Expand All @@ -97,6 +105,16 @@ export const CommentEditor = (props: IProps) => {
isChanged,
} = useContext(UnsavedContext).state;

const setAudioDraft = (hasDraft: boolean) => {
onAudioDraftChange?.(hasDraft);
};

const clearUnsavedIfEmpty = () => {
if (!curText.length && !canSaveRef.current) {
toolChanged(toolId, false);
}
};

useEffect(() => {
return () => {
if (doRecordRef.current) setCommentRecording(false);
Expand All @@ -123,11 +141,13 @@ export const CommentEditor = (props: IProps) => {
100
).then(() => {
doRecordRef.current = true;
setShowRecorder(true);
setStartRecord(false);
});
} catch {
//do it anyway...
doRecordRef.current = true;
setShowRecorder(true);
setStartRecord(false);
}
}, [startRecord, playing, itemPlaying, commentPlaying]);
Expand All @@ -137,11 +157,19 @@ export const CommentEditor = (props: IProps) => {
canSaveRef.current = valid;
setCanSave(valid);
setCanSaveRecording(valid);
setAudioDraft(valid);
if (valid) toolChanged(toolId, true);
else clearUnsavedIfEmpty();
}
};
const onRecording = (r: boolean) => {
setRecording(r);
if (r) {
toolChanged(toolId, true);
} else if (doRecordRef.current) {
// Paused/stopped: enable parent Add before blob-ready/canSave propagates (TT-7216).
setAudioDraft(true);
}
};
const handleTextChange = (e: any) => {
setCurText(e.target.value);
Expand All @@ -163,6 +191,7 @@ export const CommentEditor = (props: IProps) => {
};

const handleRecord = () => {
toolChanged(toolId, true);
setStartRecord(true);
setCommentRecording(true);
};
Expand All @@ -172,7 +201,13 @@ export const CommentEditor = (props: IProps) => {
setStatusText('');
setCurText('');
doRecordRef.current = false;
setShowRecorder(false);
canSaveRef.current = false;
setCanSave(false);
setCanSaveRecording(false);
setAudioDraft(false);
clearCompleted(toolId);
clearUnsavedIfEmpty();
};

useEffect(() => {
Expand All @@ -182,6 +217,81 @@ export const CommentEditor = (props: IProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [refresh]);

const recorderNode = (
<Box sx={{ width: '100%', minWidth: 0 }}>
<MediaRecord
toolId={toolId}
passageId={passageId}
artifactId={commentId}
onRecording={onRecording}
afterUploadCb={afterUploadCb}
defaultFilename={fileName}
allowWave={false}
setCanSave={handleSetCanSave}
setStatusText={setStatusText}
height={40}
width={400}
autoStart={true}
allowDeltaVoice={false}
allowNoNoise={false}
allowZoom={false}
keepItSmall={true}
oneTryOnly={false}
hideToolbar={true}
hideSegmentControls={true}
showSize={false}
/>
</Box>
);

const actionButtons =
onOk &&
(!cancelOnlyIfChanged || doRecordRef.current || myChanged) && (
<Box
sx={{
display: 'flex',
alignItems: 'flex-start',
gap: '1px',
flexWrap: 'wrap',
}}
>
<Tooltip title={ts.cancel}>
<span>
<Button
id="cancel"
onClick={handleCancel}
sx={{
color: 'background.paper',
minWidth: 'auto',
padding: '2px 4px',
}}
disabled={recording}
>
<CancelIcon />
</Button>
</span>
</Tooltip>
<Tooltip title={ts.save}>
<span>
<Button
id="ok"
onClick={handleOk}
sx={{
color: 'background.paper',
minWidth: 'auto',
padding: '2px 4px',
}}
disabled={
(!canSave && !curText.length) || !myChanged || recording
}
>
<SendIcon />
</Button>
</span>
</Tooltip>
</Box>
);

return (
<ColumnDiv id="commentedit">
<TextField
Expand All @@ -194,100 +304,20 @@ export const CommentEditor = (props: IProps) => {
label={t.comment}
focused
/>
{doRecordRef.current ? (
<>
{showRecorder ? (
<ColumnDiv style={{ gap: '8px', marginTop: '4px' }}>
{recorderNode}
<RowDiv
style={{
alignItems: 'flex-start',
justifyContent: 'flex-start',
alignItems: 'center',
justifyContent: 'space-between',
gap: '8px',
}}
>
<MediaRecord
toolId={toolId}
passageId={passageId}
artifactId={commentId}
onRecording={onRecording}
afterUploadCb={afterUploadCb}
defaultFilename={fileName}
allowWave={false}
setCanSave={handleSetCanSave}
setStatusText={setStatusText}
height={40}
width={250}
autoStart={true}
allowDeltaVoice={false}
allowNoNoise={false}
allowZoom={false}
keepItSmall={true}
oneTryOnly={false}
hideToolbar={true}
showSize={false}
/>
<div
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'flex-start',
gap: '4px',
flexShrink: 0,
}}
>
<div
style={{
display: 'flex',
alignItems: 'flex-start',
gap: '1px',
}}
>
{onOk &&
(!cancelOnlyIfChanged ||
doRecordRef.current ||
myChanged) && (
<Tooltip title={ts.cancel}>
<span>
<Button
id="cancel"
onClick={handleCancel}
sx={{
color: 'background.paper',
minWidth: 'auto',
padding: '2px 4px',
}}
disabled={recording}
>
<CancelIcon />
</Button>
</span>
</Tooltip>
)}
{onOk && (
<Tooltip title={ts.save}>
<span>
<Button
id="ok"
onClick={handleOk}
sx={{
color: 'background.paper',
minWidth: 'auto',
padding: '2px 4px',
}}
disabled={
(!canSave && !curText.length) ||
!myChanged ||
recording
}
>
<SendIcon />
</Button>
</span>
</Tooltip>
)}
</div>
<StatusMessage variant="caption">{statusText}</StatusMessage>
</div>
{actionButtons}
<StatusMessage variant="caption">{statusText}</StatusMessage>
</RowDiv>
</>
</ColumnDiv>
) : (
<RowDiv>
<Tooltip title={commentRecording ? t.recordUnavailable : t.record}>
Expand All @@ -303,37 +333,7 @@ export const CommentEditor = (props: IProps) => {
</Tooltip>
<div>
<StatusMessage variant="caption">{statusText}</StatusMessage>
{onOk &&
(!cancelOnlyIfChanged || doRecordRef.current || myChanged) && (
<Tooltip title={ts.cancel}>
<span>
<Button
id="cancel"
onClick={handleCancel}
sx={{ color: 'background.paper' }}
disabled={recording}
>
<CancelIcon />
</Button>
</span>
</Tooltip>
)}
{onOk && (
<Tooltip title={ts.save}>
<span>
<Button
id="ok"
onClick={handleOk}
sx={{ color: 'background.paper' }}
disabled={
(!canSave && !curText.length) || !myChanged || recording
}
>
<SendIcon />
</Button>
</span>
</Tooltip>
)}
{actionButtons}
</div>
</RowDiv>
)}
Expand Down
Loading
Loading