File size: 3,227 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { TZDate } from "@date-fns/tz";
import { addMonths, formatISO, subMonths } from "date-fns";
import { useTrackerParams } from "@/hooks/use-tracker-params";

export function handleMonthChange(direction: number, currentDate: TZDate) {
  const { setParams } = useTrackerParams();
  const newDate =
    direction > 0 ? addMonths(currentDate, 1) : subMonths(currentDate, 1);
  setParams({
    date: formatISO(newDate, { representation: "date" }),
  });
}

export function checkIsInRange(
  date: TZDate,
  isDragging: boolean,
  localRange: [string | null, string | null],
  range: [string, string] | null,
): boolean {
  if (isDragging && localRange[0] && localRange[1]) {
    const start = new TZDate(localRange[0], "UTC");
    const end = new TZDate(localRange[1], "UTC");
    const minDate = new TZDate(Math.min(start.getTime(), end.getTime()), "UTC");
    const maxDate = new TZDate(Math.max(start.getTime(), end.getTime()), "UTC");
    return date > minDate && date < maxDate;
  }
  if (!isDragging && range && range.length === 2) {
    const start = new TZDate(range[0], "UTC");
    const end = new TZDate(range[1], "UTC");
    const minDate = new TZDate(Math.min(start.getTime(), end.getTime()), "UTC");
    const maxDate = new TZDate(Math.max(start.getTime(), end.getTime()), "UTC");
    return date > minDate && date < maxDate;
  }
  return false;
}

export function checkIsFirstSelectedDate(
  date: TZDate,
  isDragging: boolean,
  localRange: [string | null, string | null],
  range: [string, string] | null,
): boolean {
  const formattedDate = formatISO(date, { representation: "date" });
  if (isDragging && localRange[0]) {
    const start = new TZDate(localRange[0], "UTC");
    const end = localRange[1] ? new TZDate(localRange[1], "UTC") : start;
    const firstDate = new TZDate(
      Math.min(start.getTime(), end.getTime()),
      "UTC",
    );
    return formattedDate === formatISO(firstDate, { representation: "date" });
  }
  if (!isDragging && range && range.length === 2) {
    const start = new TZDate(range[0], "UTC");
    const end = new TZDate(range[1], "UTC");
    const firstDate = new TZDate(
      Math.min(start.getTime(), end.getTime()),
      "UTC",
    );
    return formattedDate === formatISO(firstDate, { representation: "date" });
  }
  return false;
}

export function checkIsLastSelectedDate(
  date: TZDate,
  isDragging: boolean,
  localRange: [string | null, string | null],
  range: [string, string] | null,
): boolean {
  const formattedDate = formatISO(date, { representation: "date" });
  if (isDragging && localRange[0] && localRange[1]) {
    const start = new TZDate(localRange[0], "UTC");
    const end = new TZDate(localRange[1], "UTC");
    const lastDate = new TZDate(
      Math.max(start.getTime(), end.getTime()),
      "UTC",
    );
    return formattedDate === formatISO(lastDate, { representation: "date" });
  }
  if (!isDragging && range && range.length === 2) {
    const start = new TZDate(range[0], "UTC");
    const end = new TZDate(range[1], "UTC");
    const lastDate = new TZDate(
      Math.max(start.getTime(), end.getTime()),
      "UTC",
    );
    return formattedDate === formatISO(lastDate, { representation: "date" });
  }
  return false;
}