Skip to content

Commit

Permalink
Modern react (#132)
Browse files Browse the repository at this point in the history
* UI, utils

* remaining files

* removing unused import

* lint
  • Loading branch information
zkdan authored Jul 11, 2024
1 parent 1749285 commit 4c43465
Show file tree
Hide file tree
Showing 27 changed files with 110 additions and 118 deletions.
12 changes: 6 additions & 6 deletions Eplant/UI/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import { MouseEvent, ReactNode, useId, useState } from 'react'

import { KeyboardArrowDown } from '@mui/icons-material'
import {
Expand Down Expand Up @@ -54,17 +54,17 @@ const StyledMenu = styled((props: MenuProps) => (
export default function Dropdown({
options,
...buttonProps
}: { options: React.ReactNode[] } & ButtonProps) {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null)
}: { options: ReactNode[] } & ButtonProps) {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null)
const open = Boolean(anchorEl)
const handleClick = (event: React.MouseEvent<HTMLElement>) => {
const handleClick = (event: MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget)
}
const handleClose = () => {
setAnchorEl(null)
}
const buttonId = React.useId()
const menuId = React.useId()
const buttonId = useId()
const menuId = useId()

return (
<div>
Expand Down
4 changes: 2 additions & 2 deletions Eplant/UI/GeneticElementComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useId, useRef, useState } from 'react'
import { MouseEvent, useEffect, useId, useRef, useState } from 'react'

import GeneticElement from '@eplant/GeneticElement'
import { DragIndicator } from '@mui/icons-material'
Expand Down Expand Up @@ -265,7 +265,7 @@ export default function GeneticElementComponent({
</Paper>
)

function openMenu(e: React.MouseEvent<HTMLElement>) {
function openMenu(e: MouseEvent<HTMLElement>) {
setMenuEl(e.currentTarget)
setMenuOpen(true)
}
Expand Down
2 changes: 1 addition & 1 deletion Eplant/UI/Layout/ViewContainer/LoadingPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { SVGProps } from 'react'
import { SVGProps } from 'react'

import GeneticElement from '@eplant/GeneticElement'
import { View } from '@eplant/View'
Expand Down
4 changes: 2 additions & 2 deletions Eplant/UI/Layout/ViewContainer/ViewOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import { useTransition } from 'react'

import GeneticElement from '@eplant/GeneticElement'
import Dropdown from '@eplant/UI/Dropdown'
Expand All @@ -18,7 +18,7 @@ export default function ViewOptions<T, S, A>({
state?: S
dispatch: ViewDispatch<A>
}) {
const [transitioning, startTransition] = React.useTransition()
const [transitioning, startTransition] = useTransition()
if (!view.actions) return <></>
return (
<>
Expand Down
12 changes: 6 additions & 6 deletions Eplant/UI/Layout/ViewContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import { useEffect, useId, useMemo, useState } from 'react'

import { useConfig } from '@eplant/config'
import GeneticElement from '@eplant/GeneticElement'
Expand Down Expand Up @@ -48,15 +48,15 @@ export function ViewContainer<T, S, A>({
} & BoxProps) {
const { activeData, error, loading, loadingAmount, dispatch, state } =
useViewData(view, gene)
const idLabel = React.useId()
const selectId = React.useId()
const idLabel = useId()
const selectId = useId()
const [printing, setPrinting] = usePrinting()

const [viewingCitations, setViewingCitations] = React.useState(false)
const [viewingCitations, setViewingCitations] = useState(false)

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

React.useEffect(() => {
useEffect(() => {
if (printing) {
setTimeout(() => {
window.print()
Expand All @@ -65,7 +65,7 @@ export function ViewContainer<T, S, A>({
}
}, [printing])

const topBar = React.useMemo(
const topBar = useMemo(
() => (
<AppBar
variant='elevation'
Expand Down
1 change: 0 additions & 1 deletion Eplant/UI/Layout/ViewNotSupported.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react'
import { SVGProps } from 'react'

import GeneticElement from '@eplant/GeneticElement'
Expand Down
4 changes: 2 additions & 2 deletions Eplant/UI/LeftNav/Collections.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useId, useRef, useState } from 'react'
import { MouseEvent, useEffect, useId, useRef, useState } from 'react'

import {
DndContext,
Expand Down Expand Up @@ -317,7 +317,7 @@ export function Collection({
</Stack>
)

function openMenu(e: React.MouseEvent<HTMLElement>) {
function openMenu(e: MouseEvent<HTMLElement>) {
setMenuEl(e.currentTarget)
setMenuOpen(true)
}
Expand Down
18 changes: 9 additions & 9 deletions Eplant/UI/LeftNav/GeneSearch/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import { useEffect, useId, useRef, useState } from 'react'
import { debounce } from 'lodash'

import AddIcon from '@mui/icons-material/Add'
Expand Down Expand Up @@ -27,11 +27,11 @@ export default function SearchBar(props: {
sx?: SxProps<Theme>
inputProps?: TextFieldProps
}) {
const [value, setValue] = React.useState<string[]>([])
const [inputValue, setInputValue] = React.useState<string>('')
const [options, setOptions] = React.useState<string[]>([])
const [focused, setFocused] = React.useState<boolean>(false)
const updateOptions = React.useRef<(text: string) => void>()
const [value, setValue] = useState<string[]>([])
const [inputValue, setInputValue] = useState<string>('')
const [options, setOptions] = useState<string[]>([])
const [focused, setFocused] = useState<boolean>(false)
const updateOptions = useRef<(text: string) => void>()

function handleChange(input: string[]) {
if (input.length > 0) {
Expand All @@ -42,15 +42,15 @@ export default function SearchBar(props: {
}
}

React.useEffect(() => {
useEffect(() => {
updateOptions.current = debounce(async (newValue) => {
const newoptions = await props.complete?.(newValue)
setOptions(newoptions?.length ? newoptions : [])
}, 100)
}, [props.complete])
const id = React.useId()
const id = useId()

React.useEffect(() => {
useEffect(() => {
updateOptions.current?.(inputValue)
}, [inputValue])

Expand Down
23 changes: 12 additions & 11 deletions Eplant/UI/LeftNav/GeneSearch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react'
import { useEffect, useState } from 'react'

import GeneticElement, { Species } from '@eplant/GeneticElement'
import { useSpecies } from '@eplant/state'
import { Button, MenuItem, styled, TextField } from '@mui/material'
import { Button, styled } from '@mui/material'
import Stack from '@mui/material/Stack'

import SearchBar from './SearchBar'
Expand All @@ -26,16 +26,17 @@ export function SearchGroup({
}: {
addGeneticElements: (gene: GeneticElement[]) => void
}) {
const [species, setSpecies] = React.useState<Species>()
const [speciesList, setSpeciesList] = useSpecies()
const [searchingByExpression, setSearchingByExpression] =
React.useState<boolean>(false)
const [searchingByPhenotype, setSearchingByPhenotype] =
React.useState<boolean>(false)
const [species, setSpecies] = useState<Species>()

// Commedned out until we get multi-species support
// const [searchingByExpression, setSearchingByExpression] = useState<boolean>(false)
// const [searchingByPhenotype, setSearchingByPhenotype] = useState<boolean>(false)
// const [speciesList, setSpeciesList] = useSpecies()

// useEffect(() => {
// if (!species && speciesList.length) setSpecies(speciesList[0])
// }, [species])

React.useEffect(() => {
if (!species && speciesList.length) setSpecies(speciesList[0])
}, [species])
return (
<Stack direction='column' spacing={2}>
{/* Species selector */}
Expand Down
13 changes: 3 additions & 10 deletions Eplant/UI/LeftNav/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import _ from 'lodash'

import GeneticElement from '@eplant/GeneticElement'
Expand All @@ -13,13 +12,7 @@ import ArrowLeft from '@mui/icons-material/ArrowLeft'
import ArrowRight from '@mui/icons-material/ArrowRight'
import DarkModeIcon from '@mui/icons-material/DarkMode'
import SearchIcon from '@mui/icons-material/Search'
import {
Box,
FormControlLabel,
FormGroup,
Switch,
useTheme,
} from '@mui/material'
import { Box, useTheme } from '@mui/material'
import Stack from '@mui/material/Stack'

import { LogoWithText } from '../Logo'
Expand Down Expand Up @@ -47,7 +40,7 @@ export function LeftNav(props: {

const setActiveGeneId = useSetActiveGeneId()

React.useEffect(() => {
useEffect(() => {
const uniq = _.uniqBy(geneticElements, (g) => g.id)
if (uniq.length != geneticElements.length) setGeneticElements(uniq)
}, [geneticElements])
Expand Down
10 changes: 5 additions & 5 deletions Eplant/UI/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react'
import { forwardRef, PropsWithChildren, ReactElement, Ref } from 'react'

import { Dialog, Slide } from '@mui/material'
import { TransitionProps } from '@mui/material/transitions'

const Transition = React.forwardRef(function Transition(
const Transition = forwardRef(function Transition(
props: TransitionProps & {
children: React.ReactElement<any, any>
children: ReactElement<any, any>
},
ref: React.Ref<unknown>
ref: Ref<unknown>
) {
return <Slide direction='up' ref={ref} {...props} />
})

export default function Modal(
props: React.PropsWithChildren<{
props: PropsWithChildren<{
open: boolean
onClose: () => void
}>
Expand Down
4 changes: 2 additions & 2 deletions Eplant/UI/OptionsButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import { MouseEvent } from 'react'

import { MoreVert } from '@mui/icons-material'
import { IconButton, IconButtonProps } from '@mui/material'
Expand All @@ -7,7 +7,7 @@ export default function OptionsButton({
onClick,
...props
}: {
onClick: (e: React.MouseEvent<HTMLElement>) => void
onClick: (e: MouseEvent<HTMLElement>) => void
} & IconButtonProps) {
return (
<IconButton onClick={onClick} {...props}>
Expand Down
7 changes: 1 addition & 6 deletions Eplant/UI/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { useState } from 'react'

import ArrowCircleLeftIcon from '@mui/icons-material/ArrowCircleLeft'
import ArrowCircleRightIcon from '@mui/icons-material/ArrowCircleRight'
import { Container } from '@mui/material'
import { Box } from '@mui/material'

import SerializedGeneticElement from '../GeneticElement'
import { useSidebarState } from '../state'
Expand All @@ -18,7 +13,7 @@ export const collapsedSidebarWidth = 100

export default function Sidebar() {
const [activeGeneId, setActiveGeneId] = useActiveGeneId()
const [isCollapse, setIsCollapse] = useSidebarState()
const [isCollapse] = useSidebarState()
return (
<div>
<ResponsiveDrawer
Expand Down
6 changes: 3 additions & 3 deletions Eplant/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import { createContext, useContext } from 'react'

import CellEFP from './views/CellEFP'
import DebugView from './views/DebugView'
Expand Down Expand Up @@ -45,6 +45,6 @@ export const defaultConfig = {
defaultSpecies: '',
}

export const Config = React.createContext<EplantConfig>(defaultConfig)
export const Config = createContext<EplantConfig>(defaultConfig)

export const useConfig = () => React.useContext(Config)
export const useConfig = () => useContext(Config)
8 changes: 4 additions & 4 deletions Eplant/util/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { ErrorInfo } from 'react'
export default class ErrorBoundary extends React.Component<
React.PropsWithChildren,
import { Component, ErrorInfo, PropsWithChildren } from 'react'
export default class ErrorBoundary extends Component<
PropsWithChildren,
{ hasError: boolean }
> {
constructor(props: React.PropsWithChildren) {
constructor(props: PropsWithChildren) {
super(props)
this.state = { hasError: false }
}
Expand Down
8 changes: 4 additions & 4 deletions Eplant/util/PanZoom/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import { useEffect, useRef, useState } from 'react'

import { Box, BoxProps } from '@mui/material'

Expand All @@ -17,15 +17,15 @@ export default function PanZoom({
transform: Transform
onTransformChange: (transform: Transform) => void
}) {
const [dragStart, setDragStart] = React.useState<{
const [dragStart, setDragStart] = useState<{
click: Point
offset: Point
} | null>(null)

const { offset, zoom } = transform
const containerRef = React.useRef<HTMLDivElement>(null)
const containerRef = useRef<HTMLDivElement>(null)

React.useEffect(() => {
useEffect(() => {
const listener = (e: WheelEvent) => {
e.stopPropagation()
e.preventDefault()
Expand Down
Loading

0 comments on commit 4c43465

Please sign in to comment.