File size: 5,379 Bytes
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Box, Group, Stack, Text, useComputedColorScheme } from "@mantine/core";
import { IconFlame } from "@tabler/icons-react";
import { type ActivityData, intensityLevel } from "@/modules/searchActivity";

interface ActivityHeatmapProps {
  data: ActivityData;
  compact?: boolean;
}

const WEEKDAYS = [
  { key: "sun", label: "" },
  { key: "mon", label: "Mon" },
  { key: "tue", label: "" },
  { key: "wed", label: "Wed" },
  { key: "thu", label: "" },
  { key: "fri", label: "Fri" },
  { key: "sat", label: "" },
];

export default function ActivityHeatmap({
  data,
  compact = false,
}: ActivityHeatmapProps) {
  const scheme = useComputedColorScheme("light");
  const { columns, monthLabels, stats, maxCount, weeks } = data;

  const gap = compact ? 2 : 3;
  const labelWidth = compact ? 24 : 28;
  const labelSize = compact ? 10 : 11;

  const empty =
    scheme === "dark"
      ? "var(--mantine-color-dark-5)"
      : "var(--mantine-color-gray-2)";
  const ramp =
    scheme === "dark"
      ? [
          "var(--mantine-color-blue-9)",
          "var(--mantine-color-blue-7)",
          "var(--mantine-color-blue-5)",
          "var(--mantine-color-blue-4)",
        ]
      : [
          "var(--mantine-color-blue-2)",
          "var(--mantine-color-blue-4)",
          "var(--mantine-color-blue-6)",
          "var(--mantine-color-blue-8)",
        ];
  const colorFor = (level: number) => (level === 0 ? empty : ramp[level - 1]);

  return (
    <Stack gap={compact ? "xs" : "sm"}>
      <Group justify="space-between" align="baseline">
        <Group gap={compact ? "md" : "lg"}>
          <Stack gap={0}>
            <Group gap={4} align="center">
              {stats.currentStreak > 0 && (
                <IconFlame
                  size={compact ? 14 : 16}
                  color="var(--mantine-color-orange-6)"
                />
              )}
              <Text fw={700} size={compact ? "sm" : "md"}>
                {stats.currentStreak}d
              </Text>
            </Group>
            <Text size="xs" c="dimmed">
              Current streak
            </Text>
          </Stack>
          <Stack gap={0}>
            <Text fw={700} size={compact ? "sm" : "md"}>
              {stats.longestStreak}d
            </Text>
            <Text size="xs" c="dimmed">
              Longest
            </Text>
          </Stack>
          <Stack gap={0}>
            <Text fw={700} size={compact ? "sm" : "md"}>
              {stats.daysActive}
            </Text>
            <Text size="xs" c="dimmed">
              Active days
            </Text>
          </Stack>
        </Group>
        <Text size="xs" c="dimmed">
          Last {weeks} weeks
        </Text>
      </Group>

      <Box>
        <Group gap={gap} wrap="nowrap" mb={2} style={{ alignItems: "stretch" }}>
          <Box w={labelWidth} style={{ flexShrink: 0 }} />
          {columns.map((column, colIndex) => (
            <Text
              key={`month-${column[0]?.date ?? colIndex}`}
              fz={labelSize}
              c="dimmed"
              style={{
                flex: 1,
                whiteSpace: "nowrap",
                overflow: "visible",
              }}
            >
              {monthLabels[colIndex]}
            </Text>
          ))}
        </Group>

        <Group gap={gap} wrap="nowrap" style={{ alignItems: "stretch" }}>
          <Stack
            gap={gap}
            w={labelWidth}
            style={{ flexShrink: 0 }}
            justify="space-between"
          >
            {WEEKDAYS.map((weekday) => (
              <Text
                key={weekday.key}
                fz={labelSize}
                c="dimmed"
                style={{
                  flex: 1,
                  display: "flex",
                  alignItems: "center",
                  lineHeight: 1,
                }}
              >
                {weekday.label}
              </Text>
            ))}
          </Stack>

          {columns.map((column, colIndex) => (
            <Stack
              key={`col-${column[0]?.date ?? colIndex}`}
              gap={gap}
              style={{ flex: 1 }}
            >
              {column.map((cell) => (
                <Box
                  key={cell.date}
                  title={
                    cell.future
                      ? undefined
                      : `${cell.date}: ${cell.count} ${
                          cell.count === 1 ? "search" : "searches"
                        }`
                  }
                  style={{
                    aspectRatio: "1 / 1",
                    borderRadius: 2,
                    backgroundColor: cell.future
                      ? "transparent"
                      : colorFor(intensityLevel(cell.count, maxCount)),
                  }}
                />
              ))}
            </Stack>
          ))}
        </Group>
      </Box>

      <Group gap={6} justify="flex-end" align="center">
        <Text size="xs" c="dimmed">
          Less
        </Text>
        {[0, 1, 2, 3, 4].map((level) => (
          <Box
            key={`legend-${level}`}
            w={compact ? 10 : 12}
            h={compact ? 10 : 12}
            style={{ borderRadius: 2, backgroundColor: colorFor(level) }}
          />
        ))}
        <Text size="xs" c="dimmed">
          More
        </Text>
      </Group>
    </Stack>
  );
}