-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rani
committed
Jun 3, 2021
1 parent
8524b0e
commit 0187438
Showing
13 changed files
with
220 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
app/scripts/modules/core/src/managed/environmentBaseElements/BaseEnvironment.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/scripts/modules/core/src/managed/environmentBaseElements/EnvironmentsRender.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import classnames from 'classnames'; | ||
import { reverse } from 'lodash'; | ||
import React from 'react'; | ||
import { atom, useRecoilState } from 'recoil'; | ||
|
||
import { useElementDimensions } from 'core/presentation/hooks/useDimensions.hook'; | ||
import { logger } from 'core/utils'; | ||
|
||
const STORAGE_KEY = 'MD_environmentsDirection'; | ||
|
||
const DIRECTIONS = ['list', 'sideBySide'] as const; | ||
type Direction = typeof DIRECTIONS[number]; | ||
|
||
const isDirection = (value: string | null): value is Direction => { | ||
return Boolean(value && DIRECTIONS.includes(value as Direction)); | ||
}; | ||
|
||
const storedDirection = localStorage.getItem(STORAGE_KEY); | ||
|
||
const environmentsDirectionState = atom<Direction>({ | ||
key: 'environmentsDisplay', | ||
default: isDirection(storedDirection) ? storedDirection : 'list', | ||
}); | ||
|
||
// The goal of this hook is to store the value in an atom to be shared across the app but also update the local storage | ||
const useEnvironmentDirection = () => { | ||
const [direction, setDirection] = useRecoilState(environmentsDirectionState); | ||
React.useLayoutEffect(() => { | ||
localStorage.setItem(STORAGE_KEY, direction); | ||
}, [direction]); | ||
|
||
return { direction, setDirection }; | ||
}; | ||
|
||
export const EnvironmentsDirectionController = () => { | ||
const { direction, setDirection } = useEnvironmentDirection(); | ||
return ( | ||
<button | ||
type="button" | ||
className="btn env-direction-btn" | ||
onClick={() => setDirection((state) => (state === 'list' ? 'sideBySide' : 'list'))} | ||
> | ||
{direction === 'list' ? 'Grid view' : 'List view'} | ||
<i className={classnames(direction === 'list' ? 'far fa-list-alt' : 'fas fa-columns', 'sp-margin-xs-left')} /> | ||
</button> | ||
); | ||
}; | ||
|
||
const MIN_WIDTH_PER_COLUMN = 500; | ||
|
||
interface IEnvironmentsRenderProps { | ||
className?: string; | ||
children: React.ReactElement[]; | ||
} | ||
|
||
export const EnvironmentsRender = ({ className, children }: IEnvironmentsRenderProps) => { | ||
const { direction } = useEnvironmentDirection(); | ||
const ref = React.useRef(null); | ||
const { width } = useElementDimensions({ ref, isActive: direction === 'sideBySide' }); | ||
let numEnvironments = 1; | ||
if (Array.isArray(children)) { | ||
numEnvironments = children.length; | ||
} else { | ||
logger.log({ | ||
level: 'ERROR', | ||
error: new Error('Environments children should be an array'), | ||
action: 'Environments::Render', | ||
}); | ||
} | ||
|
||
const numColumns = Math.min(Math.round(width / MIN_WIDTH_PER_COLUMN), numEnvironments); | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
className={classnames(className, 'environments-list', { 'side-by-side': direction === 'sideBySide' })} | ||
style={direction === 'sideBySide' ? { gridTemplateColumns: `repeat(${numColumns}, 1fr)` } : undefined} | ||
> | ||
{direction === 'list' && children} | ||
{direction === 'sideBySide' && width > 0 ? reverse(React.Children.toArray(children)) : null} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/scripts/modules/core/src/presentation/hooks/useDimensions.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { debounce } from 'lodash'; | ||
import React from 'react'; | ||
|
||
export const useWindowDimensions = ({ delay = 200, isActive = true }: { delay?: number; isActive?: boolean }) => { | ||
const [dimension, setDimension] = React.useState({ width: window.innerWidth, height: window.innerHeight }); | ||
|
||
React.useLayoutEffect(() => { | ||
const debouncedResizeHandler = debounce(() => { | ||
setDimension({ width: window.innerWidth, height: window.innerHeight }); | ||
}, delay); | ||
if (isActive) { | ||
window.addEventListener('resize', debouncedResizeHandler); | ||
return () => window.removeEventListener('resize', debouncedResizeHandler); | ||
} else { | ||
return () => {}; | ||
} | ||
}, [delay, isActive]); | ||
|
||
React.useLayoutEffect(() => { | ||
if (isActive) { | ||
setDimension({ width: window.innerWidth, height: window.innerHeight }); | ||
} | ||
}, [isActive]); | ||
|
||
return dimension; | ||
}; | ||
|
||
const getElementDimensions = (ref: React.RefObject<HTMLElement>) => | ||
ref.current ? { width: ref.current.offsetWidth, height: ref.current.offsetHeight } : { width: 0, height: 0 }; | ||
|
||
export const useElementDimensions = ({ | ||
ref, | ||
delay = 200, | ||
isActive = true, | ||
}: { | ||
ref: React.RefObject<HTMLElement>; | ||
delay?: number; | ||
isActive?: boolean; | ||
}) => { | ||
const [dimension, setDimension] = React.useState(getElementDimensions(ref)); | ||
|
||
React.useLayoutEffect(() => { | ||
const debouncedResizeHandler = debounce(() => { | ||
setDimension(getElementDimensions(ref)); | ||
}, delay); | ||
|
||
if (isActive && ref.current) { | ||
const observer = new ResizeObserver(debouncedResizeHandler); | ||
observer.observe(ref.current); | ||
return () => observer.disconnect(); | ||
} else { | ||
return () => {}; | ||
} | ||
}, [delay, isActive, ref.current]); | ||
|
||
return dimension; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4615,6 +4615,11 @@ | |
"@types/node" "*" | ||
"@types/tough-cookie" "*" | ||
|
||
"@types/resize-observer-browser@^0.1.5": | ||
version "0.1.5" | ||
resolved "https://registry.yarnpkg.com/@types/resize-observer-browser/-/resize-observer-browser-0.1.5.tgz#36d897708172ac2380cd486da7a3daf1161c1e23" | ||
integrity sha512-8k/67Z95Goa6Lznuykxkfhq9YU3l1Qe6LNZmwde1u7802a3x8v44oq0j91DICclxatTr0rNnhXx7+VTIetSrSQ== | ||
|
||
"@types/source-list-map@*": | ||
version "0.1.2" | ||
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9" | ||
|
@@ -10869,6 +10874,11 @@ gzip-size@^3.0.0: | |
dependencies: | ||
duplexer "^0.1.1" | ||
|
||
[email protected]: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/hamt_plus/-/hamt_plus-1.0.2.tgz#e21c252968c7e33b20f6a1b094cd85787a265601" | ||
integrity sha1-4hwlKWjH4zsg9qGwlM2FeHomVgE= | ||
|
||
handle-thing@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/handle-thing/-/handle-thing-2.0.0.tgz#0e039695ff50c93fc288557d696f3c1dc6776754" | ||
|
@@ -16745,6 +16755,13 @@ recoil@^0.0.10: | |
resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.0.10.tgz#679ab22306f559f8a63c46fd5ff5241539f9248f" | ||
integrity sha512-+9gRqehw3yKETmoZbhSnWu4GO10HDb5xYf1CjLF1oXGK2uT6GX5Lu9mfTXwjxV/jXxEKx8MIRUUbgPxvbJ8SEw== | ||
|
||
recoil@^0.3.1: | ||
version "0.3.1" | ||
resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.3.1.tgz#40ef544160d19d76e25de8929d7e512eace13b90" | ||
integrity sha512-KNA3DRqgxX4rRC8E7fc6uIw7BACmMPuraIYy+ejhE8tsw7w32CetMm8w7AMZa34wzanKKkev3vl3H7Z4s0QSiA== | ||
dependencies: | ||
hamt_plus "1.0.2" | ||
|
||
[email protected]: | ||
version "2.2.2" | ||
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f" | ||
|