Skip to content

Commit 31c8e40

Browse files
refactor(file-tree): extract file tree resize hooks into hook file, wrap both hooks with useFileTreeResize, export the wrapper function and update main component file
1 parent feda3dc commit 31c8e40

3 files changed

Lines changed: 92 additions & 72 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { ElementSize, Size } from '@/core/model';
2+
import { useCanvasContext } from '@/core/providers';
3+
import { useEffect, useRef } from 'react';
4+
5+
// Hook to resize edition text area based on content
6+
const useFileTreeResizeOnContentChange = (
7+
id: string,
8+
coords: { x: number; y: number },
9+
text: string,
10+
currentSize: Size,
11+
calculatedSize: Size,
12+
minHeight: number
13+
) => {
14+
const previousText = useRef(text);
15+
const { updateShapeSizeAndPosition } = useCanvasContext();
16+
17+
useEffect(() => {
18+
// Only update if the text has changed AND the height is different
19+
const textChanged = previousText.current !== text;
20+
21+
const finalHeight = Math.max(calculatedSize.height, minHeight);
22+
const finalSize = { ...calculatedSize, height: finalHeight };
23+
24+
const heightChanged = finalHeight !== currentSize.height;
25+
26+
if (textChanged && heightChanged) {
27+
previousText.current = text;
28+
updateShapeSizeAndPosition(id, coords, finalSize, false);
29+
}
30+
}, [
31+
text,
32+
calculatedSize.height,
33+
currentSize.height,
34+
id,
35+
coords.x,
36+
coords.y,
37+
updateShapeSizeAndPosition,
38+
]);
39+
};
40+
41+
const useFileTreeResizeOnSizeChange = (
42+
id: string,
43+
coords: { x: number; y: number },
44+
currentWidth: number,
45+
size?: ElementSize
46+
) => {
47+
const previousSize = useRef(size);
48+
const { updateShapeSizeAndPosition } = useCanvasContext();
49+
50+
useEffect(() => {
51+
// Only update if the size has changed
52+
if (previousSize.current !== size) {
53+
previousSize.current = size;
54+
55+
const newWidth = size === 'XS' ? 150 : 230;
56+
57+
if (currentWidth !== newWidth) {
58+
updateShapeSizeAndPosition(
59+
id,
60+
coords,
61+
{ width: newWidth, height: currentWidth },
62+
false
63+
);
64+
}
65+
}
66+
}, [size, currentWidth, id, coords.x, coords.y, updateShapeSizeAndPosition]);
67+
};
68+
69+
export const useFileTreeResize = (
70+
id: string,
71+
coords: { x: number; y: number },
72+
text: string,
73+
currentSize: Size,
74+
calculatedSize: Size,
75+
minHeight: number,
76+
size?: ElementSize
77+
) => {
78+
useFileTreeResizeOnContentChange(
79+
id,
80+
coords,
81+
text,
82+
currentSize,
83+
calculatedSize,
84+
minHeight
85+
);
86+
87+
useFileTreeResizeOnSizeChange(id, coords, currentSize.width, size);
88+
};

src/common/components/mock-components/front-rich-components/file-tree/file-tree.business.ts

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes';
22
import { ElementSize, ShapeSizeRestrictions, Size } from '@/core/model';
33
import { FONT_SIZE_VALUES } from '../../front-components/shape.const';
4-
import { useCanvasContext } from '@/core/providers';
5-
import { useEffect, useRef } from 'react';
64

75
interface FileTreeSizeValues {
86
fontSize: number;
@@ -147,67 +145,3 @@ export const calculateFileTreeDynamicSize = (
147145
finalHeight
148146
);
149147
};
150-
151-
// Hook to resize edition text area based on content
152-
export const useFileTreeResizeOnContentChange = (
153-
id: string,
154-
coords: { x: number; y: number },
155-
text: string,
156-
currentSize: Size,
157-
calculatedSize: Size,
158-
minHeight: number
159-
) => {
160-
const previousText = useRef(text);
161-
const { updateShapeSizeAndPosition } = useCanvasContext();
162-
163-
useEffect(() => {
164-
// Only update if the text has changed AND the height is different
165-
const textChanged = previousText.current !== text;
166-
167-
const finalHeight = Math.max(calculatedSize.height, minHeight);
168-
const finalSize = { ...calculatedSize, height: finalHeight };
169-
170-
const heightChanged = finalHeight !== currentSize.height;
171-
172-
if (textChanged && heightChanged) {
173-
previousText.current = text;
174-
updateShapeSizeAndPosition(id, coords, finalSize, false);
175-
}
176-
}, [
177-
text,
178-
calculatedSize.height,
179-
currentSize.height,
180-
id,
181-
coords.x,
182-
coords.y,
183-
updateShapeSizeAndPosition,
184-
]);
185-
};
186-
187-
export const useFileTreeResizeOnSizeChange = (
188-
id: string,
189-
coords: { x: number; y: number },
190-
currentWidth: number,
191-
size?: ElementSize
192-
) => {
193-
const previousSize = useRef(size);
194-
const { updateShapeSizeAndPosition } = useCanvasContext();
195-
196-
useEffect(() => {
197-
// Only update if the size has changed
198-
if (previousSize.current !== size) {
199-
previousSize.current = size;
200-
201-
const newWidth = size === 'XS' ? 150 : 230;
202-
203-
if (currentWidth !== newWidth) {
204-
updateShapeSizeAndPosition(
205-
id,
206-
coords,
207-
{ width: newWidth, height: currentWidth },
208-
false
209-
);
210-
}
211-
}
212-
}, [size, currentWidth, id, coords.x, coords.y, updateShapeSizeAndPosition]);
213-
};

src/common/components/mock-components/front-rich-components/file-tree/file-tree.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import {
1111
FileTreeItem,
1212
getFileTreeSizeValues,
1313
parseFileTreeText,
14-
useFileTreeResizeOnContentChange,
15-
useFileTreeResizeOnSizeChange,
1614
} from './file-tree.business';
15+
import { useFileTreeResize } from './file-tree-resize.hook';
1716

1817
const fileTreeShapeRestrictions: ShapeSizeRestrictions = {
1918
minWidth: 150,
@@ -75,17 +74,16 @@ export const FileTreeShape = forwardRef<any, FileTreeShapeProps>(
7574
baseRestrictions: fileTreeShapeRestrictions,
7675
});
7776

78-
useFileTreeResizeOnContentChange(
77+
useFileTreeResize(
7978
id,
8079
{ x, y },
8180
text,
8281
{ width, height },
8382
restrictedSize,
84-
fileTreeShapeRestrictions.minHeight
83+
fileTreeShapeRestrictions.minHeight,
84+
otherProps?.size
8585
);
8686

87-
useFileTreeResizeOnSizeChange(id, { x, y }, width, otherProps?.size);
88-
8987
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
9088

9189
const { stroke, strokeStyle, fill, textColor, borderRadius } =

0 commit comments

Comments
 (0)