Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor(explorer-tree): pass ref as a prop instead of forwardRef (#22077)

Innei9a320ceb131f
diff --git a/src/features/ExplorerTree/types.ts b/src/features/ExplorerTree/types.ts
index ea4826fda8c..a0c26b19a27 100644
--- a/src/features/ExplorerTree/types.ts
+++ b/src/features/ExplorerTree/types.ts
@@ -1,5 +1,5 @@
import type { FileTreeRowDecoration, GitStatusEntry } from '@pierre/trees';
-import type { CSSProperties, DragEvent, KeyboardEvent, MouseEvent, ReactNode } from 'react';
+import type { CSSProperties, DragEvent, KeyboardEvent, MouseEvent, ReactNode, Ref } from 'react';
import type { NativeContextMenuItem } from '@/libs/contextMenu/types';
@@ -160,6 +160,8 @@ export interface ExplorerTreeProps<TData = unknown> {
ctx: ExplorerTreeKeyDownCtx<TData>,
) => boolean | void;
overscan?: number;
+ /** Imperative tree API; passed as a prop, as React 19 expects. */
+ ref?: Ref<ExplorerTreeHandle>;
selectedIds?: string[];
/**
* Sibling order. Without it the tree lists folders first, then names by code
diff --git a/src/features/ExplorerTree/view/ExplorerTree.tsx b/src/features/ExplorerTree/view/ExplorerTree.tsx
index 6fb0dc79514..d0df5ca14ea 100644
--- a/src/features/ExplorerTree/view/ExplorerTree.tsx
+++ b/src/features/ExplorerTree/view/ExplorerTree.tsx
@@ -9,9 +9,8 @@ import {
} from '@pierre/trees';
import { FileTree as PierreFileTree, useFileTreeSelection } from '@pierre/trees/react';
import debug from 'debug';
-import type { DragEvent, ForwardedRef, KeyboardEvent, MouseEvent } from 'react';
+import type { DragEvent, KeyboardEvent, MouseEvent } from 'react';
import {
- forwardRef,
useCallback,
useImperativeHandle,
useLayoutEffect,
@@ -100,14 +99,11 @@ interface ExplorerTreeInnerProps<TData> extends ExplorerTreeProps<TData> {
requestTreeRebuild: () => void;
}
-function ExplorerTreeInner<TData>(
- props: ExplorerTreeInnerProps<TData>,
- ref: ForwardedRef<ExplorerTreeHandle>,
-) {
+function ExplorerTreeInner<TData>(props: ExplorerTreeInnerProps<TData>) {
const propsRef = useRef(props);
propsRef.current = props;
- const { requestTreeRebuild } = props;
+ const { ref, requestTreeRebuild } = props;
const adapterRef = useSingleton(() => ({
current: normalizeTree(props.nodes),
@@ -744,24 +740,17 @@ function ExplorerTreeInner<TData>(
);
}
-const ExplorerTreeInnerWithRef = forwardRef(ExplorerTreeInner) as <TData>(
- props: ExplorerTreeInnerProps<TData> & { ref?: ForwardedRef<ExplorerTreeHandle> },
-) => ReturnType<typeof ExplorerTreeInner>;
-
// Owns the escape hatch for an unusable tree model: bumping the generation
// remounts the inner component, which builds a fresh model from the current
// paths. Kept in a parent so the inner component can ask for its own remount.
-function ExplorerTree<TData>({
- ref,
- ...props
-}: ExplorerTreeProps<TData> & { ref?: ForwardedRef<ExplorerTreeHandle> }) {
+function ExplorerTree<TData>({ ref, ...props }: ExplorerTreeProps<TData>) {
const [generation, setGeneration] = useState(0);
const requestTreeRebuild = useCallback(() => setGeneration((value) => value + 1), []);
return (
- <ExplorerTreeInnerWithRef
- {...props}
+ <ExplorerTreeInner
key={generation}
+ {...props}
ref={ref}
requestTreeRebuild={requestTreeRebuild}
/>