| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {atom, useSetAtom} from 'jotai'; |
| 9 | import {useEffect, useRef} from 'react'; |
| 10 | import {useCommand} from './ISLShortcuts'; |
| 11 | import { |
| 12 | atomWithOnChange, |
| 13 | configBackedAtom, |
| 14 | localStorageBackedAtom, |
| 15 | readAtom, |
| 16 | writeAtom, |
| 17 | } from './jotaiUtils'; |
| 18 | |
| 19 | export const mainContentWidthState = atom(500); |
| 20 | |
| 21 | export const renderCompactAtom = configBackedAtom<boolean>('isl.render-compact', false); |
| 22 | |
| 23 | export const zoomUISettingAtom = atomWithOnChange( |
| 24 | localStorageBackedAtom<number>('isl.ui-zoom', 1), |
| 25 | newValue => { |
| 26 | document.body?.style.setProperty('--zoom', `${newValue}`); |
| 27 | }, |
| 28 | ); |
| 29 | |
| 30 | export function useZoomShortcut() { |
| 31 | useCommand('ZoomIn', () => { |
| 32 | const old = readAtom(zoomUISettingAtom); |
| 33 | writeAtom(zoomUISettingAtom, Math.round((old + 0.1) * 100) / 100); |
| 34 | }); |
| 35 | useCommand('ZoomOut', () => { |
| 36 | const old = readAtom(zoomUISettingAtom); |
| 37 | writeAtom(zoomUISettingAtom, Math.round((old - 0.1) * 100) / 100); |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | export function useMainContentWidth() { |
| 42 | const setMainContentWidth = useSetAtom(mainContentWidthState); |
| 43 | |
| 44 | const mainContentRef = useRef<null | HTMLDivElement>(null); |
| 45 | useEffect(() => { |
| 46 | const element = mainContentRef.current; |
| 47 | if (element == null) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | const obs = new ResizeObserver(entries => { |
| 52 | const [entry] = entries; |
| 53 | setMainContentWidth(entry.contentRect.width); |
| 54 | }); |
| 55 | obs.observe(element); |
| 56 | return () => obs.unobserve(element); |
| 57 | }, [mainContentRef, setMainContentWidth]); |
| 58 | |
| 59 | return mainContentRef; |
| 60 | } |
| 61 | |
| 62 | export const NARROW_COMMIT_TREE_WIDTH = 800; |
| 63 | export const NARROW_COMMIT_TREE_WIDTH_WHEN_COMPACT = 300; |
| 64 | |
| 65 | export const isNarrowCommitTree = atom( |
| 66 | get => |
| 67 | get(mainContentWidthState) < |
| 68 | (get(renderCompactAtom) ? NARROW_COMMIT_TREE_WIDTH_WHEN_COMPACT : NARROW_COMMIT_TREE_WIDTH), |
| 69 | ); |
| 70 | |