From 628632ff5092f1e3cf6c968d9bdfbd9a24f59541 Mon Sep 17 00:00:00 2001
From: Marcin Zelent <marcin@zelent.net>
Date: Fri, 8 Jan 2021 19:47:37 +0100
Subject: Initial commit

---
 components/Sidebar/Sidebar.tsx | 71 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
 create mode 100644 components/Sidebar/Sidebar.tsx

(limited to 'components/Sidebar/Sidebar.tsx')

diff --git a/components/Sidebar/Sidebar.tsx b/components/Sidebar/Sidebar.tsx
new file mode 100644
index 0000000..6b0473a
--- /dev/null
+++ b/components/Sidebar/Sidebar.tsx
@@ -0,0 +1,71 @@
+import { useEffect, useRef } from 'react';
+
+import { Trip } from 'models';
+import { secondsToTimeString } from 'lib/util';
+
+import styles from './Sidebar.module.css';
+
+interface Props {
+  trips: Trip[];
+  currentTrip: Trip;
+  asideOpen: boolean;
+  handleClose: () => void;
+  setCurrentTrip: (trip: Trip) => void;
+}
+
+export default function Sidebar({
+  trips,
+  currentTrip,
+  asideOpen,
+  handleClose,
+  setCurrentTrip,
+}: Props): JSX.Element {
+  const wrapperRef = useRef(null);
+
+  useEffect(() => {
+    function handleClickOutside(e: MouseEvent) {
+      if (wrapperRef.current && !wrapperRef.current.contains(e.target)) {
+        handleClose();
+      }
+    }
+
+    document.addEventListener('mousedown', handleClickOutside);
+    return () => {
+      document.removeEventListener('mousedown', handleClickOutside);
+    };
+  }, [wrapperRef]);
+
+  function handleTripChange(trip: Trip): void {
+    setCurrentTrip(trip);
+    handleClose();
+  }
+
+  return (
+    <aside ref={wrapperRef} className={`${styles.aside} ${asideOpen && styles.asideOpen}`}>
+      <h2>Trips</h2>
+      <ul className={styles.list}>
+        {trips.map((t) => (
+          <li key={t.name}>
+            <a
+              onClick={() => handleTripChange(t)}
+              onKeyPress={() => handleTripChange(t)}
+              tabIndex={0}
+              role="menuitem"
+              className={`${styles.listItem} ${
+                t.name === currentTrip.name && styles.listItemActive
+              }`}
+            >
+              <b>{new Date(t.start).toDateString()}</b>
+              <br />
+              Total distance: {t.distance} km
+              <br />
+              Duration: {secondsToTimeString(t.duration)}
+              <br />
+              Average speed: {t.speed} km/h
+            </a>
+          </li>
+        ))}
+      </ul>
+    </aside>
+  );
+}
-- 
cgit v1.2.3

