-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: auto assignments * chore: reduce minScore in auto-assignment * chore: prepare set automatic assignments action * chore: update nmr-processing to 7.4.2 * feat: auto assignments * chore: update nmr-processing * refactor: assignments tool (#1470) * refactor: assignments tool * fix: import assignments types * refactor: assignments context * fix: y axis assignments * fix: 2d assignments * fix: leaving a zone is not always detected Co-authored-by: jobo322 <jose.bolanos@correounivalle.edu.co>
- Loading branch information
1 parent
cd6d86e
commit 1024c2e
Showing
41 changed files
with
894 additions
and
656 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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,147 @@ | ||
import { createContext, useCallback, useContext, useMemo } from 'react'; | ||
|
||
import { AssignmentsActions } from './AssignmentsReducer'; | ||
|
||
export type Axis = 'x' | 'y'; | ||
export type Assignment = Record<Axis, string[]>; | ||
export type Assignments = Record<string, Assignment>; | ||
|
||
export type AssignmentDimension = '1D' | '2D'; | ||
export interface AssignmentAction { | ||
id: string; | ||
axis: Axis | null; | ||
} | ||
export interface AssignmentState { | ||
assignments: Assignments; | ||
activated: AssignmentAction | null; | ||
highlighted: AssignmentAction | null; | ||
} | ||
export interface AssignmentContext { | ||
data: AssignmentState; | ||
dispatch: (action: AssignmentsActions) => void; | ||
} | ||
|
||
export interface AssignmentsData | ||
extends Pick<AssignmentState, 'activated' | 'highlighted'> { | ||
id: string; | ||
isActive: boolean; | ||
isOver: boolean; | ||
assigned: Record<Axis, string[]>; | ||
removeAll: (axis: Axis) => void; | ||
toggle: (atomIDs: string[], dimension: AssignmentDimension) => void; | ||
setActive: (axis: Axis) => void; | ||
show: (axis?: Axis) => void; | ||
hide: () => void; | ||
} | ||
|
||
export const assignmentState: AssignmentState = { | ||
assignments: {}, | ||
activated: null, | ||
highlighted: null, | ||
}; | ||
|
||
export const assignmentContext = createContext<AssignmentContext>({ | ||
data: assignmentState, | ||
dispatch: () => null, | ||
}); | ||
|
||
export const AssignmentProvider = assignmentContext.Provider; | ||
|
||
export function useAssignmentData() { | ||
const context = useContext(assignmentContext); | ||
|
||
if (!context) { | ||
throw new Error('Assignment context was not found'); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
// key can be signal id,range id or zone id | ||
export function useAssignment(key): AssignmentsData { | ||
const { | ||
data: { activated, highlighted, assignments }, | ||
dispatch, | ||
} = useAssignmentData(); | ||
|
||
if ((typeof key !== 'string' && typeof key !== 'number') || key === '') { | ||
throw new Error(`assignment key must be a non-empty string or number`); | ||
} | ||
const id = String(key); | ||
|
||
const isActive = useMemo(() => { | ||
return activated?.id === id; | ||
}, [activated?.id, id]); | ||
const isOver = useMemo(() => { | ||
return highlighted?.id === id; | ||
}, [highlighted?.id, id]); | ||
|
||
const assigned = useMemo(() => { | ||
return assignments[id] || null; | ||
}, [assignments, id]); | ||
|
||
const removeAll = useCallback( | ||
(axis: Axis) => { | ||
dispatch({ | ||
type: 'REMOVE', | ||
payload: { ids: [id], axis }, | ||
}); | ||
}, | ||
[dispatch, id], | ||
); | ||
|
||
const toggle = useCallback( | ||
(atomIDs: string[], dimension: AssignmentDimension) => { | ||
dispatch({ | ||
type: 'TOGGLE', | ||
payload: { atomIDs, id, dimension }, | ||
}); | ||
}, | ||
[dispatch, id], | ||
); | ||
|
||
const setActive = useCallback( | ||
(axis: Axis) => { | ||
dispatch({ | ||
type: 'SET_ACTIVE', | ||
payload: { | ||
id, | ||
axis, | ||
}, | ||
}); | ||
}, | ||
[dispatch, id], | ||
); | ||
|
||
const show = useCallback( | ||
(axis?: Axis) => { | ||
dispatch({ | ||
type: 'SHOW', | ||
payload: { | ||
id, | ||
axis, | ||
}, | ||
}); | ||
}, | ||
[dispatch, id], | ||
); | ||
const hide = useCallback(() => { | ||
dispatch({ | ||
type: 'HIDE', | ||
}); | ||
}, [dispatch]); | ||
|
||
return { | ||
id, | ||
activated, | ||
isActive, | ||
isOver, | ||
highlighted, | ||
assigned, | ||
removeAll, | ||
toggle, | ||
setActive, | ||
show, | ||
hide, | ||
}; | ||
} |
Oops, something went wrong.