expandedContextProvider.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import {createContext, useState} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import PreferencesStore from 'sentry/stores/preferencesStore';
  4. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  5. import useMedia from 'sentry/utils/useMedia';
  6. export const ExpandedContext = createContext<{
  7. expandedItemId: string | null;
  8. setExpandedItemId: (mainItemId: string | null) => void;
  9. shouldAccordionFloat: boolean;
  10. }>({
  11. expandedItemId: null,
  12. setExpandedItemId: () => {},
  13. shouldAccordionFloat: false,
  14. });
  15. // Provides the expanded context to the sidebar accordion when it's in the floating state only (collapsed sidebar or on mobile view)
  16. export function ExpandedContextProvider(props: any) {
  17. const [expandedItemId, setExpandedItemId] = useState<string | null>(null);
  18. const theme = useTheme();
  19. const preferences = useLegacyStore(PreferencesStore);
  20. const horizontal = useMedia(`(max-width: ${theme.breakpoints.medium})`);
  21. const shouldAccordionFloat = horizontal || !!preferences.collapsed;
  22. return (
  23. <ExpandedContext.Provider
  24. value={{expandedItemId, setExpandedItemId, shouldAccordionFloat}}
  25. >
  26. {props.children}
  27. </ExpandedContext.Provider>
  28. );
  29. }