From 57c86ec5516df31172d79e10726a9699e09ad9c9 Mon Sep 17 00:00:00 2001
From: Marcin Zelent <marcin@zelent.net>
Date: Tue, 26 Jan 2021 19:00:23 +0100
Subject: Added hash with the current trip's name to the URL

---
 pages/index.tsx | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

(limited to 'pages/index.tsx')

diff --git a/pages/index.tsx b/pages/index.tsx
index e422069..f9b365b 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -1,7 +1,7 @@
 import { GetStaticProps } from 'next';
 import Head from 'next/head';
 import dynamic from 'next/dynamic';
-import { useState } from 'react';
+import { useEffect, useState } from 'react';
 
 import { Trip } from 'models';
 import getTripsData from 'lib/trips';
@@ -20,6 +20,19 @@ export default function Home({ allTripsData }: Props): JSX.Element {
   const [currentPhoto, setCurrentPhoto] = useState(null);
   const [asideOpen, setAsideOpen] = useState(false);
 
+  useEffect(() => {
+    if (window.location.hash.length === 0) {
+      window.location.hash = currentTrip.name;
+    } else {
+      const index = allTripsData.findIndex((t) => t.name === window.location.hash.substr(1));
+      if (index > -1) setCurrentTrip(allTripsData[index]);
+    }
+  }, []);
+
+  useEffect(() => {
+    window.location.hash = currentTrip.name;
+  }, [currentTrip]);
+
   function handleMarkerClick(photo: string) {
     const index = currentTrip.photos.findIndex((p) => p.name === photo);
     setCurrentPhoto(index);
-- 
cgit v1.2.3

