The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useGT } from "gt-react-native"; |
| 2 | import { useEffect, useRef, useState } from "react"; |
| 3 | import Animated, { |
| 4 | cubicBezier, |
| 5 | runOnJS, |
| 6 | useAnimatedStyle, |
| 7 | useReducedMotion, |
| 8 | useSharedValue, |
| 9 | withTiming, |
| 10 | type SharedValue, |
| 11 | } from "react-native-reanimated"; |
| 12 | |
| 13 | import { constrainAuxiliaryPaneWidth, type WorkspacePaneLayout } from "../../lib/layout"; |
| 14 | import { WORKSPACE_PANE_TIMING } from "./workspace-pane-animation"; |
| 15 | import { WorkspacePaneDivider } from "./workspace-pane-divider"; |
| 16 | import { InspectorPanes } from "./inspector-host"; |
| 17 | import type { InspectorContent } from "./inspector-panes"; |
| 18 | |
| 19 | // CSS form of WORKSPACE_PANE_TIMING's Easing.inOut(Easing.cubic). |
| 20 | const PANE_EASING = cubicBezier(0.65, 0, 0.35, 1); |
| 21 | |
| 22 | /** Pass layout props to avoid an import cycle with AdaptiveWorkspaceLayout. */ |
| 23 | export function WorkspaceInspectorPane(props: { |
| 24 | readonly renderedInspectorWidth: SharedValue<number>; |
| 25 | readonly active?: boolean; |
| 26 | readonly onClose: () => void; |
| 27 | readonly onClosed?: () => void; |
| 28 | readonly panes: WorkspacePaneLayout; |
| 29 | readonly content: InspectorContent | undefined; |
| 30 | readonly setAuxiliaryPaneWidth: (width: number) => void; |
| 31 | }) { |
| 32 | const gt = useGT(); |
| 33 | const { panes, setAuxiliaryPaneWidth } = props; |
| 34 | const inspectorWidth = panes.auxiliaryPaneWidth; |
| 35 | const inspectorSupported = props.content !== undefined && inspectorWidth !== null; |
| 36 | const inspectorVisible = |
| 37 | inspectorSupported && panes.auxiliaryPaneVisible && (props.active ?? true); |
| 38 | const resizeStartWidth = useRef(0); |
| 39 | const [resizing, setResizing] = useState(false); |
| 40 | |
| 41 | const inspectorProgress = useSharedValue(inspectorVisible ? 1 : 0); |
| 42 | const { renderedInspectorWidth } = props; |
| 43 | const reduceMotion = useReducedMotion(); |
| 44 | |
| 45 | const onClosed = props.onClosed; |
| 46 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- animates the Reanimated inspector progress and width shared values when visibility or width changes |
| 47 | useEffect(() => { |
| 48 | inspectorProgress.set( |
| 49 | withTiming(inspectorVisible ? 1 : 0, WORKSPACE_PANE_TIMING, (finished) => { |
| 50 | if (finished === true && !inspectorVisible && onClosed !== undefined) { |
| 51 | runOnJS(onClosed)(); |
| 52 | } |
| 53 | }), |
| 54 | ); |
| 55 | const targetWidth = inspectorVisible ? (inspectorWidth ?? 0) : 0; |
| 56 | renderedInspectorWidth.set( |
| 57 | resizing ? targetWidth : withTiming(targetWidth, WORKSPACE_PANE_TIMING), |
| 58 | ); |
| 59 | }, [ |
| 60 | inspectorProgress, |
| 61 | inspectorVisible, |
| 62 | inspectorWidth, |
| 63 | onClosed, |
| 64 | renderedInspectorWidth, |
| 65 | resizing, |
| 66 | ]); |
| 67 | |
| 68 | const inspectorStyle = useAnimatedStyle( |
| 69 | () => ({ |
| 70 | opacity: inspectorProgress.value, |
| 71 | transform: [{ translateX: (1 - inspectorProgress.value) * 24 }], |
| 72 | width: renderedInspectorWidth.value, |
| 73 | }), |
| 74 | [], |
| 75 | ); |
| 76 | // A hidden or resizing pane takes its width at once; a visible one eases with the column. |
| 77 | const inspectorContentStyle = { |
| 78 | width: inspectorWidth ?? 0, |
| 79 | transitionProperty: "width", |
| 80 | transitionDuration: |
| 81 | !inspectorVisible || resizing || reduceMotion ? 0 : WORKSPACE_PANE_TIMING.duration, |
| 82 | transitionTimingFunction: PANE_EASING, |
| 83 | } as const; |
| 84 | const beginResize = () => { |
| 85 | resizeStartWidth.current = inspectorWidth ?? 0; |
| 86 | setResizing(true); |
| 87 | }; |
| 88 | const resizeBy = (delta: number) => { |
| 89 | setAuxiliaryPaneWidth( |
| 90 | constrainAuxiliaryPaneWidth({ |
| 91 | preferredWidth: resizeStartWidth.current + delta, |
| 92 | availableWidth: panes.contentPaneWidth, |
| 93 | }), |
| 94 | ); |
| 95 | }; |
| 96 | const endResize = () => { |
| 97 | setResizing(false); |
| 98 | }; |
| 99 | |
| 100 | return ( |
| 101 | <> |
| 102 | {inspectorVisible ? ( |
| 103 | <WorkspacePaneDivider |
| 104 | accessibilityLabel={gt("Resize detail pane")} |
| 105 | currentWidth={inspectorWidth ?? 0} |
| 106 | resizeDirection={-1} |
| 107 | onResizeStart={beginResize} |
| 108 | onResizeBy={resizeBy} |
| 109 | onResizeEnd={endResize} |
| 110 | /> |
| 111 | ) : null} |
| 112 | {inspectorSupported ? ( |
| 113 | <Animated.View |
| 114 | className="shrink-0 overflow-hidden" |
| 115 | accessibilityElementsHidden={!inspectorVisible} |
| 116 | collapsable={false} |
| 117 | importantForAccessibility={inspectorVisible ? "auto" : "no-hide-descendants"} |
| 118 | pointerEvents={inspectorVisible ? "auto" : "none"} |
| 119 | style={inspectorStyle} |
| 120 | > |
| 121 | <Animated.View className="flex-1" style={inspectorContentStyle}> |
| 122 | <InspectorPanes content={props.content} onClose={props.onClose} /> |
| 123 | </Animated.View> |
| 124 | </Animated.View> |
| 125 | ) : null} |
| 126 | </> |
| 127 | ); |
| 128 | } |