# All files / src/main-layout useMainLayoutProvider.ts

0% Statements 0/133
0% Branches 0/1
0% Functions 0/1
0% Lines 0/133

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

     import { useCallback, useMemo, useEffect, useState, useRef } from "react";
    import { useAppSize } from "../AppSizeProvider";
    import { useSsr } from "../SsrProvider";
    import type { LocalStorageDeserializer } from "../useLocalStorage";
    import { useLocalStorage } from "../useLocalStorage";
    import type {
      MainLayoutConfiguration,
      MainLayoutContext,
    } from "./useMainLayout";

    const deserializer: LocalStorageDeserializer<boolean> = (value) =>
      value === "true";

    export interface MainLayoutProviderOptions
      extends Required<MainLayoutConfiguration> {
      navId: string;
      mainId: string;
    }

    export function useMainLayoutProvider(
      options: MainLayoutProviderOptions
    ): MainLayoutContext {
      const {
        navId,
        mainId,
        layout,
        variant,
        expandedKey,
        defaultVisible,
        defaultExpanded,
      } = options;

      const { isPhone } = useAppSize();
      const mini = layout.includes("mini");
      let currentLayout = layout;
      if (isPhone) {
        currentLayout = mini ? "temporary-mini" : "temporary";
      }

      const prevLayout = useRef(currentLayout);
      const disableTemporaryTransition = useRef(false);
      const disablePersistentTransition = useRef(false);

      const [visible, setVisible] = useState(defaultVisible);
      const showTemporaryNavigation = useCallback(() => {
        setVisible(true);
      }, []);
      const hideTemporaryNavigation = useCallback(() => {
        setVisible(false);
      }, []);

      const { value: expanded, setValue: setExpanded } = useLocalStorage({
        key: expandedKey,
        deserializer,
        defaultValue: defaultExpanded,
      });
      const expandNavigation = useCallback(() => {
        setExpanded(true);
      }, [setExpanded]);
      const collapseNavigation = useCallback(() => {
        setExpanded(false);
      }, [setExpanded]);

      // when the `expandedKey` is set, do not animate if the local storage expanded state
      // did not match the `defaultExpanded` state
      const ssr = useSsr();
      const initialExpanded = useRef<boolean | null>(expanded);
      if (!ssr && initialExpanded.current !== null) {
        disablePersistentTransition.current = expanded !== initialExpanded.current;
        initialExpanded.current = null;
      }

      useEffect(() => {
        const changed = currentLayout !== prevLayout.current;
        prevLayout.current = currentLayout;
        if (changed) {
          disablePersistentTransition.current = true;
        }

        if (!changed || !visible) {
          return;
        }

        disableTemporaryTransition.current = true;
        hideTemporaryNavigation();
      }, [currentLayout, hideTemporaryNavigation, visible]);

      const toggleNavigation = useCallback(() => {
        if (currentLayout.includes("temporary")) {
          setVisible((prevVisible) => !prevVisible);
        } else {
          setExpanded((prevExpanded) => !prevExpanded);
        }
      }, [currentLayout, setExpanded, setVisible]);

      return useMemo<MainLayoutContext>(
        () => ({
          mini,
          navId,
          mainId,
          layout,
          variant,
          visible,
          expanded,
          setVisible,
          setExpanded,
          currentLayout,
          toggleNavigation,
          expandNavigation,
          collapseNavigation,
          showTemporaryNavigation,
          hideTemporaryNavigation,
          disableTemporaryTransition,
          disablePersistentTransition,
        }),
        [
          collapseNavigation,
          currentLayout,
          expandNavigation,
          expanded,
          hideTemporaryNavigation,
          layout,
          mainId,
          mini,
          navId,
          setExpanded,
          showTemporaryNavigation,
          toggleNavigation,
          variant,
          visible,
        ]
      );
    }