-
-
Notifications
You must be signed in to change notification settings - Fork 6
TT-7334 feat: add Prompt tool functionality and components #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../public/localization/strings2ecd8049.json → .../public/localization/stringsa06687f1.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/renderer/src/components/PassageDetail/Prompt/PassageDetailPrompt.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { useRole } from '../../../crud/useRole'; | ||
| import { useStepPermissions } from '../../../utils/useStepPermission'; | ||
| import usePassageDetailContext from '../../../context/usePassageDetailContext'; | ||
| import PassageDetailPromptAdmin from './PassageDetailPromptAdmin'; | ||
| import PassageDetailPromptMember from './PassageDetailPromptMember'; | ||
|
|
||
| interface IProps { | ||
| width: number; | ||
| } | ||
|
|
||
| export default function PassageDetailPrompt(props: IProps) { | ||
| const { userIsAdmin } = useRole(); | ||
| const { canDoSectionStep, permissionsOn } = useStepPermissions(); | ||
| const { section, currentstep } = usePassageDetailContext(); | ||
|
|
||
| const showAdmin = | ||
| userIsAdmin || (permissionsOn && canDoSectionStep(currentstep, section)); | ||
|
|
||
| if (showAdmin) { | ||
| return <PassageDetailPromptAdmin width={props.width} />; | ||
| } | ||
| return <PassageDetailPromptMember width={props.width} />; | ||
| } | ||
195 changes: 195 additions & 0 deletions
195
src/renderer/src/components/PassageDetail/Prompt/PassageDetailPromptAdmin.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import { useContext, useEffect, useMemo, useRef, useState } from 'react'; | ||
| import { Stack } from '@mui/material'; | ||
| import { shallowEqual, useSelector } from 'react-redux'; | ||
| import { | ||
| ArtifactTypeSlug, | ||
| IMediaState, | ||
| MediaSt, | ||
| remoteIdGuid, | ||
| useArtifactType, | ||
| } from '../../../crud'; | ||
| import { ReplaceRelatedRecord } from '../../../model/baseModel'; | ||
| import { | ||
| RecordIdentity, | ||
| RecordKeyMap, | ||
| RecordTransformBuilder, | ||
| } from '@orbit/records'; | ||
| import { useGlobal } from '../../../context/useGlobal'; | ||
| import usePassageDetailContext from '../../../context/usePassageDetailContext'; | ||
| import { UnsavedContext } from '../../../context/UnsavedContext'; | ||
| import MediaRecord from '../../MediaRecord'; | ||
| import { useStepPermissions } from '../../../utils/useStepPermission'; | ||
| import { useSecResCreate, useSecResDelete } from '../../../crud'; | ||
| import { usePromptSectionResource } from './usePromptSectionResource'; | ||
| import { SectionResourceD } from '../../../model'; | ||
| import { sharedSelector } from '../../../selector'; | ||
| import { ISharedStrings } from '../../../model'; | ||
|
|
||
| interface IProps { | ||
| width: number; | ||
| } | ||
|
|
||
| const toolId = 'PromptTool'; | ||
|
|
||
| export default function PassageDetailPromptAdmin(props: IProps) { | ||
| const { width } = props; | ||
| const [memory] = useGlobal('memory'); | ||
| const ts: ISharedStrings = useSelector(sharedSelector, shallowEqual); | ||
| const { getTypeId } = useArtifactType(); | ||
| const { rowData, section, currentstep, forceRefresh, setRecording } = | ||
| usePassageDetailContext(); | ||
| const { canDoSectionStep, canAlwaysDoStep } = useStepPermissions(); | ||
| const { promptMediaId, sectionResource, hasPrompt } = | ||
| usePromptSectionResource(rowData, section, currentstep); | ||
| const { AddSectionResource } = useSecResCreate(section, currentstep); | ||
| const DeleteSectionResource = useSecResDelete(); | ||
| const { | ||
| startSave, | ||
| toolChanged, | ||
| toolsChanged, | ||
| clearRequested, | ||
| clearCompleted, | ||
| } = useContext(UnsavedContext).state; | ||
| const [, setBigBusy] = useGlobal('importexportBusy'); | ||
| const [canSave, setCanSave] = useState(false); | ||
| const [statusText, setStatusText] = useState(''); | ||
| const [preload, setPreload] = useState(0); | ||
| const [recorderState, setRecorderState] = useState<IMediaState>(); | ||
| const sectionResourceRef = useRef<SectionResourceD | null>(null); | ||
| /** Prevents preload ↔ trackState update loops; reset when promptMediaId changes. */ | ||
| const recordPreloadInitiatedRef = useRef<string | null>(null); | ||
|
|
||
| const resourceArtifactId = useMemo( | ||
| () => getTypeId(ArtifactTypeSlug.Resource) || '', | ||
| [getTypeId] | ||
| ); | ||
|
|
||
| const canEdit = canAlwaysDoStep() || canDoSectionStep(currentstep, section); | ||
|
|
||
| useEffect(() => { | ||
| sectionResourceRef.current = sectionResource; | ||
| }, [sectionResource]); | ||
|
|
||
| useEffect(() => { | ||
| toolChanged(toolId, canSave); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [canSave]); | ||
|
|
||
| useEffect(() => { | ||
| if (clearRequested(toolId)) { | ||
| const sr = sectionResourceRef.current; | ||
| if (sr) { | ||
| void DeleteSectionResource(sr).then(() => { | ||
| forceRefresh(); | ||
| clearCompleted(toolId); | ||
| }); | ||
| } else { | ||
| clearCompleted(toolId); | ||
| } | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [toolsChanged]); | ||
|
|
||
| useEffect(() => { | ||
| recordPreloadInitiatedRef.current = null; | ||
| }, [promptMediaId]); | ||
|
|
||
| useEffect(() => { | ||
| const hasExisting = | ||
| Boolean(promptMediaId) && | ||
| recorderState?.status === MediaSt.FETCHED && | ||
| recorderState?.id === promptMediaId; | ||
| const shouldAutoPreload = | ||
| hasExisting && recordPreloadInitiatedRef.current !== promptMediaId; | ||
| if (shouldAutoPreload && promptMediaId) { | ||
| recordPreloadInitiatedRef.current = promptMediaId; | ||
| setPreload((p) => p + 1); | ||
| } | ||
| }, [promptMediaId, recorderState]); | ||
|
|
||
| const linkMediaToStep = async (mediaId: string) => { | ||
| const mediaRecId = { type: 'mediafile', id: mediaId } as RecordIdentity; | ||
| const t = new RecordTransformBuilder(); | ||
| await memory.update([ | ||
| ...ReplaceRelatedRecord( | ||
| t, | ||
| mediaRecId, | ||
| 'orgWorkflowStep', | ||
| 'orgworkflowstep', | ||
| currentstep | ||
| ), | ||
| ]); | ||
| }; | ||
|
|
||
| const afterUploadCb = async (mediaId: string | undefined) => { | ||
| if (!mediaId) { | ||
| setStatusText(ts.NoSaveWoMedia); | ||
| return; | ||
| } | ||
| setStatusText(''); | ||
| const existing = sectionResourceRef.current; | ||
| if (existing) { | ||
| await DeleteSectionResource(existing); | ||
| sectionResourceRef.current = null; | ||
| } | ||
| const mediaRecId = { | ||
| type: 'mediafile', | ||
| id: | ||
| remoteIdGuid('mediafile', mediaId, memory?.keyMap as RecordKeyMap) || | ||
| mediaId, | ||
| } as RecordIdentity; | ||
| await linkMediaToStep(mediaRecId.id); | ||
| await AddSectionResource(0, null, mediaRecId); | ||
| forceRefresh(); | ||
| setPreload((p) => p + 1); | ||
| }; | ||
|
|
||
| const handleTrackRecorder = (state: IMediaState) => setRecorderState(state); | ||
|
|
||
| const handleRecording = (recording: boolean) => { | ||
| setRecording(recording); | ||
| }; | ||
|
|
||
| const handleSave = () => { | ||
| startSave(toolId); | ||
| }; | ||
|
|
||
| const onSaving = () => { | ||
| setBigBusy(true); | ||
| }; | ||
|
|
||
| const onReady = () => { | ||
| setBigBusy(false); | ||
| }; | ||
|
|
||
| const defaultFilename = useMemo(() => `prompt-${section.id}`, [section.id]); | ||
|
|
||
| return ( | ||
| <Stack sx={{ width, maxWidth: width, minWidth: 0 }}> | ||
| <MediaRecord | ||
| toolId={toolId} | ||
| artifactId={resourceArtifactId} | ||
| passageId={undefined} | ||
| afterUploadCb={afterUploadCb} | ||
| mediaId={promptMediaId} | ||
| onSaving={onSaving} | ||
| onReady={onReady} | ||
| onRecording={handleRecording} | ||
| defaultFilename={defaultFilename} | ||
| allowRecord={canEdit} | ||
| allowZoom={true} | ||
| allowWave={true} | ||
| preload={preload} | ||
| trackState={handleTrackRecorder} | ||
| setCanSave={setCanSave} | ||
| setStatusText={setStatusText} | ||
| handleSave={handleSave} | ||
| isSaveDisabled={!canEdit} | ||
| height={280} | ||
| width={width} | ||
| forceMobileView={true} | ||
| metaData={hasPrompt ? undefined : <span>{statusText}</span>} | ||
| /> | ||
| </Stack> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.