import * as React from "react"; import { Canvas, Group } from "@shopify/react-native-skia"; import { StyleSheet, View, type ViewStyle, type StyleProp, type LayoutChangeEvent, } from "react-native"; import { Gesture, GestureHandlerRootView } from "react-native-gesture-handler"; import { type ContextBridge, FiberProvider, useContextBridge } from "its-fine"; import { PolarChartProvider } from "./contexts/PolarChartContext"; import type { ColorFields, InputFields, NumericalFields, StringKeyOf, } from "../types"; import { type ChartTransformState } from "../cartesian/hooks/useChartTransformState"; import { panTransformGesture, pinchTransformGesture, } from "../cartesian/utils/transformGestures"; import { GestureHandler } from "../shared/GestureHandler"; type PolarChartBaseProps = { onLayout: ({ nativeEvent: { layout } }: LayoutChangeEvent) => void; hasMeasuredLayoutSize: boolean; canvasSize: { width: number; height: number }; containerStyle?: StyleProp; canvasStyle?: StyleProp; transformState?: ChartTransformState; }; const PolarChartBase = ( props: React.PropsWithChildren, ) => { const { containerStyle, canvasStyle, children, onLayout, hasMeasuredLayoutSize, canvasSize, transformState, } = props; const { width, height } = canvasSize; const Bridge: ContextBridge = useContextBridge(); let composed = Gesture.Race(); if (transformState) { composed = Gesture.Race( composed, pinchTransformGesture(transformState), panTransformGesture(transformState), ); } return ( {children} ); }; type PolarChartProps< RawData extends Record, LabelKey extends StringKeyOf>, ValueKey extends StringKeyOf>, ColorKey extends StringKeyOf>, > = { data: RawData[]; colorKey: ColorKey; labelKey: LabelKey; valueKey: ValueKey; } & Omit< PolarChartBaseProps, "canvasSize" | "onLayout" | "hasMeasuredLayoutSize" // omit exposing internal props for calculating canvas layout/size >; export const PolarChart = < RawData extends Record, LabelKey extends StringKeyOf>, ValueKey extends StringKeyOf>, ColorKey extends StringKeyOf>, >( props: React.PropsWithChildren< PolarChartProps >, ) => { const { data, labelKey, colorKey, valueKey } = props; const [canvasSize, setCanvasSize] = React.useState({ width: 0, height: 0 }); const [hasMeasuredLayoutSize, setHasMeasuredLayoutSize] = React.useState(false); const onLayout = React.useCallback( ({ nativeEvent: { layout } }: LayoutChangeEvent) => { setHasMeasuredLayoutSize(true); setCanvasSize(layout); }, [], ); return ( ); }; const styles = StyleSheet.create({ baseContainer: { flex: 1, }, canvasContainer: { flex: 1, }, });