Skip to content

Commit 80969eb

Browse files
authored
fix: Crash intermittently when creating lightweight charts (#12480)
Crash in canvas library intermittently `throw new Error('No window is associated with the canvas');` <!-- Before opening a pull request, please read the [contributing guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md) first --> <!-- start pr-codex --> --- ## PR-Codex overview This PR primarily focuses on enhancing the safety checks in various `useEffect` hooks across multiple components by ensuring that `chartRef.current?.ownerDocument?.defaultView` is defined before proceeding with chart creation. ### Detailed summary - Updated `useEffect` in `PairPriceChart.tsx` to check for `chartRef.current?.ownerDocument?.defaultView`. - Similar updates made in `ChainlinkChart.tsx`, `BarChart/index.tsx`, `CandleChart/index.tsx`, `LineChart/index.tsx`, and other chart-related components. - Added checks for `chartRef.current?.ownerDocument?.defaultView` in `DropdownMenu.tsx` and `V3Info` charts to ensure proper chart initialization. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 94ccf41 commit 80969eb

File tree

9 files changed

+15
-9
lines changed

9 files changed

+15
-9
lines changed

apps/web/src/views/Info/components/InfoCharts/BarChart/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const Chart = ({ data, setHoverValue, setHoverDate }: LineChartProps) => {
3636
}, [data])
3737

3838
useEffect(() => {
39-
if (!chartRef?.current || !transformedData || transformedData.length === 0) return
39+
if (!chartRef.current?.ownerDocument?.defaultView || !transformedData || transformedData.length === 0) return
4040

4141
const chart = createChart(chartRef?.current, {
4242
layout: {

apps/web/src/views/Info/components/InfoCharts/CandleChart/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const CandleChart = ({ data, setValue, setLabel }: LineChartProps) => {
2424

2525
// if chart not instantiated in canvas, create it
2626
useEffect(() => {
27-
if (!chartRef.current || !data) return
27+
if (!chartRef.current?.ownerDocument?.defaultView || !data) return
2828
const chart = createChart(chartRef.current, {
2929
autoSize: true,
3030
// height: CANDLE_CHART_HEIGHT,

apps/web/src/views/Info/components/InfoCharts/LineChart/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const LineChart = ({ data, setHoverValue, setHoverDate, dateFormat = 'h:mm a' }:
3939
}, [data])
4040

4141
useEffect(() => {
42-
if (!chartRef?.current || !transformedData || transformedData.length === 0) return
42+
if (!chartRef.current?.ownerDocument?.defaultView || !transformedData || transformedData.length === 0) return
4343

4444
const chart = createChart(chartRef?.current, {
4545
layout: {

apps/web/src/views/Predictions/components/ChainlinkChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ const Chart = ({
237237
const [chartCreated, setChart] = useState<IChartApi | undefined>()
238238

239239
useEffect(() => {
240-
if (!chartRef?.current) return
240+
if (!chartRef.current?.ownerDocument?.defaultView) return
241241

242242
const chart = createChart(chartRef?.current, {
243243
layout: {

apps/web/src/views/V3Info/components/BarChart/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const BarChart = ({
8989

9090
// if chart not instantiated in canvas, create it
9191
useEffect(() => {
92-
if (!chartCreated && data && !!chartRef?.current?.parentElement) {
92+
if (!chartCreated && data && !!chartRef?.current?.parentElement && !!chartRef.current?.ownerDocument?.defaultView) {
9393
const chart = createChart(chartRef.current, {
9494
height,
9595
width: chartRef.current.parentElement.clientWidth - 62,

apps/web/src/views/V3Info/components/CandleChart/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ const CandleChart = ({
8181

8282
// if chart not instantiated in canvas, create it
8383
useEffect(() => {
84-
if (!chartCreated && data && data?.length > 0 && !!chartRef?.current?.parentElement) {
84+
if (
85+
!chartCreated &&
86+
data &&
87+
data?.length > 0 &&
88+
!!chartRef?.current?.parentElement &&
89+
!!chartRef.current?.ownerDocument?.defaultView
90+
) {
8591
const chart = createChart(chartRef.current, {
8692
height,
8793
width: chartRef.current.parentElement.clientWidth - 32,

apps/web/src/views/V3Info/components/LineChart/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const LineChart = ({
8787

8888
// if chart not instantiated in canvas, create it
8989
useEffect(() => {
90-
if (!chartCreated && data && !!chartRef?.current?.parentElement) {
90+
if (!chartCreated && data && !!chartRef?.current?.parentElement && !!chartRef.current?.ownerDocument?.defaultView) {
9191
const chart = createChart(chartRef.current, {
9292
height,
9393
width: chartRef.current.parentElement.clientWidth - 32,

packages/uikit/src/components/Chart/PairPriceChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export const PairPriceChart: React.FC<PairPriceChartNewProps> = ({
171171
}, [setHoverValue, setHoverDate, legendRef]);
172172

173173
useEffect(() => {
174-
if (!chartRef?.current) return;
174+
if (!chartRef.current?.ownerDocument?.defaultView) return;
175175

176176
const chart = createChart(chartRef?.current, {
177177
layout: {

packages/uikit/src/components/DropdownMenu/DropdownMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ const DropdownMenu: React.FC<React.PropsWithChildren<DropdownMenuProps>> = ({
229229
});
230230
hideDropdownMenu.cancel();
231231
};
232-
}, [setIsOpen, tooltipRef, targetRef, isBottomNav, hasItems]);
232+
}, [setIsOpen, tooltipRef, targetRef, isBottomNav, hasItems, trigger]);
233233

234234
useEffect(() => {
235235
if (setMenuOpenByIndex && index !== undefined) {

0 commit comments

Comments
 (0)