Commit 48718916 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Update user-focus store, rootThread intergration for clarity

parent b104d018
...@@ -4,6 +4,11 @@ const { createElement } = require('preact'); ...@@ -4,6 +4,11 @@ const { createElement } = require('preact');
const useStore = require('../store/use-store'); const useStore = require('../store/use-store');
/**
* Render a control to interact with any focused "mode" in the sidebar.
* Currently only a user-focus mode is supported but this could be broadened
* and abstracted if needed. Allow user to toggle in and out of the focus "mode."
*/
function FocusedModeHeader() { function FocusedModeHeader() {
const store = useStore(store => ({ const store = useStore(store => ({
actions: { actions: {
......
...@@ -49,22 +49,14 @@ function RootThread($rootScope, store, searchFilter, viewFilter) { ...@@ -49,22 +49,14 @@ function RootThread($rootScope, store, searchFilter, viewFilter) {
function buildRootThread(state) { function buildRootThread(state) {
const sortFn = sortFns[state.sortKey]; const sortFn = sortFns[state.sortKey];
const shouldFilterThread = () => { const shouldFilterThread = () => {
// is there a query or focused truthy value from the config? // Is there a search query, or are we in an active (focused) focus mode?
return state.filterQuery || store.focusModeFocused(); return state.filterQuery || store.focusModeFocused();
}; };
let filterFn; let filterFn;
if (shouldFilterThread()) { if (shouldFilterThread()) {
const userFilter = {}; // optional user filter object for focused mode const filters = searchFilter.generateFacetedFilter(state.filterQuery, {
// look for a unique username, if present, add it to the user filter user: store.focusModeUsername(), // `null` if no focused user
const focusedUsername = store.focusModeUsername(); // may be null if no focused user });
if (focusedUsername) {
// focused user found, add it to the filter object
userFilter.user = focusedUsername;
}
const filters = searchFilter.generateFacetedFilter(
state.filterQuery,
userFilter
);
filterFn = function(annot) { filterFn = function(annot) {
return viewFilter.filter([annot], filters).length > 0; return viewFilter.filter([annot], filters).length > 0;
......
...@@ -324,7 +324,7 @@ function setFilterQuery(query) { ...@@ -324,7 +324,7 @@ function setFilterQuery(query) {
} }
/** /**
* Set the focused to only show annotations by the focused user. * Set the focused to only show annotations matching the current focus mode.
*/ */
function setFocusModeFocused(focused) { function setFocusModeFocused(focused) {
return { return {
...@@ -379,28 +379,29 @@ const getFirstSelectedAnnotationId = createSelector( ...@@ -379,28 +379,29 @@ const getFirstSelectedAnnotationId = createSelector(
function filterQuery(state) { function filterQuery(state) {
return state.filterQuery; return state.filterQuery;
} }
/** /**
* Returns the on/off state of the focus mode. This can be toggled on or off to * Do the config settings indicate that the client should be in a focused mode?
* filter to the focused user.
* *
* @return {boolean} * @return {boolean}
*/ */
function focusModeFocused(state) { function focusModeEnabled(state) {
return state.focusMode.enabled && state.focusMode.focused; return state.focusMode.enabled;
} }
/** /**
* Returns the value of the focus mode from the config. * Is a focus mode enabled, and is it presently applied?
* *
* @return {boolean} * @return {boolean}
*/ */
function focusModeEnabled(state) { function focusModeFocused(state) {
return state.focusMode.enabled; return focusModeEnabled(state) && state.focusMode.focused;
} }
/** /**
* Returns the username of the focused mode or null if none is found. * Returns the username for a focused user or `null` if no focused user.
* *
* @return {object} * @return {object|null}
*/ */
function focusModeUsername(state) { function focusModeUsername(state) {
if (state.focusMode.config.user && state.focusMode.config.user.username) { if (state.focusMode.config.user && state.focusMode.config.user.username) {
...@@ -409,6 +410,16 @@ function focusModeUsername(state) { ...@@ -409,6 +410,16 @@ function focusModeUsername(state) {
return null; return null;
} }
/**
* Does the configured focus mode include user info, i.e. are we focusing on a
* user?
*
* @return {boolean}
*/
function focusModeHasUser(state) {
return focusModeEnabled(state) && !!focusModeUsername(state);
}
/** /**
* Returns the display name for a user or the username * Returns the display name for a user or the username
* if display name is not present. If both are missing * if display name is not present. If both are missing
...@@ -453,6 +464,7 @@ module.exports = { ...@@ -453,6 +464,7 @@ module.exports = {
filterQuery, filterQuery,
focusModeFocused, focusModeFocused,
focusModeEnabled, focusModeEnabled,
focusModeHasUser,
focusModeUsername, focusModeUsername,
focusModeUserPrettyName, focusModeUserPrettyName,
isAnnotationSelected, isAnnotationSelected,
......
...@@ -223,6 +223,27 @@ describe('store/modules/selection', () => { ...@@ -223,6 +223,27 @@ describe('store/modules/selection', () => {
}); });
}); });
describe('focusModeHasUser()', () => {
it('should return `true` if focus enabled and valid `user` object present', () => {
store = createStore(
[selection],
[{ focus: { user: { username: 'whatever' } } }]
);
assert.isTrue(store.focusModeHasUser());
});
it('should return `false` if focus enabled but `user` object invalid', () => {
store = createStore(
[selection],
[{ focus: { user: { displayName: 'whatever' } } }] // `username` is required
);
assert.isFalse(store.focusModeHasUser());
});
it('should return `false` if `user` object missing', () => {
store = createStore([selection], [{ focus: {} }]);
assert.isFalse(store.focusModeHasUser());
});
});
describe('focusModeUserPrettyName()', function() { describe('focusModeUserPrettyName()', function() {
it('should return false by default when focus mode is not enabled', function() { it('should return false by default when focus mode is not enabled', function() {
store = createStore( store = createStore(
......
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