1.9 KB70 lines
Blame
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
8import {atom, useSetAtom} from 'jotai';
9import {useEffect, useRef} from 'react';
10import {useCommand} from './ISLShortcuts';
11import {
12 atomWithOnChange,
13 configBackedAtom,
14 localStorageBackedAtom,
15 readAtom,
16 writeAtom,
17} from './jotaiUtils';
18
19export const mainContentWidthState = atom(500);
20
21export const renderCompactAtom = configBackedAtom<boolean>('isl.render-compact', false);
22
23export const zoomUISettingAtom = atomWithOnChange(
24 localStorageBackedAtom<number>('isl.ui-zoom', 1),
25 newValue => {
26 document.body?.style.setProperty('--zoom', `${newValue}`);
27 },
28);
29
30export 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
41export 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
62export const NARROW_COMMIT_TREE_WIDTH = 800;
63export const NARROW_COMMIT_TREE_WIDTH_WHEN_COMPACT = 300;
64
65export const isNarrowCommitTree = atom(
66 get =>
67 get(mainContentWidthState) <
68 (get(renderCompactAtom) ? NARROW_COMMIT_TREE_WIDTH_WHEN_COMPACT : NARROW_COMMIT_TREE_WIDTH),
69);
70