Massive/ViewBest.tsx

77 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-07-09 01:48:45 +00:00
import * as shape from 'd3-shape';
2022-07-08 12:11:10 +00:00
import React, {useContext, useEffect, useState} from 'react';
2022-07-09 01:48:45 +00:00
import {View} from 'react-native';
2022-07-08 12:11:10 +00:00
import {Button, Dialog, Portal} from 'react-native-paper';
2022-07-09 01:48:45 +00:00
import {Grid, LineChart, YAxis} from 'react-native-svg-charts';
2022-07-08 12:11:10 +00:00
import {DatabaseContext} from './App';
import Best from './best';
export default function ViewBest({
best,
setBest,
}: {
best?: Best;
setBest: (best?: Best) => void;
}) {
const [data, setData] = useState<number[]>([]);
const [unit, setUnit] = useState<string>();
2022-07-08 12:11:10 +00:00
const db = useContext(DatabaseContext);
useEffect(() => {
const selectBest = `
SELECT max(weight) AS weight, STRFTIME('%Y-%m-%d', created) as created, unit
FROM sets
WHERE name = ?
GROUP BY name, STRFTIME('%Y-%m-%d', created)
`;
const refresh = async () => {
const [result] = await db.executeSql(selectBest, [best?.name]);
if (result.rows.length === 0) return;
console.log(`${ViewBest.name}.${refresh.name}:`, result.rows.raw());
setData(result.rows.raw().map(row => row.weight));
setUnit(result.rows.item(0).unit);
};
2022-07-08 12:11:10 +00:00
refresh();
2022-07-09 01:27:19 +00:00
}, [best, db]);
2022-07-08 12:11:10 +00:00
const contentInset = {top: 20, bottom: 20};
2022-07-08 12:11:10 +00:00
return (
<Portal>
<Dialog visible={!!best} onDismiss={() => setBest(undefined)}>
<Dialog.Title>{best?.name}</Dialog.Title>
<Dialog.Content>
<View style={{height: 200, flexDirection: 'row'}}>
<YAxis
data={data}
contentInset={contentInset}
svg={{
fill: 'grey',
fontSize: 10,
}}
numberOfTicks={10}
formatLabel={value => `${value}${unit}`}
/>
<LineChart
style={{flex: 1, marginLeft: 16}}
data={data}
svg={{stroke: 'rgb(134, 65, 244)'}}
curve={shape.curveNatural}
contentInset={contentInset}>
<Grid />
</LineChart>
</View>
2022-07-08 12:11:10 +00:00
</Dialog.Content>
<Dialog.Actions>
2022-07-09 01:27:19 +00:00
<Button
mode="contained"
icon="close"
onPress={() => setBest(undefined)}>
2022-07-08 12:11:10 +00:00
Close
</Button>
</Dialog.Actions>
</Dialog>
</Portal>
);
}