| 1 | import React from "react";
|
| 2 | import { Text, StyleSheet, ActivityIndicator } from "react-native";
|
| 3 | import { Card } from "@/components/ui/Cards";
|
| 4 | import { ThemedText } from "@/components/misc/themed-text";
|
| 5 | import { useInsight } from "@/hooks/useInsights.queries";
|
| 6 | import { Spacing, Typography, Colors } from "@/constants/theme";
|
| 7 | import { InsightCard, MetricAccent } from "./InsightCard";
|
| 8 |
|
| 9 | export const EnergySleepWidget: React.FC = () => {
|
| 10 | const { data: insight, isLoading } = useInsight("ENERGY_SLEEP_CORRELATION");
|
| 11 |
|
| 12 | if (isLoading) {
|
| 13 | return (
|
| 14 | <Card style={styles.container}>
|
| 15 | <ActivityIndicator />
|
| 16 | </Card>
|
| 17 | );
|
| 18 | }
|
| 19 |
|
| 20 | if (!insight) {
|
| 21 | return (
|
| 22 | <InsightCard
|
| 23 | category="Energia"
|
| 24 | body={
|
| 25 | <Text style={styles.empty}>
|
| 26 | Registre sono e humor para ver correlações.
|
| 27 | </Text>
|
| 28 | }
|
| 29 | accent="neutral"
|
| 30 | metric="—"
|
| 31 | />
|
| 32 | );
|
| 33 | }
|
| 34 |
|
| 35 | const meta = insight.metadata as { correlationScore: number; sampleSize: number };
|
| 36 | const { label, accent } = resolveCorrelation(meta.correlationScore, meta.sampleSize);
|
| 37 |
|
| 38 | return (
|
| 39 | <InsightCard
|
| 40 | category="Energia"
|
| 41 | body={insight.body}
|
| 42 | metric={label}
|
| 43 | accent={accent}
|
| 44 | />
|
| 45 | );
|
| 46 | };
|
| 47 |
|
| 48 | function resolveCorrelation(
|
| 49 | score: number,
|
| 50 | sampleSize: number,
|
| 51 | ): { label: string; accent: MetricAccent } {
|
| 52 | const pct = Math.round(Math.abs(score) * 100);
|
| 53 | const lowData = sampleSize < 5;
|
| 54 |
|
| 55 | if (score > 0.6)
|
| 56 | return { label: lowData ? "Alta*" : `+${pct}%`, accent: "green" };
|
| 57 | if (score > 0.3)
|
| 58 | return { label: lowData ? "Média*" : `+${pct}%`, accent: "green" };
|
| 59 | if (score >= -0.3)
|
| 60 | return { label: "~0%", accent: "neutral" };
|
| 61 | return { label: `-${pct}%`, accent: "purple" };
|
| 62 | }
|
| 63 |
|
| 64 | const styles = StyleSheet.create({
|
| 65 | container: {
|
| 66 | flex: 1,
|
| 67 | padding: Spacing.cardGap,
|
| 68 | },
|
| 69 | label: {
|
| 70 | ...Typography.labelSm,
|
| 71 | opacity: 0.6,
|
| 72 | },
|
| 73 | empty: {
|
| 74 | color: Colors.light.lightSlateGray,
|
| 75 | marginTop: 4,
|
| 76 | },
|
| 77 | });
|