import { RouteProp, useFocusEffect, useNavigation, useRoute, } from '@react-navigation/native'; import React, {useCallback, useEffect, useRef, useState} from 'react'; import {FileSystem} from 'react-native-file-access'; import {IconButton} from 'react-native-paper'; import RNPickerSelect from 'react-native-picker-select'; import Share from 'react-native-share'; import ViewShot from 'react-native-view-shot'; import {BestPageParams} from './BestPage'; import Chart from './Chart'; import {getVolumes, getWeights} from './db'; import {Metrics} from './metrics'; import {Periods} from './periods'; import Set from './set'; import {formatMonth} from './time'; import Volume from './volume'; export default function ViewBest() { const {params} = useRoute>(); const [weights, setWeights] = useState([]); const [volumes, setVolumes] = useState([]); const [metric, setMetric] = useState(Metrics.Weight); const [period, setPeriod] = useState(Periods.Monthly); const navigation = useNavigation(); const viewShot = useRef(null); useFocusEffect( useCallback(() => { console.log(`${ViewBest.name}.useFocusEffect`); navigation.getParent()?.setOptions({ headerLeft: () => ( navigation.goBack()} /> ), headerRight: () => ( viewShot.current?.capture?.().then(async uri => { const base64 = await FileSystem.readFile(uri, 'base64'); const url = `data:image/jpeg;base64,${base64}`; Share.open({ message: params.best.name, type: 'image/jpeg', url, failOnCancel: false, }); }) } icon="share-social-outline" /> ), title: params.best.name, }); }, [navigation, params.best]), ); useEffect(() => { if (metric === Metrics.Weight) getWeights(params.best.name, period).then(setWeights); else if (metric === Metrics.Volume) getVolumes(params.best.name, period).then(setVolumes); console.log(`${ViewBest.name}.useEffect`, {metric, period}); }, [params.best.name, metric, period]); return ( {metric === Metrics.Volume && ( v.value)} yFormat={(value: number) => `${value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')}${ volumes[0].unit }` } xData={weights} xFormat={(_value, index) => formatMonth(weights[index].created!)} /> )} {metric === Metrics.Weight && ( set.weight)} yFormat={value => `${value}${weights[0].unit}`} xData={weights} xFormat={(_value, index) => formatMonth(weights[index].created!)} /> )} ); }