Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

121 строка
2.8 KiB

  1. import React from "react";
  2. import {
  3. RecordTimesheetInput,
  4. RecordLeaveInput,
  5. } from "@/app/api/timesheets/actions";
  6. import {
  7. DateCalendar,
  8. LocalizationProvider,
  9. PickersDay,
  10. PickersDayProps,
  11. } from "@mui/x-date-pickers";
  12. import { useTranslation } from "react-i18next";
  13. import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
  14. import dayjs, { Dayjs } from "dayjs";
  15. import "dayjs/locale/zh-hk";
  16. import timezone from "dayjs/plugin/timezone";
  17. import utc from "dayjs/plugin/utc";
  18. import { INPUT_DATE_FORMAT } from "@/app/utils/formatUtil";
  19. dayjs.extend(utc);
  20. dayjs.extend(timezone);
  21. dayjs.tz.guess();
  22. export interface Props {
  23. timesheet: RecordTimesheetInput;
  24. leaves: RecordLeaveInput;
  25. publicHolidays: string[];
  26. onDateSelect: (date: string) => void;
  27. onMonthChange: (day: Dayjs) => void;
  28. }
  29. const getColor = (
  30. hasTimeInput: boolean,
  31. hasLeave: boolean,
  32. isPublicHoliday: boolean,
  33. ): string | undefined => {
  34. if (hasTimeInput && hasLeave) {
  35. return "success.light";
  36. } else if (hasTimeInput) {
  37. return "info.light";
  38. } else if (hasLeave) {
  39. return "warning.light";
  40. } else if (isPublicHoliday){
  41. return "error.light";
  42. }else {
  43. return undefined;
  44. }
  45. };
  46. const EntryDay: React.FC<PickersDayProps<Dayjs> & Props> = ({
  47. timesheet,
  48. leaves,
  49. publicHolidays,
  50. ...pickerProps
  51. }) => {
  52. const timesheetDays = Object.keys(timesheet);
  53. const leaveDays = Object.keys(leaves);
  54. const hasTimesheetInput = timesheetDays.some((day) =>
  55. dayjs(day).isSame(pickerProps.day, "day"),
  56. );
  57. const hasLeaveInput = leaveDays.some((day) =>
  58. dayjs(day).isSame(pickerProps.day, "day"),
  59. );
  60. const isPublicHoliday = publicHolidays.some((day) =>
  61. dayjs(day).isSame(pickerProps.day, "day"),
  62. );
  63. return (
  64. <PickersDay
  65. {...pickerProps}
  66. disabled={!(hasTimesheetInput || hasLeaveInput)}
  67. sx={{ backgroundColor: getColor(hasTimesheetInput, hasLeaveInput, isPublicHoliday) }}
  68. />
  69. );
  70. };
  71. const PastEntryCalendar: React.FC<Props> = ({
  72. timesheet,
  73. leaves,
  74. publicHolidays,
  75. onDateSelect,
  76. onMonthChange,
  77. }) => {
  78. const {
  79. i18n: { language },
  80. } = useTranslation("home");
  81. const onChange = (day: Dayjs) => {
  82. onDateSelect(day.format(INPUT_DATE_FORMAT));
  83. };
  84. return (
  85. <LocalizationProvider
  86. dateAdapter={AdapterDayjs}
  87. adapterLocale={`${language}-hk`}
  88. >
  89. <DateCalendar
  90. onChange={onChange}
  91. onMonthChange={onMonthChange}
  92. disableFuture
  93. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  94. slots={{ day: EntryDay as any }}
  95. slotProps={{
  96. day: {
  97. timesheet,
  98. leaves,
  99. publicHolidays,
  100. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  101. } as any,
  102. }}
  103. />
  104. </LocalizationProvider>
  105. );
  106. };
  107. export default PastEntryCalendar;