From 163666d396e409d29ef79bfef47de1846e16a532 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Tue, 20 Sep 2022 11:00:01 +0200 Subject: [PATCH] fix: Map offset on edge --- app/components/chart-footnotes.tsx | 4 +- app/components/chart-preview.tsx | 60 ++++++++++++++------------ app/components/chart-published.tsx | 28 ++++++------ app/components/chart-table-preview.tsx | 3 +- 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/app/components/chart-footnotes.tsx b/app/components/chart-footnotes.tsx index 55ac6181f..dd567d79c 100644 --- a/app/components/chart-footnotes.tsx +++ b/app/components/chart-footnotes.tsx @@ -49,11 +49,13 @@ export const ChartFootnotes = ({ dataSource, chartConfig, configKey, + onToggleTableView, }: { dataSetIri: string; dataSource: DataSource; chartConfig: ChartConfig; configKey?: string; + onToggleTableView: () => void; }) => { const classes = useStyles(); const locale = useLocale(); @@ -150,7 +152,7 @@ export const ChartFootnotes = ({ } /> } - onClick={() => setIsChartTablePreview(!isChartTablePreview)} + onClick={onToggleTableView} sx={{ p: 0, typography: "caption" }} > {isChartTablePreview ? ( diff --git a/app/components/chart-preview.tsx b/app/components/chart-preview.tsx index a4fa6a47a..6bf1574b8 100644 --- a/app/components/chart-preview.tsx +++ b/app/components/chart-preview.tsx @@ -2,6 +2,7 @@ import { Trans } from "@lingui/macro"; import { Box, Typography } from "@mui/material"; import Head from "next/head"; import * as React from "react"; +import { useRef } from "react"; import { ChartDataFilters } from "@/charts/shared/chart-data-filters"; import { useQueryFilters } from "@/charts/shared/chart-helpers"; @@ -23,7 +24,7 @@ import { DataSetTable } from "@/configurator/components/datatable"; import { useDataCubeMetadataQuery } from "@/graphql/query-hooks"; import { DataCubePublicationStatus } from "@/graphql/resolver-types"; import { useLocale } from "@/locales/use-locale"; -import { useResizeObserver } from "@/utils/use-resize-observer"; +import useEvent from "@/utils/use-event"; export const ChartPreview = ({ dataSetIri, @@ -57,16 +58,17 @@ export const ChartPreviewInner = ({ locale, }, }); - const [isTablePreview] = useChartTablePreview(); - - const [chartRef, _, height] = useResizeObserver(); - const lastHeight = React.useRef(height); - - React.useEffect(() => { - if (height !== 0) { - lastHeight.current = height; + const [isTablePreview, setIsChartTablePreview] = useChartTablePreview(); + const lastHeight = useRef("auto" as "auto" | number); + const chartTableContainerRef = useRef(); + const handleToggleTableView = useEvent(() => { + if (!chartTableContainerRef.current) { + return; } - }, [height]); + const bcr = chartTableContainerRef.current.getBoundingClientRect(); + lastHeight.current = bcr.height; + return setIsChartTablePreview((c) => !c); + }); return ( - {isTablePreview ? ( - - ) : ( - - )} + + {isTablePreview ? ( + + ) : ( + + )} + {state.chartConfig && ( )} diff --git a/app/components/chart-published.tsx b/app/components/chart-published.tsx index 77c259fd3..bb8ea7e0b 100644 --- a/app/components/chart-published.tsx +++ b/app/components/chart-published.tsx @@ -2,7 +2,7 @@ import { Trans } from "@lingui/macro"; import { Box, Theme, Typography } from "@mui/material"; import { makeStyles } from "@mui/styles"; import * as React from "react"; -import { useEffect, useMemo } from "react"; +import { useEffect, useMemo, useRef } from "react"; import { ChartDataFilters } from "@/charts/shared/chart-data-filters"; import { isUsingImputation } from "@/charts/shared/imputation"; @@ -35,7 +35,7 @@ import { import { useDataCubeMetadataQuery } from "@/graphql/query-hooks"; import { DataCubePublicationStatus } from "@/graphql/resolver-types"; import { useLocale } from "@/locales/use-locale"; -import { useResizeObserver } from "@/utils/use-resize-observer"; +import useEvent from "@/utils/use-event"; export const ChartPublished = ({ dataSet, @@ -102,14 +102,7 @@ export const ChartPublishedInner = ({ }); const [isTablePreview] = useChartTablePreview(); - const [chartRef, _, height] = useResizeObserver(); - const lastHeight = React.useRef(height); - - React.useEffect(() => { - if (height !== 0) { - lastHeight.current = height; - } - }, [height]); + const lastHeight = React.useRef("auto" as "auto" | number); const publishedConfiguratorState = useMemo(() => { return { @@ -119,6 +112,17 @@ export const ChartPublishedInner = ({ } as ConfiguratorStatePublishing; }, [chartConfig, dataSource]); + const [, setIsChartTablePreview] = useChartTablePreview(); + const chartTableContainerRef = useRef(); + const handleToggleTableView = useEvent(() => { + if (!chartTableContainerRef.current) { + return; + } + const bcr = chartTableContainerRef.current.getBoundingClientRect(); + lastHeight.current = bcr.height; + return setIsChartTablePreview((c) => !c); + }); + return ( @@ -175,7 +179,7 @@ export const ChartPublishedInner = ({ )} - + ) : ( )} diff --git a/app/components/chart-table-preview.tsx b/app/components/chart-table-preview.tsx index 613437fdb..250358cfb 100644 --- a/app/components/chart-table-preview.tsx +++ b/app/components/chart-table-preview.tsx @@ -2,12 +2,13 @@ import { createContext, Dispatch, ReactNode, + SetStateAction, useContext, useState, } from "react"; const ChartTablePreviewContext = createContext< - [boolean, Dispatch] | undefined + [boolean, Dispatch>] | undefined >(undefined); export const useChartTablePreview = () => {