Commit 5a9f32ee authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Optimize and DRY out map-related logic

- Use `createSelector` in a few places
- Add utility function for creating "true-filled" maps from arrays
- Rename `truthyKeys` => `trueKeys`
parent 0ae89144
...@@ -52,10 +52,23 @@ TAB_SORTKEYS_AVAILABLE[uiConstants.TAB_ORPHANS] = [ ...@@ -52,10 +52,23 @@ TAB_SORTKEYS_AVAILABLE[uiConstants.TAB_ORPHANS] = [
* @param {Object} obj * @param {Object} obj
* @return {string[]} * @return {string[]}
*/ */
function truthyKeys(obj) { function trueKeys(obj) {
return Object.keys(obj).filter(key => obj[key] === true); return Object.keys(obj).filter(key => obj[key] === true);
} }
/**
* Convert an array of strings into an object mapping each array entry (string)
* to `true`.
*
* @param {string[]} arr
* @return {Object<string,true>}
*/
function toTrueMap(arr) {
const obj = /** @type {Object<string,true>} */ ({});
arr.forEach(key => (obj[key] = true));
return obj;
}
function initialSelection(settings) { function initialSelection(settings) {
const selection = {}; const selection = {};
// TODO: Do not take into account existence of `settings.query` here // TODO: Do not take into account existence of `settings.query` here
...@@ -172,9 +185,7 @@ const update = { ...@@ -172,9 +185,7 @@ const update = {
}, },
FOCUS_ANNOTATIONS: function (state, action) { FOCUS_ANNOTATIONS: function (state, action) {
const focused = {}; return { focused: toTrueMap(action.focusedTags) };
action.focusedTags.forEach(tag => (focused[tag] = true));
return { focused };
}, },
SET_FOCUS_MODE_FOCUSED: function (state, action) { SET_FOCUS_MODE_FOCUSED: function (state, action) {
...@@ -287,11 +298,9 @@ const actions = util.actionTypes(update); ...@@ -287,11 +298,9 @@ const actions = util.actionTypes(update);
* @param {string[]} ids - Identifiers of annotations to select * @param {string[]} ids - Identifiers of annotations to select
*/ */
function selectAnnotations(ids) { function selectAnnotations(ids) {
const selection = {};
ids.forEach(id => (selection[id] = true));
return { return {
type: actions.SELECT_ANNOTATIONS, type: actions.SELECT_ANNOTATIONS,
selection, selection: toTrueMap(ids),
}; };
} }
...@@ -364,11 +373,9 @@ function setExpanded(id, expanded) { ...@@ -364,11 +373,9 @@ function setExpanded(id, expanded) {
* @param {string[]} ids - annotations to highlight * @param {string[]} ids - annotations to highlight
*/ */
function highlightAnnotations(ids) { function highlightAnnotations(ids) {
const highlighted = {};
ids.forEach(id => (highlighted[id] = true));
return { return {
type: actions.HIGHLIGHT_ANNOTATIONS, type: actions.HIGHLIGHT_ANNOTATIONS,
highlighted, highlighted: toTrueMap(ids),
}; };
} }
...@@ -421,13 +428,15 @@ function setSortKey(key) { ...@@ -421,13 +428,15 @@ function setSortKey(key) {
/* Selectors */ /* Selectors */
function focusedAnnotations(state) { const focusedAnnotations = createSelector(
return truthyKeys(state.selection.focused); state => state.selection.focused,
} focused => trueKeys(focused)
);
function forcedVisibleAnnotations(state) { const forcedVisibleAnnotations = createSelector(
return truthyKeys(state.selection.forcedVisible); state => state.selection.forcedVisible,
} forcedVisible => trueKeys(forcedVisible)
);
/** /**
* Is the annotation referenced by `$tag` currently focused? * Is the annotation referenced by `$tag` currently focused?
...@@ -446,7 +455,7 @@ function isAnnotationFocused(state, $tag) { ...@@ -446,7 +455,7 @@ function isAnnotationFocused(state, $tag) {
*/ */
const hasSelectedAnnotations = createSelector( const hasSelectedAnnotations = createSelector(
state => state.selection.selected, state => state.selection.selected,
selection => truthyKeys(selection).length > 0 selection => trueKeys(selection).length > 0
); );
/** De-select all annotations. */ /** De-select all annotations. */
...@@ -469,7 +478,7 @@ function clearSelection() { ...@@ -469,7 +478,7 @@ function clearSelection() {
const getFirstSelectedAnnotationId = createSelector( const getFirstSelectedAnnotationId = createSelector(
state => state.selection.selected, state => state.selection.selected,
selection => { selection => {
const selectedIds = truthyKeys(selection); const selectedIds = trueKeys(selection);
return selectedIds.length ? selectedIds[0] : null; return selectedIds.length ? selectedIds[0] : null;
} }
); );
...@@ -574,7 +583,7 @@ const hasAppliedFilter = createSelector( ...@@ -574,7 +583,7 @@ const hasAppliedFilter = createSelector(
const selectedAnnotations = createSelector( const selectedAnnotations = createSelector(
state => state.selection.selected, state => state.selection.selected,
selection => truthyKeys(selection) selection => trueKeys(selection)
); );
export default { export default {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment