All files / src/components/app MyAppSections.tsx

82.45% Statements 47/57
68.75% Branches 11/16
84% Functions 21/25
86.27% Lines 44/51

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                                          1x 3x 5x 2x   2x           3x 3x                                     3x   3x                               3x 2x         3x     1x           7x         5x 7x     7x   5x 7x     7x   7x       5x     9x       7x   7x   7x     5x 7x 15x   7x       7x               7x 7x       7x   5x 7x   6x   5x 6x 4x   7x       5x       7x         5x    
import { PageConfig } from "@/routes/App";
import { onlyUnique } from "@/util/array";
import { ReactNode } from "react";
import SectionLink from "../buttons/SectionLink/SectionLink";
import Logo from "../img/Logo/Logo";
import { NavbarSection } from "../nav/NavBar/Navbar";
import NavItem from "../nav/NavItem/NavItem";
 
export interface SubPageConfig {
  page_config: PageConfig;
  links: MyAppSectionLink[];
}
 
export interface MyAppSectionLink {
  route: string;
  title: ReactNode;
  subpages?: MyAppSectionLink[];
}
 
export interface MyAppSection {
  links: MyAppSectionLink[];
}
 
export interface MyAppSectionsProps {
  homeName: string;
  home: MyAppSection;
  sections: MyAppSection[];
}
 
export const MyAppSections = (props: MyAppSectionsProps) => {
  const getSectionLinks = (links: MyAppSectionLink[]) => {
    return links.map((link) => {
      Iif (link.subpages && link.subpages.length > 0) {
        return <NavItem key={link.route} link={link} />;
      } else return <NavItem link={link} key={link.route} />;
    });
  };
 
  // The brand block at the top of the sidebar: logo, the app's name running
  // vertically, and the home page's title underneath.
  const brand = (title?: ReactNode) => (
    <>
      <div className="flex flex-row items-center">
        <Logo></Logo>
        <div className="w-8 flex justify-center">
          <span className="transform rotate-90 text-xs ml-4 origin-middle inline-block">
            {props.homeName.toUpperCase()}
          </span>
        </div>
      </div>
      <div className="mb-4" />
      {title}
    </>
  );
 
  // An app whose config declares no `nav_section: "home"` page has nothing to
  // link the brand block to. Reading `links[0].route` unconditionally threw
  // "Cannot read properties of undefined (reading 'route')" and took the whole
  // section down — which is what the Legal section did before it had a home
  // page. Render the brand unlinked instead.
  const [homeLink, ...restHomeLinks] = props.home.links;
 
  const homeSection = {
    elements: [
      homeLink ? (
        <SectionLink href={homeLink.route} key={homeLink.route}>
          {brand(homeLink.title)}
        </SectionLink>
      ) : (
        <div key="brand" className="block w-full pl-4 py-2 border-l">
          {brand()}
        </div>
      ),
      ...getSectionLinks(restHomeLinks),
    ],
    hr: true,
  } as NavbarSection;
 
  const otherSections: NavbarSection[] = props.sections.map((section) => {
    return {
      elements: getSectionLinks(section.links),
      hr: true,
    };
  });
  return [homeSection, ...otherSections];
};
 
export const appSections = (
  homeName: string,
  props: Record<string, PageConfig>,
  paramMap?: Record<string, string>,
  titleMap?: Record<string, ReactNode>,
) => {
  const PAGE_ENTRIES = Object.entries(props).map(([key, cfg]) => ({
    key,
    cfg,
  }));
 
  const replaceParams = (route: string) => {
    Object.entries(paramMap ?? {}).forEach(([k, v]) => {
      route = route.replace(k, v);
    });
    return route;
  };
  const replaceTitle = (route: string) => {
    for (const [k, v] of Object.entries(titleMap ?? {})) {
      if (route === k) return v;
    }
    return null;
  };
  const toLink = (cfg: PageConfig): MyAppSectionLink => ({
    route: replaceParams(cfg.route),
    title: replaceTitle(cfg.route) ?? cfg.title,
  });
  const getLinks = (
    entries: { key: string; cfg: PageConfig }[],
  ): MyAppSectionLink[] => {
    return entries.map(({ key, cfg }) => {
      // const subpageEntries = PAGE_ENTRIES.filter(
      //   ({ cfg: c }) => c.nav_subpage_of === key,
      // );
      const link = toLink(cfg);
      // if (subpageEntries.length > 0) {
      link.subpages = getSubSubpageLinks(key);
      // }
      return link;
    });
  };
  const getSubSubpageLinks = (key: string): MyAppSectionLink[] => {
    const subpageEntries = PAGE_ENTRIES.filter(
      ({ cfg: c }) => c.nav_subpage_of === key,
    );
    const subpageLinks = subpageEntries.map(({ cfg }) => ({
      cfg,
      link: toLink(cfg),
    }));
    for (const { cfg, link } of subpageLinks) {
      const subSubpageEntries = PAGE_ENTRIES.filter(
        ({ cfg: c }) => c.nav_subpage_of === cfg.route,
      );
      if (subSubpageEntries.length > 0) {
        link.subpages = getSubSubpageLinks(cfg.route);
      }
    }
    const links = subpageLinks.map(({ link }) => link);
    return links;
  };
 
  // Pages with nav_subpage_of are rendered under their parent, not as top-level nav items
  const topLevelEntries = PAGE_ENTRIES.filter(({ cfg }) => !cfg.nav_subpage_of);
 
  const distinct_sections = topLevelEntries
    .map(({ cfg }) => cfg.nav_section)
    .filter(onlyUnique)
    .filter((el) => el !== undefined);
 
  const other_sections: MyAppSection[] = distinct_sections
    .filter((el) => el !== "home")
    .map((section) => ({
      links: getLinks(
        topLevelEntries.filter(({ cfg }) => cfg.nav_section === section),
      ),
    }));
 
  const sections: MyAppSectionsProps = {
    homeName: homeName,
    home: {
      links: getLinks(
        topLevelEntries.filter(({ cfg }) => cfg.nav_section === "home"),
      ),
    },
    sections: other_sections,
  };
  return sections;
};