React Scheduler View Implementation
This challenge requires you to build a functional scheduler view component in React using TypeScript. A scheduler view is a common UI pattern used to display events, appointments, or tasks over a period of time, allowing users to easily visualize their schedule. This exercise will test your understanding of React component development, state management, and basic date/time manipulation.
Problem Description
You need to create a React component named SchedulerView that displays a series of "events" within a daily timeline. The component should accept an array of event objects as props and render them in a visually organized manner.
Key Requirements:
- Event Representation: Each event should have at least a
title(string),startTime(Date object), andendTime(Date object). - Timeline Display: The view should represent a 24-hour day, with visual indicators for each hour.
- Event Placement: Events must be positioned on the timeline based on their
startTimeandendTime. The height of an event block should be proportional to its duration. - Overlapping Events: If multiple events occur at the same time, they should be displayed without overlapping horizontally. You can achieve this by assigning them different "lanes" or "columns" within the same hour.
- Styling: Basic styling should be applied to make the timeline and events visually distinct. You are free to choose your styling approach (CSS Modules, styled-components, inline styles, etc.).
- TypeScript: The entire solution must be written in TypeScript, including type definitions for events and component props.
Expected Behavior:
- The
SchedulerViewcomponent should render a vertical timeline from 00:00 to 23:00. - Each event passed as a prop should be rendered as a distinct block on the timeline.
- The position and height of an event block should accurately reflect its start time, end time, and duration.
- Overlapping events should be displayed side-by-side within the same hour slot.
Edge Cases to Consider:
- Events that span across hour boundaries.
- Events that start and end at the same time (zero duration).
- Events that start or end exactly on an hour mark.
- Empty event list.
- Events that extend beyond the 24-hour period (though for simplicity, we can assume events are within a single day).
Examples
Example 1:
Input Props:
events = [
{ id: 'e1', title: 'Meeting', startTime: new Date('2023-10-27T09:00:00'), endTime: new Date('2023-10-27T10:30:00') },
{ id: 'e2', title: 'Lunch Break', startTime: new Date('2023-10-27T12:00:00'), endTime: new Date('2023-10-27T13:00:00') }
]
Output Visualization:
Imagine a vertical timeline.
- 'Meeting' would start at the 9:00 mark and extend down to the 10:30 mark. Its height would be 1.5 hours.
- 'Lunch Break' would start at the 12:00 mark and extend down to the 13:00 mark. Its height would be 1 hour.
Example 2:
Input Props:
events = [
{ id: 'e3', title: 'Team Sync', startTime: new Date('2023-10-27T14:00:00'), endTime: new Date('2023-10-27T14:30:00') },
{ id: 'e4', title: 'Code Review', startTime: new Date('2023-10-27T14:15:00'), endTime: new Date('2023-10-27T15:00:00') }
]
Output Visualization:
Imagine a vertical timeline.
- Both 'Team Sync' and 'Code Review' occur within the 14:00 hour.
- 'Team Sync' starts at 14:00 and ends at 14:30.
- 'Code Review' starts at 14:15 and ends at 15:00.
- These two events should be displayed side-by-side within the 14:00 hour segment, and 'Code Review' should extend into the 15:00 segment.
Example 3 (Edge Case):
Input Props:
events = [
{ id: 'e5', title: 'All Day Event', startTime: new Date('2023-10-27T00:00:00'), endTime: new Date('2023-10-27T23:59:59') }
]
Output Visualization:
Imagine a vertical timeline.
- 'All Day Event' would occupy the entire height of the timeline from 00:00 to 23:59.
Constraints
- The
SchedulerViewcomponent will receive an array of event objects, where the array length can be from 0 to 100. - Each event's
startTimeandendTimewill be validDateobjects representing times within a single 24-hour period. - The duration of an event will be at least 1 minute and at most 24 hours.
- The component should render efficiently, even with a moderate number of events (e.g., up to 50 events).
Notes
- Consider how to calculate the position and height of an event based on its duration relative to the total duration of the timeline (24 hours).
- For handling overlapping events, you'll need a strategy to determine how many "lanes" are required for a given hour and assign events to those lanes. This might involve iterating through events within each hour and checking for overlaps.
- You can assume a fixed height for the
SchedulerViewcontainer. The height of individual event blocks will be a percentage or pixel value derived from their duration. - Feel free to define additional properties for your event objects if you think they are necessary for styling or identification (e.g.,
id,color).