11import LoadingIcon from "@/src/components/shared/loading/LoadingIcon"
22import { selectUser } from "@/src/reduxStore/states/general"
3- import { removeFromRlaById , selectDisplayUserRole , selectHoverGroupDict , selectRecordRequests , selectRecordRequestsRecord , selectSettings , selectTmpHighlightIds , selectTokenLookupSelected , selectUserDisplayId , setHoverGroupDict , setTokenLookupSelected , tmpAddHighlightIds } from "@/src/reduxStore/states/pages/labeling"
3+ import { removeFromRlaById , selectDisplayUserRole , selectHoverGroupDict , selectRecordRequests , selectRecordRequestsRecord , selectSettings , selectTmpHighlightIds , selectUserDisplayId , setActiveTokenSelection , tmpAddHighlightIds } from "@/src/reduxStore/states/pages/labeling"
44import { selectLabelingTasksAll , selectVisibleAttributesLabeling } from "@/src/reduxStore/states/pages/settings"
55import { selectProjectId } from "@/src/reduxStore/states/project"
66import { HotkeyLookup , LabelSourceHover , LabelingVars , TokenLookup } from "@/src/types/components/projects/projectId/labeling/labeling"
@@ -27,6 +27,7 @@ import { filterRlaDataForUser } from "@/src/util/components/projects/projectId/l
2727import { LabelingPageParts } from "@/src/types/components/projects/projectId/labeling/labeling-main-component"
2828import style from '@/src/styles/components/projects/projectId/labeling.module.css' ;
2929import { useConsoleLog } from "@/submodules/react-components/hooks/useConsoleLog"
30+ import { getStoreSnapshotValue } from "@/src/reduxStore/store"
3031
3132const L_VARS = getDefaultLabelingVars ( ) ;
3233
@@ -60,7 +61,6 @@ export default function LabelingSuiteLabeling() {
6061 const [ activeTasks , setActiveTasks ] = useState < any [ ] > ( null ) ;
6162 const [ position , setPosition ] = useState ( { top : 0 , left : 0 } ) ;
6263 const [ labelHotkeys , setLabelHotkeys ] = useState < HotkeyLookup > ( { } ) ;
63- const [ saveTokenData , setSaveTokenData ] = useState < any > ( null ) ;
6464
6565 const extractionRef = useRef ( null ) ;
6666
@@ -100,10 +100,7 @@ export default function LabelingSuiteLabeling() {
100100
101101 useEffect ( ( ) => {
102102 const handleMouseDown = ( event ) => {
103- console . log ( "closing the box?" )
104- if ( ! event . target . closest ( '.label-selection-box' ) ) {
105- setActiveTasksFunc ( [ ] ) ;
106- }
103+ setActiveTasksFuncRef . current ( [ ] ) ;
107104 } ;
108105 window . addEventListener ( 'mousedown' , handleMouseDown ) ;
109106 return ( ) => {
@@ -123,8 +120,7 @@ export default function LabelingSuiteLabeling() {
123120 if ( ! tokenLookup || ! extractionRef . current ) return ;
124121 const handleMouseUp = ( e ) => {
125122 const [ check , attributeIdStart , tokenStart , tokenEnd , startEl ] = parseSelectionData ( ) ;
126- setSaveTokenData ( { attributeIdStart, tokenStart, tokenEnd, startEl } ) ;
127-
123+ dispatch ( setActiveTokenSelection ( { attributeId : attributeIdStart , tokenStart, tokenEnd } ) ) ;
128124 if ( ! check ) {
129125 clearSelected ( ) ;
130126 } else {
@@ -240,7 +236,7 @@ export default function LabelingSuiteLabeling() {
240236 for ( const key in lVars . taskLookup ) {
241237 const found = lVars . taskLookup [ key ] . lookup . filter ( t => activeTaskIds . includes ( t . task . id ) ) ;
242238 if ( found . length != 0 ) {
243- setActiveTasksFunc ( found ) ;
239+ setActiveTasksFuncRef . current ( found ) ;
244240 break ;
245241 }
246242 }
@@ -257,7 +253,7 @@ export default function LabelingSuiteLabeling() {
257253 setLabelHotkeys ( labelHotkeysCopy ) ;
258254 }
259255
260- function setActiveTasksFunc ( tasks : any | any [ ] ) {
256+ const setActiveTasksFunc = useCallback ( ( tasks : any | any [ ] ) => {
261257 if ( ! canEditLabels && user ?. role != UserRole . ANNOTATOR && userDisplayRole != UserRole . ANNOTATOR ) {
262258 if ( activeTasks ) setActiveTasks ( [ ] ) ;
263259 return ;
@@ -268,7 +264,12 @@ export default function LabelingSuiteLabeling() {
268264 setActiveTasks ( [ tasks ] ) ;
269265 }
270266 checkLabelVisibleInSearch ( labelLookup ) ;
271- }
267+ } , [ canEditLabels , user , activeTasks , userDisplayRole , labelLookup ] ) ;
268+
269+ const setActiveTasksFuncRef = useRef ( null ) ;
270+ useEffect ( ( ) => { setActiveTasksFuncRef . current = setActiveTasksFunc } , [ setActiveTasksFunc ] ) ;
271+
272+
272273
273274 function toggleGoldStar ( taskId : string , currentState : boolean ) {
274275 if ( currentState ) {
@@ -444,15 +445,15 @@ export default function LabelingSuiteLabeling() {
444445 }
445446 }
446447
447- function addRla ( task : any , labelId : string , tokenLookupCopy ?: TokenLookup ) {
448+ function addRla ( task : any , labelId : string ) {
448449 if ( ! canEditLabels ) return ;
449450 if ( task . taskType == LabelingTaskTaskType . MULTICLASS_CLASSIFICATION ) {
450451 addLabelToTask ( task . id , labelId ) ;
451452 } else {
452- addLabelToSelection ( task . attribute . id , task . id , labelId , tokenLookupCopy ) ;
453+ addLabelToSelection ( task . attribute . id , task . id , labelId ) ;
453454 }
454455 if ( settings . labeling . closeLabelBoxAfterLabel ) {
455- setActiveTasksFunc ( [ ] ) ;
456+ setActiveTasksFuncRef . current ( [ ] ) ;
456457 clearSelected ( ) ;
457458 }
458459 }
@@ -482,25 +483,17 @@ export default function LabelingSuiteLabeling() {
482483 } ) ;
483484 }
484485
485- function addLabelToSelection ( attributeId : string , labelingTaskId : string , labelId : string , tokenLookupCopy ?: TokenLookup ) {
486- const tokenLookupFinal = tokenLookupCopy ? tokenLookupCopy : tokenLookup ;
487- const selectionData = collectSelectionData ( attributeId , tokenLookupFinal , attributes , recordRequests ) ;
486+ function addLabelToSelection ( attributeId : string , labelingTaskId : string , labelId : string ) {
487+
488+ const currentSelection = getStoreSnapshotValue ( [ 'labeling' , 'activeTokenSelection' ] ) ;
489+ const selectionData = collectSelectionData ( attributeId , currentSelection , attributes , recordRequests ) ;
488490 if ( ! selectionData ) return ;
489491 const sourceId = SessionManager . getSourceId ( ) ;
490492 addExtractionLabelToRecordMut ( { variables : { projectId : projectId , recordId : record . id , labelingTaskId : labelingTaskId , labelId : labelId , tokenStartIndex : selectionData . startIdx , tokenEndIndex : selectionData . endIdx , value : selectionData . value , sourceId : sourceId } } ) . then ( ( res ) => { } ) ;
491493 }
492494
493495 const clearSelected = useCallback ( ( ) => {
494- console . log ( "clearSelected called" )
495- // const tokenLookupCopy = jsonCopy(tokenLookup);
496- // for (const attributeId in tokenLookupCopy) {
497- // if (!tokenLookupCopy[attributeId].token) continue;
498- // for (const token of tokenLookupCopy[attributeId].token) {
499- // if ('selected' in token) delete token.selected;
500- // }
501- // }
502- // setTokenLookup(tokenLookupCopy);
503- dispatch ( setTokenLookupSelected ( null ) ) ;
496+ dispatch ( setActiveTokenSelection ( null ) ) ;
504497 } , [ tokenLookup ] ) ;
505498
506499 function setSelected ( attributeId : string , tokenStart : number , tokenEnd : number , e ?: any ) {
@@ -510,14 +503,12 @@ export default function LabelingSuiteLabeling() {
510503 labelBoxPosition ( e ) ;
511504 return ;
512505 }
513- // for (const token of tokenLookupCopy[attributeId]?.token) {
514- // token.selected = token.idx >= tokenStart && token.idx <= tokenEnd;
515- // }
506+ dispatch ( setActiveTokenSelection ( { attributeId, tokenStart, tokenEnd } ) ) ;
516507 if ( lVars . taskLookup [ attributeId ] . lookup [ 0 ] . task . taskType == LabelingTaskTaskType . INFORMATION_EXTRACTION ) {
517508 const extractionTasks = lVars . taskLookup [ attributeId ] . lookup . filter ( t => t . task . taskType == LabelingTaskTaskType . INFORMATION_EXTRACTION ) ;
518- setActiveTasksFunc ( extractionTasks ) ;
509+ setActiveTasksFuncRef . current ( extractionTasks ) ;
519510 } else {
520- setActiveTasksFunc ( lVars . taskLookup [ attributeId ] . lookup ) ;
511+ setActiveTasksFuncRef . current ( lVars . taskLookup [ attributeId ] . lookup ) ;
521512 }
522513 setTokenLookup ( tokenLookupCopy ) ;
523514 labelBoxPosition ( e ) ;
@@ -542,7 +533,7 @@ export default function LabelingSuiteLabeling() {
542533 function handleKeyboardEvent ( event ) {
543534 const labelSelection = document . getElementById ( 'label-selection-box' ) ;
544535 if ( event . key == 'ArrowRight' || event . key == 'ArrowLeft' ) {
545- setActiveTasksFunc ( [ ] ) ;
536+ setActiveTasksFuncRef . current ( [ ] ) ;
546537 }
547538 if ( labelSelection && ! labelSelection . classList . contains ( 'hidden' ) ) return ;
548539 for ( const key in labelHotkeys ) {
@@ -582,7 +573,7 @@ export default function LabelingSuiteLabeling() {
582573 { task . task . taskType == LabelingTaskTaskType . INFORMATION_EXTRACTION ? ( < ExtractionDisplay ref = { extractionRef } attributeId = { attribute . id } tokenLookup = { tokenLookup } labelLookup = { labelLookup }
583574 deleteRla = { ( rlaId ) => {
584575 deleteRecordLabelAssociation ( rlaId ) ;
585- setActiveTasksFunc ( [ ] ) ;
576+ setActiveTasksFuncRef . current ( [ ] ) ;
586577 } }
587578 setSelected = { ( start , end , e ) => setSelected ( attribute . id , start , end , e ) } /> ) : ( < >
588579 { ( recordRequests . record . data [ lVars . taskLookup [ attribute . id ] . attribute . name ] != null && recordRequests . record . data [ lVars . taskLookup [ attribute . id ] . attribute . name ] !== '' ) ?
@@ -605,7 +596,7 @@ export default function LabelingSuiteLabeling() {
605596 </ div > ) ) }
606597 < Tooltip content = { TOOLTIPS_DICT . LABELING . CHOOSE_LABELS } color = "invert" placement = "top" >
607598 < button onClick = { ( event ) => {
608- setActiveTasksFunc ( task ) ;
599+ setActiveTasksFuncRef . current ( task ) ;
609600 labelBoxPosition ( event ) ;
610601 } } className = "flex flex-row flex-nowrap bg-white text-gray-700 text-sm font-medium mr-3 px-2 py-0.5 rounded-md border border-gray-300 hover:bg-gray-50 focus:outline-none" >
611602 < span > other</ span >
@@ -647,23 +638,17 @@ export default function LabelingSuiteLabeling() {
647638 </ div > }
648639 </ > }
649640 </ div >
650- { activeTasks && activeTasks . length > 0 ? (
641+ < div className = { activeTasks && activeTasks . length > 0 ? null : "hidden" } >
651642 < LabelSelectionBox activeTasks = { activeTasks } position = { position } labelLookup = { labelLookup } labelAddButtonDisabledDict = { labelAddButtonDisabledDict }
652643 clearSelected = { clearSelected }
653644 addRla = { ( task , labelId ) => {
654- // const tokenLookupCopy = jsonCopy(tokenLookup);
655- // if (saveTokenData && tokenLookupCopy[saveTokenData.attributeIdStart]) {
656- // for (const token of tokenLookupCopy[saveTokenData.attributeIdStart]?.token) {
657- // token.selected = token.idx >= saveTokenData.tokenStart && token.idx <= saveTokenData.tokenEnd;
658- // }
659- // }
660- addRla ( task , labelId , tokenLookup ) ;
645+ addRla ( task , labelId ) ;
661646 } }
662647 addNewLabelToTask = { ( newLabel , task ) => addNewLabelToTask ( newLabel , task ) }
663648 checkLabelVisibleInSearch = { ( newLabel , task ) => checkLabelVisibleInSearch ( labelLookup , newLabel , task ) }
664649 labelHotkeys = { labelHotkeys }
665- /> )
666- : null }
650+ />
651+ </ div >
667652 </ div > }
668653 </ Fragment > ) ) }
669654 </ Fragment > ) ) }
0 commit comments