From 4ebea634717cd5333a7ce36bc2d3f80e3d3929d2 Mon Sep 17 00:00:00 2001
From: esikkala <esko.ikkala@aalto.fi>
Date: Fri, 11 Jun 2021 11:51:33 +0300
Subject: [PATCH] Instance page URLs: support hash fragments

---
 .../components/perspectives/sampo/InstanceHomePage.js  | 10 +++++++++-
 .../components/perspectives/sampo/Perspective3.js      |  1 +
 src/client/configs/sampo/PerspectiveConfig.js          |  2 ++
 src/client/containers/SemanticPortal.js                |  5 ++++-
 src/client/index.js                                    |  8 ++++++--
 5 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/client/components/perspectives/sampo/InstanceHomePage.js b/src/client/components/perspectives/sampo/InstanceHomePage.js
index f01f8bdc..73c54ada 100644
--- a/src/client/components/perspectives/sampo/InstanceHomePage.js
+++ b/src/client/components/perspectives/sampo/InstanceHomePage.js
@@ -155,6 +155,8 @@ class InstanceHomePage extends React.Component {
     const { classes, perspectiveState, perspectiveConfig, rootUrl, screenSize, layoutConfig } = this.props
     const { instanceTableData, fetching } = perspectiveState
     const resultClass = perspectiveConfig.id
+    const defaultInstancePageTab = perspectiveConfig.defaultInstancePageTab
+      ? perspectiveConfig.defaultInstancePageTab : 'table'
     const hasTableData = this.hasTableData()
     return (
       <div className={classes.root}>
@@ -180,7 +182,13 @@ class InstanceHomePage extends React.Component {
             <>
               <Route
                 exact path={`${rootUrl}/${resultClass}/page/${this.state.localID}`}
-                render={() => <Redirect to={`${rootUrl}/${resultClass}/page/${this.state.localID}/table`} />}
+                render={routeProps =>
+                  <Redirect
+                    to={{
+                      pathname: `${rootUrl}/${resultClass}/page/${this.state.localID}/${defaultInstancePageTab}`,
+                      hash: routeProps.location.hash
+                    }}
+                  />}
               />
               <Route
                 path={[`${rootUrl}/${resultClass}/page/${this.state.localID}/table`, '/iframe.html']} // support also rendering in Storybook
diff --git a/src/client/components/perspectives/sampo/Perspective3.js b/src/client/components/perspectives/sampo/Perspective3.js
index 5fb6d5b2..94b1c109 100644
--- a/src/client/components/perspectives/sampo/Perspective3.js
+++ b/src/client/components/perspectives/sampo/Perspective3.js
@@ -22,6 +22,7 @@ const Perspective3 = props => {
         routeProps={props.routeProps}
         tabs={props.perspective.tabs}
         screenSize={props.screenSize}
+        layoutConfig={props.layoutConfig}
       />
       <Route
         exact path={`${rootUrl}/${perspective.id}/faceted-search`}
diff --git a/src/client/configs/sampo/PerspectiveConfig.js b/src/client/configs/sampo/PerspectiveConfig.js
index f0aaf623..04cb345e 100644
--- a/src/client/configs/sampo/PerspectiveConfig.js
+++ b/src/client/configs/sampo/PerspectiveConfig.js
@@ -20,6 +20,8 @@ export const perspectiveConfig = [
     id: 'perspective1',
     frontPageImage: manuscriptsImage,
     defaultActiveFacets: new Set([]),
+    defaultTab: 'table',
+    defaultInstancePageTab: 'table',
     tabs: [
       {
         id: 'table',
diff --git a/src/client/containers/SemanticPortal.js b/src/client/containers/SemanticPortal.js
index 323ba7c5..e6d50069 100644
--- a/src/client/containers/SemanticPortal.js
+++ b/src/client/containers/SemanticPortal.js
@@ -396,7 +396,10 @@ const SemanticPortal = props => {
                   <Switch>
                     <Redirect
                       from={`/${perspective.id}/page/:id`}
-                      to={`${rootUrlWithLang}/${perspective.id}/page/:id`}
+                      to={{
+                        pathname: `${rootUrlWithLang}/${perspective.id}/page/:id`,
+                        hash: props.location.hash
+                      }}
                     />
                     <Route
                       path={`${rootUrlWithLang}/${perspective.id}/page/:id`}
diff --git a/src/client/index.js b/src/client/index.js
index 3f65f304..fc312ff8 100644
--- a/src/client/index.js
+++ b/src/client/index.js
@@ -27,12 +27,16 @@ if (Object.prototype.hasOwnProperty.call(availableLocales, localeFromUrl)) {
 } else {
   // support urls without a locale
   locale = defaultLocale
+  const { pathname, hash } = window.location
   const newPathname = updateLocaleToPathname({
-    pathname: window.location.pathname,
+    pathname,
     locale,
     replaceOld: false
   })
-  history.push({ pathname: newPathname })
+  history.push({
+    pathname: newPathname,
+    hash
+  })
 }
 store.dispatch(loadLocales(locale))
 
-- 
GitLab