Skip to content

Commit

Permalink
a bunch of changes that aren't working right now
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukthiw committed Oct 5, 2024
1 parent bc14f2f commit adac7f1
Show file tree
Hide file tree
Showing 16 changed files with 654 additions and 278 deletions.
158 changes: 134 additions & 24 deletions Eplant/Eplant.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,150 @@
// import useStateWithStorage from '@eplant/util/useStateWithStorage'
import { Route, Routes } from 'react-router-dom'

import { CssBaseline, ThemeProvider } from '@mui/material'
import { useEffect, useState } from 'react'
import { Outlet, useLocation, useNavigate, useParams } from 'react-router-dom'

import {
Box,
CircularProgress,
CssBaseline,
ThemeProvider,
useTheme,
} from '@mui/material'

import { dark, light } from './css/theme'
import Sidebar from './UI/Sidebar'
import { ViewContainer } from './UI/Layout/ViewContainer'
import Sidebar, { collapsedSidebarWidth, sidebarWidth } from './UI/Sidebar'
import { ViewDataError } from './View/viewData'
import FallbackView from './views/FallbackView'
import { useConfig } from './config'
import EplantLayout from './EplantLayout'
import { useDarkMode } from './state'
import {
useActiveGeneId,
useActiveViewId,
useDarkMode,
useGeneticElements,
usePageLoad,
useSidebarState,
useSpecies,
} from './state'
import { updateColors } from './updateColors'
export type EplantProps = Record<string, never>
export default function Eplant() {
const { rootPath } = useConfig()
const [darkMode] = useDarkMode()

return (
<ThemeProvider theme={darkMode ? dark : light}>
<CssBaseline />
<Routes>
<Route path={rootPath}>
<Route index element={<MainEplant />} />
</Route>
</Routes>
</ThemeProvider>
)
}
/**
* The main Eplant component. This is the root of the application. It contains the left nav and the layout.
* @returns {JSX.Element} The rendered Eplant component
*/
const Eplant = () => {
const [darkMode] = useDarkMode()
const [activeGeneId, setActiveGeneId] = useActiveGeneId()
const [activeViewId, setActiveViewId] = useState<string>('')
const [isCollapse, setIsCollapse] = useSidebarState()
const [genes, setGenes] = useGeneticElements()
const theme = useTheme()
const [globalProgress, loaded] = usePageLoad()
const config = useConfig()
const navigate = useNavigate()
const location = useLocation()
const params = useParams()
const [speciesList] = useSpecies()
useEffect(() => {
if (loaded) {
updateColors(theme)
}
}, [theme, loaded])
// On app url change, make sure loaded gene and view aligns with URL
useEffect(() => {
const loadGene = async (geneid: string) => {
// TODO: This is super jank, should probably write some better utilities for loading genes
const species = speciesList.find(
(species) => species.name === 'Arabidopsis'
)
const gene = await species?.api.searchGene(geneid)
if (gene) {
setGenes([...genes, gene])
}
}
if (params.geneid) {
if (params.geneid !== activeGeneId) {
if (!genes.find((gene) => gene.id === params.geneid)) {
loadGene(params.geneid)
}
setActiveGeneId(params.geneid)
}
} else {
setActiveGeneId('')
}
setActiveViewId(location.pathname.split('/')[1])
}, [location.pathname])

// On active gene change update the gene path segment
useEffect(() => {
const pathSegments = location.pathname.split('/')
const geneid = activeGeneId ? activeGeneId : ''
if (pathSegments.length == 3) {
pathSegments[pathSegments.length - 1] = geneid
} else if (pathSegments.length == 2) {
pathSegments.push(geneid)
}

const newPath = pathSegments.join('/') + location.search
if (newPath !== location.pathname + location.search) {
navigate(newPath)
}
}, [activeGeneId])

// SideBar and EplantLayout children
export function MainEplant() {
return (
<>
<ThemeProvider theme={darkMode ? dark : light}>
<CssBaseline />
<Sidebar />
<EplantLayout />
</>
<Box
sx={(theme) => ({
height: `calc(100% - ${theme.spacing(1)})`,
left: `${isCollapse ? collapsedSidebarWidth : sidebarWidth}px`,
right: '0px',
position: 'absolute',
marginTop: '0.5rem',
boxSizing: 'border-box',
transition: 'left 1s ease-out',
backgroundColor: theme.palette.background.paper,
})}
>
<Box
sx={{
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'stretch',
justifyContent: 'stretch',
}}
>
<div />
{loaded ? (
<ViewContainer
gene={genes.find((gene) => gene.id === activeGeneId) ?? null}
view={
config.views.find(
(view) => view.id === (activeViewId ?? config.defaultView)
) ?? FallbackView
}
setView={(viewid) => {
setActiveViewId(viewid)
}}
sx={{
width: '100%',
height: '100%',
}}
></ViewContainer>
) : (
<div>
<CircularProgress
variant='indeterminate'
value={globalProgress * 100}
/>
</div>
)}
</Box>
</Box>
</ThemeProvider>
)
}
export default Eplant
13 changes: 2 additions & 11 deletions Eplant/EplantLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { useContext, useEffect, useRef, useState } from 'react'
import * as FlexLayout from 'flexlayout-react'
import {
Actions,
BorderNode,
ITabSetRenderValues,
Layout,
TabSetNode,
} from 'flexlayout-react'
import { useEffect } from 'react'

import { Add, CallMade, Close } from '@mui/icons-material'
import { Box, CircularProgress, IconButton } from '@mui/material'
import { useTheme } from '@mui/material/styles'

Expand Down Expand Up @@ -71,7 +62,7 @@ const EplantLayout = () => {
) ?? FallbackView
}
setView={(view) => {
setActiveViewId(view.id)
setActiveViewId(view)
}}
sx={{
width: '100%',
Expand Down
58 changes: 39 additions & 19 deletions Eplant/UI/Layout/ViewContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useId, useMemo, useState } from 'react'
import { Outlet, useLocation, useNavigate } from 'react-router-dom'

import { useConfig } from '@eplant/config'
import GeneticElement from '@eplant/GeneticElement'
Expand Down Expand Up @@ -26,6 +27,7 @@ import Box, { BoxProps } from '@mui/material/Box'
import { View } from '../../../View'

import LoadingPage from './LoadingPage'
import { ViewContext } from './types'
import ViewOptions from './ViewOptions'

/**
Expand All @@ -43,16 +45,19 @@ export function ViewContainer<T, S, A>({
...props
}: {
view: View<T, S, A>
setView: (view: View) => void
setView: (viewid: string) => void
gene: GeneticElement | null
} & BoxProps) {
const { activeData, error, loading, loadingAmount, dispatch, state } =
useViewData(view, gene)
// const { activeData, error, dispatch, state } = useViewData(view, gene)
const [loading, setLoading] = useState(false)
const [loadAmount, setLoadAmount] = useState(0)
const idLabel = useId()
const selectId = useId()
const [printing, setPrinting] = usePrinting()

const [viewingCitations, setViewingCitations] = useState(false)
const navigate = useNavigate()
const location = useLocation()

const { userViews, views, genericViews } = useConfig()

Expand All @@ -64,7 +69,6 @@ export function ViewContainer<T, S, A>({
}, 100)
}
}, [printing])

const topBar = useMemo(
() => (
<AppBar
Expand Down Expand Up @@ -121,7 +125,15 @@ export function ViewContainer<T, S, A>({
id={selectId}
onChange={(e) => {
const view = views.find((view) => view.id == e?.target?.value)
if (view) setView(view)
if (view) {
const pathSegments = location.pathname.split('/')
pathSegments[1] = view.id
const newPath = pathSegments.join('/')
if (newPath !== location.pathname + location.search) {
setView(view.id)
navigate(newPath)
}
}
}}
sx={{
'& .MuiSelect-select': {
Expand Down Expand Up @@ -183,7 +195,7 @@ export function ViewContainer<T, S, A>({
}}
key={view.name}
onClick={(e) => {
if (view) setView(view)
if (view) setView(view.id)
}}
>
{view.name}
Expand All @@ -194,13 +206,13 @@ export function ViewContainer<T, S, A>({
</FormControl>
</Stack>

<ViewOptions
{/* <ViewOptions
gene={gene}
state={state}
view={view}
loading={loading}
dispatch={dispatch}
/>
/> */}
<Button
variant='text'
sx={{
Expand All @@ -222,18 +234,18 @@ export function ViewContainer<T, S, A>({
disabled={loading}
color='secondary'
onClick={() => {
downloadFile(
`${view.id}${gene ? '-' + gene.id : ''}.json`,
JSON.stringify(activeData, null, 2)
)
// downloadFile(
// `${view.id}${gene ? '-' + gene.id : ''}.json`,
// JSON.stringify(activeData, null, 2)
// )
}}
>
Download data
</Button>
</Toolbar>
</AppBar>
),
[view.id, gene?.id, loading, activeData, state, dispatch]
[view.id, gene?.id, loading]
)
return (
<Box {...props} display='flex' flexDirection='column'>
Expand All @@ -243,7 +255,8 @@ export function ViewContainer<T, S, A>({
</DialogTitle>
<DialogContent>
{view.citation ? (
<view.citation state={state} activeData={activeData} gene={gene} />
// <view.citation state={state} activeData={activeData} gene={gene} />
<div></div>
) : (
<Box>No information provided for this view</Box>
)}
Expand Down Expand Up @@ -283,21 +296,28 @@ export function ViewContainer<T, S, A>({
>
<ErrorBoundary>
{/* Only show the gene header if a gene is selected and this view belongs to the gene */}
{loading || activeData === undefined ? (
{loading ? (
<LoadingPage
loadingAmount={loadingAmount}
loadingAmount={loadAmount}
gene={gene}
view={view}
error={error}
error={null}
/>
) : (
<>
<view.component
{/* <view.component
state={state}
geneticElement={gene}
activeData={activeData}
dispatch={dispatch}
/>
/> */}
<Outlet
context={{
geneticElement: gene,
setLoadAmount: setLoadAmount,
setIsLoading: setLoading,
}}
></Outlet>
</>
)}
</ErrorBoundary>
Expand Down
7 changes: 7 additions & 0 deletions Eplant/UI/Layout/ViewContainer/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import GeneticElement from '@eplant/GeneticElement'

export type ViewContext = {
geneticElement: GeneticElement | null
setLoadAmount: (loaded: number) => void
setIsLoading: (isLoading: boolean) => void
}
6 changes: 3 additions & 3 deletions Eplant/UI/Layout/ViewNotSupported.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Illustration = ({
*/
export default function NotSupported(props: {
geneticElement: GeneticElement | null
view: View
viewName: string
}) {
const theme = useTheme()
return (
Expand All @@ -45,7 +45,7 @@ export default function NotSupported(props: {

<Typography variant='h6'>
{props.geneticElement
? `Cannot view ${props.view.name.toLowerCase()} for ${
? `Cannot view ${props.viewName.toLowerCase()} for ${
props.geneticElement.id
}`
: `There is no gene selected`}
Expand All @@ -58,7 +58,7 @@ export default function NotSupported(props: {
>
{props.geneticElement
? 'No data is available for this gene.'
: `The ${props.view.name.toLowerCase()} requires a selected gene.`}
: `The ${props.viewName.toLowerCase()} requires a selected gene.`}
</Typography>
</Stack>
)
Expand Down
Loading

0 comments on commit adac7f1

Please sign in to comment.