UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

1 lines 2.96 kB
{"version":3,"sources":["../src/utils/calendarActivityUtils.ts"],"sourcesContent":["import dayjs from 'dayjs';\nimport { CalendarActivityStatus } from '../types/activities';\n\n/**\n * Shared, timezone-safe helpers for the painel calendar + activities list.\n *\n * Deadlines (`finalDate`) are stored in UTC, and a \"23:59 local\" deadline is\n * persisted as \"02:59Z\" on the NEXT calendar day. The calendar dot, the list\n * grouping and the date filter must all key off the SAME local calendar day —\n * never the raw UTC day (`finalDate.split('T')[0]`), which can be one day ahead\n * and makes a day's own activity disappear when that day is selected.\n *\n * These live in the lib so aluno and professor share one source of truth.\n */\n\n/** Minimal shape needed by the date helpers. */\nexport interface DatedActivity {\n finalDate?: string | null;\n}\n\n/**\n * Local (UTC-3 in production) calendar-day key for an activity's `finalDate`,\n * formatted as 'YYYY-MM-DD'. Locale-independent.\n */\nexport function getActivityDateKey(finalDate: string): string {\n return dayjs(finalDate).format('YYYY-MM-DD');\n}\n\n/**\n * Calendar dot status derived from how many days remain until the deadline.\n * `now` is injectable for testing.\n */\nexport function getCalendarActivityStatus(\n finalDate: string,\n now: Date = new Date()\n): CalendarActivityStatus {\n const deadlineMs = new Date(finalDate).getTime();\n const nowMs = now.getTime();\n\n // Compare timestamps first: a deadline already in the past is OVERDUE even if\n // it passed only a few hours ago. (Math.ceil on a small negative delta would\n // round to 0 and misclassify it as NEAR_DEADLINE.)\n if (deadlineMs < nowMs) {\n return CalendarActivityStatus.OVERDUE;\n }\n\n const diffDays = Math.ceil((deadlineMs - nowMs) / (1000 * 60 * 60 * 24));\n if (diffDays <= 3) {\n return CalendarActivityStatus.NEAR_DEADLINE;\n }\n return CalendarActivityStatus.IN_DEADLINE;\n}\n\n/**\n * Keep only activities whose `finalDate` (local day) is on or after `baseDate`\n * ('YYYY-MM-DD'). Activities without a `finalDate` are dropped. Comparing the\n * local day strings keeps this consistent with the calendar dots and grouping.\n */\nexport function filterActivitiesFromDate<T extends DatedActivity>(\n activities: T[],\n baseDate: string\n): T[] {\n return activities.filter((activity) =>\n activity.finalDate\n ? getActivityDateKey(activity.finalDate) >= baseDate\n : false\n );\n}\n"],"mappings":";AAAA,OAAO,WAAW;AAwBX,SAAS,mBAAmB,WAA2B;AAC5D,SAAO,MAAM,SAAS,EAAE,OAAO,YAAY;AAC7C;AAMO,SAAS,0BACd,WACA,MAAY,oBAAI,KAAK,GACG;AACxB,QAAM,aAAa,IAAI,KAAK,SAAS,EAAE,QAAQ;AAC/C,QAAM,QAAQ,IAAI,QAAQ;AAK1B,MAAI,aAAa,OAAO;AACtB;AAAA,EACF;AAEA,QAAM,WAAW,KAAK,MAAM,aAAa,UAAU,MAAO,KAAK,KAAK,GAAG;AACvE,MAAI,YAAY,GAAG;AACjB;AAAA,EACF;AACA;AACF;AAOO,SAAS,yBACd,YACA,UACK;AACL,SAAO,WAAW;AAAA,IAAO,CAAC,aACxB,SAAS,YACL,mBAAmB,SAAS,SAAS,KAAK,WAC1C;AAAA,EACN;AACF;","names":[]}