All files / utils courseTabUtils.js

100% Statements 11/11
100% Branches 6/6
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63            3x                               3x     265x           211x 211x   1x         53x 53x                             213x 73x       140x    
// Which tab of the course page (InstructorCourseShowPage) is showing is kept
// in the URL, as ?tab=<name>, so that a tab can be linked to, and survives a
// refresh of the page. The tab last used for each course is also remembered
// in the browser's local storage, for when the page is opened without ?tab=.
 
// Every tab the page can have; some are only shown for some courses or users.
export const COURSE_TABS = [
  "students",
  "staff",
  "teams",
  "sections",
  "assignments",
  "new-assignments",
  "jobs",
  "downloads",
  "dokku",
  "slack",
  "api-keys",
  "edudora",
  "settings",
];
 
export const DEFAULT_COURSE_TAB = "assignments";
 
export function courseTabStorageKey(courseId) {
  return `frontiers.courseTab.${courseId}`;
}
 
// Local storage can be unavailable (for example, blocked by browser settings);
// remembering the tab is a convenience, so that is never treated as an error.
export function getStoredCourseTab(courseId) {
  try {
    return window.localStorage.getItem(courseTabStorageKey(courseId));
  } catch {
    return null;
  }
}
 
export function storeCourseTab(courseId, tab) {
  try {
    window.localStorage.setItem(courseTabStorageKey(courseId), tab);
  } catch {
    // ignored; see above
  }
}
 
// requestedTab: the value of ?tab=, or null if the URL has none
// storedTab: the tab remembered for this course, or null
// visibleTabs: the tabs that the page is showing at the moment
//
// A tab that is asked for but is not (yet) visible, for example the Slack tab
// while the course options are still loading, gives the default tab for now;
// since this is worked out on every render, the requested tab takes over as
// soon as it appears.
export function chooseCourseTab({ requestedTab, storedTab, visibleTabs }) {
  if (requestedTab !== null) {
    return visibleTabs.includes(requestedTab)
      ? requestedTab
      : DEFAULT_COURSE_TAB;
  }
  return visibleTabs.includes(storedTab) ? storedTab : DEFAULT_COURSE_TAB;
}