Skip to content

Commit

Permalink
Merge pull request #177 from melfore/175-grid-improve-display-of-times
Browse files Browse the repository at this point in the history
[Grid] Improve display of time
  • Loading branch information
CrisGrud committed Jan 18, 2024
2 parents 0217753 + a6f352f commit 653296e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/grid/Cell/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { memo, useMemo } from "react";
import { Interval } from "luxon";

import { KonvaGroup, KonvaLine, KonvaRect, KonvaText } from "../../@konva";
import { KonvaGroup, KonvaLine, KonvaText } from "../../@konva";
import { useTimelineContext } from "../../timeline/TimelineContext";
import { DEFAULT_STROKE_DARK_MODE, DEFAULT_STROKE_LIGHT_MODE } from "../../utils/theme";
import { displayInterval } from "../../utils/time-resolution";
Expand All @@ -22,10 +22,14 @@ const GridCell = ({ column, height, index, hourInfo: visibleDayInfo }: GridCellP
columnWidth,
resolution: { unit: resolutionUnit },
rowHeight,
dateLocale,
theme: { color: themeColor },
} = useTimelineContext();

const cellLabel = useMemo(() => displayInterval(column, resolutionUnit), [column, resolutionUnit]);
const cellLabel = useMemo(
() => displayInterval(column, resolutionUnit, dateLocale!),
[column, resolutionUnit, dateLocale]
);

const xPos = useMemo(() => {
if (resolutionUnit === "day") {
Expand Down Expand Up @@ -58,12 +62,10 @@ const GridCell = ({ column, height, index, hourInfo: visibleDayInfo }: GridCellP
}
return DEFAULT_STROKE_DARK_MODE;
}, [themeColor]);

return (
<KonvaGroup key={`timeslot-${index}`}>
<KonvaLine x={xPos} y={yPos} points={[0, 0, 0, height]} stroke={stroke} strokeWidth={1} />
<KonvaRect fill="transparent" x={xPos - 15} y={yPos - 10} height={15} width={30} />
<KonvaText fill={themeColor} x={xPos - 15} y={yPos - 8} text={cellLabel} />
<KonvaText fill={themeColor} x={xPos} y={yPos - 8} text={cellLabel} width={columnWidth} align="center" />
</KonvaGroup>
);
};
Expand Down
3 changes: 1 addition & 2 deletions src/grid/CellGroup/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo } from "react";
import { Duration, Interval } from "luxon";

import { KonvaGroup, KonvaLine, KonvaRect, KonvaText } from "../../@konva";
import { KonvaGroup, KonvaLine, KonvaText } from "../../@konva";
import { useTimelineContext } from "../../timeline/TimelineContext";
import { DEFAULT_STROKE_DARK_MODE, DEFAULT_STROKE_LIGHT_MODE } from "../../utils/theme";
import { displayAboveInterval } from "../../utils/time-resolution";
Expand Down Expand Up @@ -117,7 +117,6 @@ const GridCellGroup = ({ column, index, dayInfo, hourInfo }: GridCellGroupProps)
return (
<KonvaGroup key={`timeslot-${index}`}>
<KonvaLine x={xPos} y={0} points={points} stroke={stroke} strokeWidth={1} />
<KonvaRect fill="transparent" x={xPos} y={yPos - 10} height={15} width={unitAboveSpanInPx} />
<KonvaText
align="center"
fill={themeColor}
Expand Down
18 changes: 9 additions & 9 deletions src/utils/time-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ export const displayAboveInterval = (interval: Interval, unit: Scale, locale: st
case "hour":
return start.setLocale(locale).toFormat("dd/MM/yy HH:mm");
case "day":
return start.setLocale(locale).toFormat("ccc dd yyyy");
return start.setLocale(locale).toFormat("DDDD");
case "week":
return `${start.setLocale(locale).toFormat("MMM yyyy")} CW ${start.toFormat("WW")}`;
return `${start.setLocale(locale).toFormat("MMMM yyyy")} CW ${start.toFormat("WW")}`;
case "month":
return start.setLocale(locale).toFormat("MMM yyyy");
return start.setLocale(locale).toFormat("MMMM yyyy");
default:
return "N/A";
}
Expand Down Expand Up @@ -191,23 +191,23 @@ export const daysInMonth = (month: number, year: number) => {
* @param interval the interval to display
* @param unit the unit in which to display the interval
*/
export const displayInterval = (interval: Interval, unit: Scale): string => {
export const displayInterval = (interval: Interval, unit: Scale, locale: string): string => {
const { start } = interval;
if (!start) {
return "-";
}

switch (unit) {
case "minute":
return start.toFormat("mm");
return start.setLocale(locale).toFormat("mm");
case "hour":
return start.toFormat("HH:mm");
return start.setLocale(locale).toFormat("HH:mm");
case "day":
return start.toFormat("ccc dd");
return start.setLocale(locale).toFormat("ccc dd");
case "week":
return `CW ${start.toFormat("WW")}`;
return `CW ${start.setLocale(locale).toFormat("WW")}`;
case "month":
return start.toFormat("MMM yyyy");
return start.setLocale(locale).toFormat("MMM yyyy");
default:
return "N/A";
}
Expand Down

0 comments on commit 653296e

Please sign in to comment.