All files / src/components CalendarView.tsx

0% Statements 0/222
0% Branches 0/1
0% Functions 0/1
0% Lines 0/222

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import { useState, useMemo } from 'react';
import { ChevronLeft, ChevronRight } from 'lucide-react';
import type { Archive } from '../api/client';
import { api } from '../api/client';
 
interface CalendarViewProps {
  archives: Archive[];
  onArchiveClick?: (archive: Archive) => void;
}
 
function getDaysInMonth(year: number, month: number): number {
  return new Date(year, month + 1, 0).getDate();
}
 
function getFirstDayOfMonth(year: number, month: number): number {
  return new Date(year, month, 1).getDay();
}
 
const MONTH_NAMES = [
  'January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December'
];
 
const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
 
export function CalendarView({ archives, onArchiveClick }: CalendarViewProps) {
  const today = new Date();
  const [currentMonth, setCurrentMonth] = useState(today.getMonth());
  const [currentYear, setCurrentYear] = useState(today.getFullYear());
  const [selectedDate, setSelectedDate] = useState<string | null>(null);
 
  // Group archives by date
  const archivesByDate = useMemo(() => {
    const map = new Map<string, Archive[]>();
    archives.forEach(archive => {
      const date = new Date(archive.completed_at || archive.created_at);
      const key = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
      const existing = map.get(key) || [];
      existing.push(archive);
      map.set(key, existing);
    });
    return map;
  }, [archives]);
 
  const daysInMonth = getDaysInMonth(currentYear, currentMonth);
  const firstDay = getFirstDayOfMonth(currentYear, currentMonth);
 
  const prevMonth = () => {
    if (currentMonth === 0) {
      setCurrentMonth(11);
      setCurrentYear(currentYear - 1);
    } else {
      setCurrentMonth(currentMonth - 1);
    }
  };
 
  const nextMonth = () => {
    if (currentMonth === 11) {
      setCurrentMonth(0);
      setCurrentYear(currentYear + 1);
    } else {
      setCurrentMonth(currentMonth + 1);
    }
  };
 
  const goToToday = () => {
    setCurrentMonth(today.getMonth());
    setCurrentYear(today.getFullYear());
  };
 
  // Build calendar grid
  const calendarDays: (number | null)[] = [];
  for (let i = 0; i < firstDay; i++) {
    calendarDays.push(null);
  }
  for (let day = 1; day <= daysInMonth; day++) {
    calendarDays.push(day);
  }
 
  const selectedArchives = selectedDate ? archivesByDate.get(selectedDate) || [] : [];
 
  return (
    <div className="flex flex-col lg:flex-row gap-6">
      {/* Calendar */}
      <div className="flex-1">
        {/* Header */}
        <div className="flex items-center justify-between mb-4">
          <button
            onClick={prevMonth}
            className="p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors"
          >
            <ChevronLeft className="w-5 h-5 text-bambu-gray" />
          </button>
          <div className="flex items-center gap-3">
            <h2 className="text-lg font-semibold text-white">
              {MONTH_NAMES[currentMonth]} {currentYear}
            </h2>
            <button
              onClick={goToToday}
              className="px-2 py-1 text-xs bg-bambu-dark-tertiary hover:bg-bambu-green/20 text-bambu-gray hover:text-white rounded transition-colors"
            >
              Today
            </button>
          </div>
          <button
            onClick={nextMonth}
            className="p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors"
          >
            <ChevronRight className="w-5 h-5 text-bambu-gray" />
          </button>
        </div>
 
        {/* Day headers */}
        <div className="grid grid-cols-7 gap-1 mb-1">
          {DAY_NAMES.map(day => (
            <div key={day} className="text-center text-xs text-bambu-gray py-2">
              {day}
            </div>
          ))}
        </div>
 
        {/* Calendar grid */}
        <div className="grid grid-cols-7 gap-1">
          {calendarDays.map((day, index) => {
            if (day === null) {
              return <div key={`empty-${index}`} className="aspect-square" />;
            }
 
            const dateKey = `${currentYear}-${String(currentMonth + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
            const dayArchives = archivesByDate.get(dateKey) || [];
            const hasArchives = dayArchives.length > 0;
            const isToday = day === today.getDate() && currentMonth === today.getMonth() && currentYear === today.getFullYear();
            const isSelected = dateKey === selectedDate;
            const successCount = dayArchives.filter(a => a.status === 'completed').length;
            const failedCount = dayArchives.filter(a => a.status === 'failed').length;
 
            return (
              <button
                key={day}
                onClick={() => setSelectedDate(isSelected ? null : dateKey)}
                className={`aspect-square rounded-lg p-1 flex flex-col items-center justify-center transition-colors relative ${
                  isSelected
                    ? 'bg-bambu-green text-white'
                    : isToday
                    ? 'bg-bambu-green/20 text-white ring-2 ring-bambu-green'
                    : hasArchives
                    ? 'bg-bambu-dark-tertiary hover:bg-bambu-dark-tertiary/70 text-white'
                    : 'hover:bg-bambu-dark-tertiary/50 text-bambu-gray'
                }`}
              >
                <span className={`text-sm font-medium ${isToday && !isSelected ? 'text-bambu-green' : ''}`}>
                  {day}
                </span>
                {hasArchives && (
                  <div className="absolute bottom-1 left-1/2 -translate-x-1/2 flex items-center gap-1">
                    <div className={`w-2 h-2 rounded-full ${
                      failedCount > 0 && successCount === 0
                        ? 'bg-red-400'
                        : failedCount > 0
                        ? 'bg-yellow-400'
                        : 'bg-green-400'
                    }`} />
                    <span className="text-xs font-medium">{dayArchives.length}</span>
                  </div>
                )}
              </button>
            );
          })}
        </div>
 
        {/* Monthly stats */}
        <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary">
          <div className="grid grid-cols-3 gap-4 text-center">
            <div>
              <div className="text-2xl font-bold text-white">
                {archives.filter(a => {
                  const d = new Date(a.completed_at || a.created_at);
                  return d.getMonth() === currentMonth && d.getFullYear() === currentYear;
                }).length}
              </div>
              <div className="text-xs text-bambu-gray">Prints this month</div>
            </div>
            <div>
              <div className="text-2xl font-bold text-green-400">
                {archives.filter(a => {
                  const d = new Date(a.completed_at || a.created_at);
                  return d.getMonth() === currentMonth && d.getFullYear() === currentYear && a.status === 'completed';
                }).length}
              </div>
              <div className="text-xs text-bambu-gray">Successful</div>
            </div>
            <div>
              <div className="text-2xl font-bold text-red-400">
                {archives.filter(a => {
                  const d = new Date(a.completed_at || a.created_at);
                  return d.getMonth() === currentMonth && d.getFullYear() === currentYear && a.status === 'failed';
                }).length}
              </div>
              <div className="text-xs text-bambu-gray">Failed</div>
            </div>
          </div>
        </div>
      </div>
 
      {/* Selected day details */}
      <div className="lg:w-80 bg-bambu-dark rounded-xl p-4">
        {selectedDate ? (
          <>
            <h3 className="text-sm font-medium text-bambu-gray mb-3">
              {new Date(selectedDate + 'T12:00:00').toLocaleDateString('en-US', {
                weekday: 'long',
                month: 'long',
                day: 'numeric',
                year: 'numeric'
              })}
            </h3>
            {selectedArchives.length > 0 ? (
              <div className="space-y-2 max-h-96 overflow-y-auto">
                {selectedArchives.map(archive => (
                  <button
                    key={archive.id}
                    onClick={() => onArchiveClick?.(archive)}
                    className="w-full flex items-center gap-3 p-2 rounded-lg hover:bg-bambu-dark-tertiary transition-colors text-left"
                  >
                    {archive.thumbnail_path ? (
                      <img
                        src={api.getArchiveThumbnail(archive.id)}
                        alt=""
                        className="w-12 h-12 rounded object-cover"
                      />
                    ) : (
                      <div className="w-12 h-12 rounded bg-bambu-dark-tertiary flex items-center justify-center">
                        <span className="text-xs text-bambu-gray">3MF</span>
                      </div>
                    )}
                    <div className="flex-1 min-w-0">
                      <p className="text-sm text-white truncate">
                        {archive.print_name || archive.filename}
                      </p>
                      <div className="flex items-center gap-2 text-xs">
                        <span className={archive.status === 'failed' ? 'text-red-400' : 'text-green-400'}>
                          {archive.status === 'failed' ? 'Failed' : 'Completed'}
                        </span>
                        {archive.filament_color && (
                          <div className="flex gap-0.5">
                            {archive.filament_color.split(',').map((color, i) => (
                              <div
                                key={i}
                                className="w-3 h-3 rounded-full border border-white/20"
                                style={{ backgroundColor: color }}
                              />
                            ))}
                          </div>
                        )}
                      </div>
                    </div>
                  </button>
                ))}
              </div>
            ) : (
              <p className="text-sm text-bambu-gray">No prints on this day</p>
            )}
          </>
        ) : (
          <div className="text-center py-8">
            <p className="text-sm text-bambu-gray">Select a day to see prints</p>
          </div>
        )}
      </div>
    </div>
  );
}