Skip to content

Commit e7d92bf

Browse files
committed
Revert "Moving to hashrouter"
This reverts commit cd42aa7.
1 parent 6652b37 commit e7d92bf

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

Eplant/UI/Layout/ViewContainer/index.tsx

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { useState } from 'react'
2-
import { Outlet } from 'react-router-dom'
1+
import { useEffect, useState } from 'react'
2+
import { Outlet, useLocation, useNavigate, useParams } from 'react-router-dom'
33

44
import { useConfig } from '@eplant/config'
55
import {
66
useActiveGeneId,
77
useActiveViewId,
88
useGeneticElements,
99
usePrinting,
10+
useSpecies,
1011
} from '@eplant/state'
1112
import Modal from '@eplant/UI/Modal'
1213
import ErrorBoundary from '@eplant/util/ErrorBoundary'
@@ -36,9 +37,83 @@ export function ViewContainer<T, S, A>({ ...props }) {
3637
const [printing, setPrinting] = usePrinting()
3738
const [viewingCitations, setViewingCitations] = useState(false)
3839
const { views } = useConfig()
39-
const [genes] = useGeneticElements()
40-
const [activeGeneId] = useActiveGeneId()
41-
const [activeViewId] = useActiveViewId()
40+
const navigate = useNavigate()
41+
const location = useLocation()
42+
const params = useParams()
43+
const [speciesList] = useSpecies()
44+
const [genes, setGenes] = useGeneticElements()
45+
const [activeGeneId, setActiveGeneId] = useActiveGeneId()
46+
const [activeViewId, setActiveViewId] = useActiveViewId()
47+
const [geneNotFound, setGeneNotFound] = useState(false)
48+
49+
// On app url change, make sure loaded gene and view aligns with URL
50+
useEffect(() => {
51+
const loadGene = async (geneid: string) => {
52+
// TODO: This is super jank, should probably write some better utilities for loading genes
53+
const species = speciesList.find(
54+
(species) => species.name === 'Arabidopsis'
55+
)
56+
const newGene = await species?.api.searchGene(geneid)
57+
if (newGene) {
58+
setGenes([...genes, newGene])
59+
} else {
60+
setGeneNotFound(true)
61+
setActiveGeneId('')
62+
}
63+
}
64+
if (params.geneid) {
65+
if (params.geneid !== activeGeneId) {
66+
if (!genes.find((g) => g.id === params.geneid)) {
67+
loadGene(params.geneid)
68+
}
69+
if (!geneNotFound) setActiveGeneId(params.geneid)
70+
}
71+
} else {
72+
// Set active gene to first available if one is already loaded
73+
if (genes.length > 0) {
74+
setActiveGeneId(genes[0].id)
75+
} else {
76+
setActiveGeneId('')
77+
}
78+
}
79+
80+
// Set activeview
81+
const urlView =
82+
views.find((view) => view.id === location.pathname.split('/')[1]) ??
83+
GeneInfoViewMetadata
84+
85+
setActiveViewId(urlView.id)
86+
}, [])
87+
88+
// On when the activegene or view changes, update path
89+
useEffect(() => {
90+
const oldPathSegments = location.pathname
91+
.split('/')
92+
.filter((segment) => segment !== '')
93+
94+
const newPathSegments = []
95+
if (activeViewId) {
96+
newPathSegments.push(activeViewId)
97+
}
98+
if (activeGeneId) {
99+
newPathSegments.push(activeGeneId)
100+
}
101+
102+
if (newPathSegments.length > 0) {
103+
let newPath
104+
if (
105+
oldPathSegments.length > 0 &&
106+
oldPathSegments[0] == newPathSegments[0]
107+
) {
108+
// If the view is the same we want to retain quary params in url, else we can wipe
109+
// them and have URLStateManager handle things
110+
newPath = '/' + newPathSegments.join('/') + location.search
111+
} else {
112+
newPath = '/' + newPathSegments.join('/')
113+
}
114+
navigate(newPath)
115+
}
116+
}, [activeGeneId, activeViewId])
42117

43118
// Get view and gene objects once everything resolves
44119
const activeView =

Eplant/main.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { StrictMode } from 'react'
22
import { Provider } from 'jotai'
33
import * as ReactDOM from 'react-dom/client'
4-
import { createHashRouter, Navigate, RouterProvider } from 'react-router-dom'
4+
import { createBrowserRouter, Navigate, RouterProvider } from 'react-router-dom'
55

66
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
77
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
@@ -20,7 +20,7 @@ import { Config, defaultConfig } from './config'
2020
import Eplant from './Eplant'
2121

2222
import './css/index.css'
23-
const router = createHashRouter(
23+
const router = createBrowserRouter(
2424
[
2525
{
2626
path: '/',

0 commit comments

Comments
 (0)