Skip to content

Commit e094a34

Browse files
committed
fix: update components to use React imports consistently and handle errors without logging
1 parent b233d7e commit e094a34

File tree

10 files changed

+35
-42
lines changed

10 files changed

+35
-42
lines changed

src/blocks/Slider/Slider.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface SliderProps
4343
initialSlide?: number;
4444
}
4545

46+
// eslint-disable-next-line react-hooks/rules-of-hooks
4647
Swiper.use([Autoplay, A11y, Pagination]);
4748

4849
export const SliderBlock = ({

src/blocks/SliderOld/SliderOld.tsx

+20-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import React, {
2-
Fragment,
3-
useCallback,
4-
useContext,
5-
useEffect,
6-
useMemo,
7-
useRef,
8-
useState,
9-
} from 'react';
1+
import * as React from 'react';
102

113
import {useUniqId} from '@gravity-ui/uikit';
124
import debounce from 'lodash/debounce';
@@ -93,19 +85,19 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
9385
initialIndex = 0,
9486
} = props;
9587

96-
const {isServer} = useContext(SSRContext);
97-
const isMobile = useContext(MobileContext);
98-
const [breakpoint, setBreakpoint] = useState<number>(BREAKPOINTS.xl);
88+
const {isServer} = React.useContext(SSRContext);
89+
const isMobile = React.useContext(MobileContext);
90+
const [breakpoint, setBreakpoint] = React.useState<number>(BREAKPOINTS.xl);
9991
const sliderId = useUniqId();
100-
const disclosedChildren = useMemo<React.ReactElement[]>(
92+
const disclosedChildren = React.useMemo<React.ReactElement[]>(
10193
() => discloseAllNestedChildren(children as React.ReactElement[], sliderId),
10294
[children, sliderId],
10395
);
10496
const childrenCount = disclosedChildren.length;
10597
const isAutoplayEnabled = autoplaySpeed !== undefined && autoplaySpeed > 0;
106-
const isUserInteractionRef = useRef(false);
98+
const isUserInteractionRef = React.useRef(false);
10799

108-
const [slidesToShow] = useState<SliderBreakpointParams>(
100+
const [slidesToShow] = React.useState<SliderBreakpointParams>(
109101
getSlidesToShowWithDefaults({
110102
contentLength: childrenCount,
111103
breakpoints: props.slidesToShow,
@@ -118,11 +110,11 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
118110
const slidesToShowCount = getSlidesToShowCount(slidesToShow);
119111
const slidesCountByBreakpoint = getSlidesCountByBreakpoint(breakpoint, slidesToShow);
120112

121-
const [currentIndex, setCurrentIndex] = useState<number>(initialIndex);
122-
const [childStyles, setChildStyles] = useState<Object>({});
123-
const [slider, setSlider] = useState<SlickSlider>();
124-
const prevIndexRef = useRef<number>(0);
125-
const autoplayTimeId = useRef<Timeout>();
113+
const [currentIndex, setCurrentIndex] = React.useState<number>(initialIndex);
114+
const [childStyles, setChildStyles] = React.useState<Object>({});
115+
const [slider, setSlider] = React.useState<SlickSlider>();
116+
const prevIndexRef = React.useRef<number>(0);
117+
const autoplayTimeId = React.useRef<Timeout>();
126118
const {hasFocus, unsetFocus} = useFocus(slider?.innerSlider?.list);
127119

128120
const asUserInteraction =
@@ -133,7 +125,7 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
133125
};
134126

135127
// eslint-disable-next-line react-hooks/exhaustive-deps
136-
const onResize = useCallback(
128+
const onResize = React.useCallback(
137129
debounce(() => {
138130
if (!slider) {
139131
return;
@@ -151,7 +143,7 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
151143
[slider, breakpoint],
152144
);
153145

154-
const scrollLastSlide = useCallback(
146+
const scrollLastSlide = React.useCallback(
155147
(current: number) => {
156148
const lastSlide = childrenCount - slidesToShowCount;
157149

@@ -173,15 +165,15 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
173165
[autoplaySpeed, childrenCount, isAutoplayEnabled, slider, slidesToShowCount],
174166
);
175167

176-
useEffect(() => {
168+
React.useEffect(() => {
177169
if (hasFocus && autoplayTimeId.current) {
178170
clearTimeout(autoplayTimeId.current);
179171
} else {
180172
scrollLastSlide(currentIndex);
181173
}
182174
}, [currentIndex, hasFocus, scrollLastSlide]);
183175

184-
useEffect(() => {
176+
React.useEffect(() => {
185177
onResize();
186178

187179
window.addEventListener('resize', onResize, {passive: true});
@@ -205,7 +197,7 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
205197
}
206198
};
207199

208-
const onBeforeChange = useCallback(
200+
const onBeforeChange = React.useCallback(
209201
(current: number, next: number) => {
210202
if (handleBeforeChange) {
211203
handleBeforeChange(current, next);
@@ -218,7 +210,7 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
218210
[handleBeforeChange],
219211
);
220212

221-
const onAfterChange = useCallback(
213+
const onAfterChange = React.useCallback(
222214
(current: number) => {
223215
if (handleAfterChange) {
224216
handleAfterChange(current);
@@ -289,7 +281,7 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
289281
const renderAccessibleBar = (index: number) => {
290282
return (
291283
// To have this key differ from keys used in renderDot function, added `-accessible-bar` part
292-
<Fragment key={`${index}-accessible-bar`}>
284+
<React.Fragment key={`${index}-accessible-bar`}>
293285
{slidesCountByBreakpoint > 0 && (
294286
<li
295287
className={b('accessible-bar')}
@@ -306,7 +298,7 @@ export const SliderOldBlock = (props: React.PropsWithChildren<SliderOldProps>) =
306298
{...getRovingItemProps(currentIndex + 1)}
307299
/>
308300
)}
309-
</Fragment>
301+
</React.Fragment>
310302
);
311303
};
312304

src/blocks/SliderOld/utils.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useEffect, useRef, useState} from 'react';
1+
import * as React from 'react';
22

33
import pickBy from 'lodash/pickBy';
44

@@ -113,8 +113,8 @@ export function useRovingTabIndex(props: {
113113
uniqId: string;
114114
}) {
115115
const {itemCount, activeIndex, firstIndex = 0, uniqId} = props;
116-
const [currentIndex, setCurrentIndex] = useState(firstIndex);
117-
const hasFocusRef = useRef(false);
116+
const [currentIndex, setCurrentIndex] = React.useState(firstIndex);
117+
const hasFocusRef = React.useRef(false);
118118
const lastIndex = itemCount + firstIndex - 1;
119119

120120
const getRovingItemProps = (
@@ -130,17 +130,17 @@ export function useRovingTabIndex(props: {
130130
};
131131
};
132132

133-
useEffect(() => {
133+
React.useEffect(() => {
134134
if (!hasFocusRef.current) {
135135
return;
136136
}
137137
document.getElementById(getRovingListItemId(uniqId, currentIndex))?.focus();
138138
}, [activeIndex, currentIndex, uniqId]);
139139

140140
const setNextIndex = () =>
141-
setCurrentIndex((prev) => (prev >= lastIndex ? firstIndex : prev + 1));
141+
setCurrentIndex((prev: number) => (prev >= lastIndex ? firstIndex : prev + 1));
142142
const setPrevIndex = () =>
143-
setCurrentIndex((prev) => (prev <= firstIndex ? lastIndex : prev - 1));
143+
setCurrentIndex((prev: number) => (prev <= firstIndex ? lastIndex : prev - 1));
144144

145145
const onRovingListKeyDown: React.KeyboardEventHandler<HTMLElement> = (e) => {
146146
const key = e.key.toLowerCase();

src/components/VideoBlock/VideoBlock.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const VideoBlock = (props: VideoBlockProps) => {
110110
});
111111

112112
return url.href;
113-
} catch (e) {
113+
} catch {
114114
return src;
115115
}
116116
}

src/components/YandexForm/YandexForm.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const YandexForm = (props: YandexFormProps) => {
118118
// to catch this event and handle analytics redirectUrl is added to condition
119119
handleSubmit();
120120
}
121-
} catch (error) {
121+
} catch {
122122
return;
123123
}
124124
},

src/containers/Loadable/Loadable.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const Loadable = (props: LoadableComponentsProps) => {
5757
error = false;
5858

5959
setDataState({data, loading: false, error});
60-
} catch (ex) {
60+
} catch {
6161
error = true;
6262
setDataState({loading: false, error});
6363
}

src/editor/data/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const getBlockPreview = (blockType: BlockType): PreviewComponent => {
2525
}
2626

2727
return DefaultPreview;
28-
} catch (err) {
28+
} catch {
2929
/*eslint-disable no-console */
3030
console.warn(`Preview image for ${blockType} not found`);
3131
return DefaultPreview;

src/editor/widget/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const getHostStyles = () => {
33
.map((styleSheet) => {
44
try {
55
return [...styleSheet.cssRules].map((rule) => rule.cssText).join('');
6-
} catch (e) {
6+
} catch {
77
//eslint-disable-next-line no-console
88
console.log(`Access to stylesheet ${styleSheet.href} is denied. Ignoring...`);
99

src/navigation/components/NavigationItem/components/GithubButton/GithubButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const GithubButton = ({
3838
if (githubButton.parentNode) {
3939
githubButton.parentNode.replaceChild(el, githubButton);
4040
}
41-
} catch (_) {}
41+
} catch {}
4242
});
4343
}
4444
});

src/utils/blocks.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ReactNode} from 'react';
1+
import * as React from 'react';
22

33
import camelCase from 'lodash/camelCase';
44
import flatten from 'lodash/flatten';
@@ -64,7 +64,7 @@ export function getHeaderTag(size: TextSize) {
6464
type SelectVariantArgs = {
6565
block?: boolean;
6666
content?: string;
67-
children?: ReactNode;
67+
children?: React.ReactNode;
6868
tagName?: TagName;
6969
};
7070

0 commit comments

Comments
 (0)