eletrotupi / tcc/ frontend/components/insights/TriggerPatternWidget.tsxafa605370e2af884a10edffbb025e6ee263ed462
2.1 KB | 92 lines Raw
1
import React from "react";
2
import { View, StyleSheet, ActivityIndicator } from "react-native";
3
import { Text } from "@/components/ui/Text";
4
import { MaterialIcons } from "@expo/vector-icons";
5
import { Card } from "@/components/ui/Cards";
6
import { useInsight } from "@/hooks/useInsights.queries";
7
import { Colors, Spacing } from "@/constants/theme";
8
import { getTriggerCategory } from "@/constants/trigger-categories";
9
import { InsightCard } from "./InsightCard";
10

11
export const TriggerPatternWidget: React.FC = () => {
12
  const { data: insight, isLoading } = useInsight("TRIGGER_PATTERN");
13

14
  if (isLoading) {
15
    return (
16
      <Card style={styles.container}>
17
        <ActivityIndicator />
18
      </Card>
19
    );
20
  }
21

22
  if (!insight) {
23
    return (
24
      <InsightCard
25
        category="Gatilhos"
26
        body={
27
          <Text style={styles.empty}>
28
            Registre gatilhos com mais frequência para identificar padrões.
29
          </Text>
30
        }
31
        accent="neutral"
32
        metric="—"
33
      />
34
    );
35
  }
36

37
  const metadata = insight.metadata as {
38
    topCategory: string;
39
    topCount: number;
40
    total: number;
41
    distribution: Record<string, number>;
42
  };
43

44
  const pct =
45
    metadata.total > 0
46
      ? Math.round((metadata.topCount / metadata.total) * 100)
47
      : 0;
48

49
  const topMeta = getTriggerCategory(metadata.topCategory);
50

51
  return (
52
    <InsightCard
53
      category="Gatilhos"
54
      body={
55
        <View style={styles.bodyRow}>
56
          <Text style={styles.bodyText}>{insight.body}</Text>
57
        </View>
58
      }
59
      metric={`${pct}%`}
60
      accent="neutral"
61
      footnote="Percentual da categoria de gatilho mais frequente no período."
62
    />
63
  );
64
};
65

66
const styles = StyleSheet.create({
67
  container: {
68
    padding: Spacing.cardGap,
69
  },
70
  bodyRow: {
71
    flexDirection: "row",
72
    alignItems: "center",
73
    gap: 6,
74
  },
75
  iconWrap: {
76
    width: 22,
77
    height: 22,
78
    borderRadius: 11,
79
    alignItems: "center",
80
    justifyContent: "center",
81
  },
82
  bodyText: {
83
    flex: 1,
84
    fontSize: 14,
85
    color: Colors.light.text,
86
    lineHeight: 20,
87
  },
88
  empty: {
89
    color: Colors.light.lightSlateGray,
90
    marginTop: 4,
91
  },
92
});