Skip to content

Commit 1d191a2

Browse files
committed
fix: some refactoring
1 parent 0d73536 commit 1d191a2

File tree

5 files changed

+125
-105
lines changed

5 files changed

+125
-105
lines changed
File renamed without changes.

src/containers/Drawer/Drawer.tsx renamed to src/components/Drawer/Drawer.tsx

Lines changed: 42 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,16 @@ import {DrawerItem, Drawer as GravityDrawer} from '@gravity-ui/navigation';
44

55
import {cn} from '../../utils/cn';
66

7+
import {useDrawerContext} from './DrawerContext';
8+
79
const DEFAULT_DRAWER_WIDTH_PERCENTS = 60;
810
const DEFAULT_DRAWER_WIDTH = 600;
911
const DRAWER_WIDTH_KEY = 'drawer-width';
1012
const b = cn('ydb-drawer');
1113

1214
import './Drawer.scss';
1315

14-
// Create a context for sharing container dimensions
15-
interface DrawerContextType {
16-
containerWidth: number;
17-
setContainerWidth: React.Dispatch<React.SetStateAction<number>>;
18-
}
19-
20-
const DrawerContext = React.createContext<DrawerContextType | undefined>(undefined);
21-
22-
// Custom hook to use the drawer context
23-
const useDrawerContext = () => {
24-
const context = React.useContext(DrawerContext);
25-
if (context === undefined) {
26-
return {containerWidth: 0, setContainerWidth: () => {}};
27-
}
28-
return context;
29-
};
30-
31-
interface ContentWrapperProps {
16+
interface DrawerPaneContentWrapperProps {
3217
isVisible: boolean;
3318
onClose: () => void;
3419
children: React.ReactNode;
@@ -41,7 +26,7 @@ interface ContentWrapperProps {
4126
isPercentageWidth?: boolean;
4227
}
4328

44-
const ContentWrapper = ({
29+
const DrawerPaneContentWrapper = ({
4530
isVisible,
4631
onClose,
4732
children,
@@ -52,7 +37,7 @@ const ContentWrapper = ({
5237
className,
5338
detectClickOutside = false,
5439
isPercentageWidth,
55-
}: ContentWrapperProps) => {
40+
}: DrawerPaneContentWrapperProps) => {
5641
const [drawerWidth, setDrawerWidth] = React.useState(() => {
5742
const savedWidth = localStorage.getItem(storageKey);
5843
return savedWidth ? Number(savedWidth) : defaultWidth;
@@ -126,12 +111,7 @@ const ContentWrapper = ({
126111
);
127112
};
128113

129-
interface ContainerProps {
130-
children: React.ReactNode;
131-
className?: string;
132-
}
133-
134-
interface ItemWrapperProps {
114+
interface DrawerPaneProps {
135115
children: React.ReactNode;
136116
renderDrawerContent: () => React.ReactNode;
137117
isDrawerVisible: boolean;
@@ -145,80 +125,40 @@ interface ItemWrapperProps {
145125
isPercentageWidth?: boolean;
146126
}
147127

148-
export const Drawer = {
149-
Container: ({children, className}: ContainerProps) => {
150-
const [containerWidth, setContainerWidth] = React.useState(0);
151-
const containerRef = React.useRef<HTMLDivElement>(null);
152-
153-
React.useEffect(() => {
154-
if (!containerRef.current) {
155-
return undefined;
156-
}
157-
158-
const updateWidth = () => {
159-
if (containerRef.current) {
160-
setContainerWidth(containerRef.current.clientWidth);
161-
}
162-
};
163-
164-
// Set initial width
165-
updateWidth();
166-
167-
// Update width on resize
168-
const resizeObserver = new ResizeObserver(updateWidth);
169-
resizeObserver.observe(containerRef.current);
170-
171-
return () => {
172-
if (containerRef.current) {
173-
resizeObserver.disconnect();
174-
}
175-
};
176-
}, []);
177-
178-
return (
179-
<DrawerContext.Provider value={{containerWidth, setContainerWidth}}>
180-
<div ref={containerRef} className={b('drawer-container', className)}>
181-
{children}
182-
</div>
183-
</DrawerContext.Provider>
184-
);
185-
},
186-
187-
ItemWrapper: ({
188-
children,
189-
renderDrawerContent,
190-
isDrawerVisible,
191-
onCloseDrawer,
192-
drawerId,
193-
storageKey,
194-
defaultWidth,
195-
direction,
196-
className,
197-
detectClickOutside,
198-
isPercentageWidth,
199-
}: ItemWrapperProps) => {
200-
React.useEffect(() => {
201-
return () => {
202-
onCloseDrawer();
203-
};
204-
}, [onCloseDrawer]);
205-
return (
206-
<React.Fragment>
207-
{children}
208-
<ContentWrapper
209-
isVisible={isDrawerVisible}
210-
onClose={onCloseDrawer}
211-
drawerId={drawerId}
212-
storageKey={storageKey}
213-
defaultWidth={defaultWidth}
214-
direction={direction}
215-
className={className}
216-
detectClickOutside={detectClickOutside}
217-
isPercentageWidth={isPercentageWidth}
218-
>
219-
{renderDrawerContent()}
220-
</ContentWrapper>
221-
</React.Fragment>
222-
);
223-
},
128+
export const DrawerWrapper = ({
129+
children,
130+
renderDrawerContent,
131+
isDrawerVisible,
132+
onCloseDrawer,
133+
drawerId,
134+
storageKey,
135+
defaultWidth,
136+
direction,
137+
className,
138+
detectClickOutside,
139+
isPercentageWidth,
140+
}: DrawerPaneProps) => {
141+
React.useEffect(() => {
142+
return () => {
143+
onCloseDrawer();
144+
};
145+
}, [onCloseDrawer]);
146+
return (
147+
<React.Fragment>
148+
{children}
149+
<DrawerPaneContentWrapper
150+
isVisible={isDrawerVisible}
151+
onClose={onCloseDrawer}
152+
drawerId={drawerId}
153+
storageKey={storageKey}
154+
defaultWidth={defaultWidth}
155+
direction={direction}
156+
className={className}
157+
detectClickOutside={detectClickOutside}
158+
isPercentageWidth={isPercentageWidth}
159+
>
160+
{renderDrawerContent()}
161+
</DrawerPaneContentWrapper>
162+
</React.Fragment>
163+
);
224164
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
3+
import {cn} from '../../utils/cn';
4+
5+
const b = cn('ydb-drawer');
6+
7+
export interface DrawerContextType {
8+
containerWidth: number;
9+
setContainerWidth: React.Dispatch<React.SetStateAction<number>>;
10+
}
11+
12+
const DrawerContext = React.createContext<DrawerContextType>({
13+
containerWidth: 0,
14+
setContainerWidth: () => {},
15+
});
16+
17+
interface DrawerContextProviderProps {
18+
children: React.ReactNode;
19+
className?: string;
20+
}
21+
22+
export const DrawerContextProvider = ({children, className}: DrawerContextProviderProps) => {
23+
const [containerWidth, setContainerWidth] = React.useState(0);
24+
const containerRef = React.useRef<HTMLDivElement>(null);
25+
26+
React.useEffect(() => {
27+
if (!containerRef.current) {
28+
return undefined;
29+
}
30+
31+
const updateWidth = () => {
32+
if (containerRef.current) {
33+
setContainerWidth(containerRef.current.clientWidth);
34+
}
35+
};
36+
37+
// Set initial width
38+
updateWidth();
39+
40+
// Update width on resize
41+
const resizeObserver = new ResizeObserver(updateWidth);
42+
resizeObserver.observe(containerRef.current);
43+
44+
return () => {
45+
if (containerRef.current) {
46+
resizeObserver.disconnect();
47+
}
48+
};
49+
}, []);
50+
51+
// Memoize the context value to prevent unnecessary re-renders
52+
const value = React.useMemo(
53+
() => ({
54+
containerWidth,
55+
setContainerWidth,
56+
}),
57+
[containerWidth],
58+
);
59+
60+
return (
61+
<DrawerContext.Provider value={value}>
62+
<div ref={containerRef} className={b('drawer-container', className)}>
63+
{children}
64+
</div>
65+
</DrawerContext.Provider>
66+
);
67+
};
68+
69+
export const useDrawerContext = (): DrawerContextType => {
70+
const context = React.useContext(DrawerContext);
71+
72+
if (context === undefined) {
73+
throw Error('useDrawerContext must be used within a DrawerContextProvider');
74+
}
75+
76+
return context;
77+
};

src/components/Drawer/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export {DrawerWrapper} from './Drawer';
2+
export {useDrawerContext} from './DrawerContext';
3+
export type {DrawerContextType} from './DrawerContext';

src/containers/Tenant/Diagnostics/Diagnostics.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {Link} from 'react-router-dom';
66
import {StringParam, useQueryParams} from 'use-query-params';
77

88
import {AutoRefreshControl} from '../../../components/AutoRefreshControl/AutoRefreshControl';
9+
import {DrawerContextProvider} from '../../../components/Drawer/DrawerContext';
910
import {useFeatureFlagsAvailable} from '../../../store/reducers/capabilities/hooks';
1011
import {TENANT_DIAGNOSTICS_TABS_IDS} from '../../../store/reducers/tenant/constants';
1112
import {setDiagnosticsTab} from '../../../store/reducers/tenant/tenant';
1213
import type {AdditionalNodesProps, AdditionalTenantsProps} from '../../../types/additionalProps';
1314
import type {EPathType} from '../../../types/api/schema';
1415
import {cn} from '../../../utils/cn';
1516
import {useTypedDispatch, useTypedSelector} from '../../../utils/hooks';
16-
import {Drawer} from '../../Drawer/Drawer';
1717
import {Heatmap} from '../../Heatmap';
1818
import {Nodes} from '../../Nodes/Nodes';
1919
import {Operations} from '../../Operations';
@@ -195,11 +195,11 @@ function Diagnostics(props: DiagnosticsProps) {
195195
</Helmet>
196196
) : null}
197197
{renderTabs()}
198-
<Drawer.Container>
198+
<DrawerContextProvider>
199199
<div className={b('page-wrapper')} ref={containerRef}>
200200
{renderTabContent()}
201201
</div>
202-
</Drawer.Container>
202+
</DrawerContextProvider>
203203
</div>
204204
);
205205
}

0 commit comments

Comments
 (0)