Skip to content

Commit

Permalink
fix: enable loading images from the same webSource (#120)
Browse files Browse the repository at this point in the history
* fix: enable loading images from the same webSource

* refactor: separate Pixelium into 2 components

* refactor: remove webSource prop in pixelium component

* Merge remote-tracking branch 'origin/main' into 117-cannot-load-image-twice

* refactor: remove webSource
  • Loading branch information
moonayyur authored Jun 7, 2024
1 parent 7369b60 commit 5418e3b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 112 deletions.
27 changes: 0 additions & 27 deletions src/components/AutoLoader.tsx

This file was deleted.

59 changes: 59 additions & 0 deletions src/components/PixeliumApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import styled from '@emotion/styled';
import { memo, useRef } from 'react';
import { SplitPane, Toolbar } from 'react-science/ui';

import CenterPanel from './layout/CenterPanel';
import Header from './layout/Header';
import Sidebar from './layout/Sidebar';
import ModalContainer from './modal/ModalContainer';
import ExportTool from './tool/ExportTool';
import GreyTool from './tool/FilterTool';
import GeometryTool from './tool/GeometryTool';
import ImportTool from './tool/ImportTool';
import MaskTool from './tool/MaskTool';
import MorphologyTool from './tool/MorphologyTool';
import ROITool from './tool/ROITool';

const PixeliumStyle = styled.div`
width: 100%;
height: 100%;
background-color: white;
display: flex;
flex-direction: column;
`;

const PixeliumMainStyle = styled.div`
display: flex;
flex-direction: row;
height: 100%;
overflow: hidden;
`;

function PixeliumApp() {
// Refs
const rootRef = useRef<HTMLDivElement>(null);

return (
<PixeliumStyle ref={rootRef}>
<Header />
<PixeliumMainStyle>
<Toolbar vertical>
<ImportTool />
<ExportTool />
<GreyTool />
<MaskTool />
<MorphologyTool />
<GeometryTool />
<ROITool />
<ModalContainer />
</Toolbar>
<SplitPane direction="horizontal" size="300px" controlledSide="end">
<CenterPanel />
<Sidebar />
</SplitPane>
</PixeliumMainStyle>
</PixeliumStyle>
);
}

export default memo(PixeliumApp);
80 changes: 19 additions & 61 deletions src/components/Pixelium.tsx → src/components/PixeliumProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import styled from '@emotion/styled';
import { WebSource } from 'filelist-utils';
import { memo, useCallback, useMemo, useReducer, useRef } from 'react';
import {
memo,
ReactNode,
useCallback,
useMemo,
useReducer,
useRef,
} from 'react';
import { KbsProvider } from 'react-kbs';
import { RoiProvider } from 'react-roi';
import { RootLayout, SplitPane, Toolbar } from 'react-science/ui';
import { RootLayout } from 'react-science/ui';

import useViewDispatch from '../hooks/useViewDispatch';
import {
Expand All @@ -23,7 +28,6 @@ import {
ViewState,
} from '../state/view/ViewReducer';

import AutoLoader from './AutoLoader';
import { AnnotationsProvider } from './context/AnnotationsContext';
import { DataProvider } from './context/DataContext';
import { DispatchProvider } from './context/DispatchContext';
Expand All @@ -33,42 +37,22 @@ import { PreferencesProvider } from './context/PreferencesContext';
import { initialROIState, ROIProvider, ROIReducer } from './context/ROIContext';
import { RootProvider } from './context/RootContext';
import { ViewProvider } from './context/ViewContext';
import CenterPanel from './layout/CenterPanel';
import Header from './layout/Header';
import Sidebar from './layout/Sidebar';
import ModalContainer from './modal/ModalContainer';
import ExportTool from './tool/ExportTool';
import GreyTool from './tool/FilterTool';
import GeometryTool from './tool/GeometryTool';
import ImportTool from './tool/ImportTool';
import MaskTool from './tool/MaskTool';
import MorphologyTool from './tool/MorphologyTool';
import ROITool from './tool/ROITool';

const PixeliumStyle = styled.div`
width: 100%;
height: 100%;
background-color: white;
display: flex;
flex-direction: column;
`;

interface PixeliumProps {
data?: DataState;
preferences?: PreferencesState;
view?: ViewState;
webSource?: WebSource;
children: ReactNode;
}

const PixeliumMainStyle = styled.div`
display: flex;
flex-direction: row;
height: 100%;
overflow: hidden;
`;

function Pixelium({ data, preferences, view, webSource }: PixeliumProps) {
function PixeliumProvider({
data,
preferences,
view,
children,
}: PixeliumProps) {
const viewDispatch = useViewDispatch();

// Refs
const rootRef = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -149,33 +133,7 @@ function Pixelium({ data, preferences, view, webSource }: PixeliumProps) {
}}
onAfterZoomChange={setPanZoom}
>
<AnnotationsProvider>
<AutoLoader webSource={webSource}>
<PixeliumStyle ref={rootRef}>
<Header />
<PixeliumMainStyle>
<Toolbar vertical>
<ImportTool />
<ExportTool />
<GreyTool />
<MaskTool />
<MorphologyTool />
<GeometryTool />
<ROITool />
<ModalContainer />
</Toolbar>
<SplitPane
direction="horizontal"
size="400px"
controlledSide="end"
>
<CenterPanel />
<Sidebar />
</SplitPane>
</PixeliumMainStyle>
</PixeliumStyle>
</AutoLoader>
</AnnotationsProvider>
<AnnotationsProvider>{children}</AnnotationsProvider>
</RoiProvider>
</ROIProvider>
</PipelineProvider>
Expand All @@ -190,4 +148,4 @@ function Pixelium({ data, preferences, view, webSource }: PixeliumProps) {
);
}

export default memo(Pixelium);
export default memo(PixeliumProvider);
24 changes: 11 additions & 13 deletions src/demo/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import ky from 'ky';
import { Fragment, memo, useEffect, useState } from 'react';
import { Button } from 'react-science/ui';

import useFileLoader from '../hooks/useFileLoader';
import useLog from '../hooks/useLog';

const StyledSidebar = styled.div`
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -50,18 +53,15 @@ interface TocResponse {
}[];
}

interface SidebarProps {
setWebSource: (webSource: WebSource) => void;
}
function Sidebar() {
const { handleWebSource } = useFileLoader();
const { logger } = useLog();

function Sidebar({ setWebSource }: SidebarProps) {
const [toc, setToC] = useState<TocResponse>({
title: '',
sections: [],
});

const [selectedSource, setSelectedSource] = useState<WebSource>();

useEffect(() => {
ky.get('https://image-js.github.io/image-dataset-demo/toc.json')
.json<TocResponse>()
Expand All @@ -72,12 +72,6 @@ function Sidebar({ setWebSource }: SidebarProps) {
});
}, []);

useEffect(() => {
if (selectedSource) {
setWebSource(selectedSource);
}
}, [selectedSource, setWebSource]);

return (
<StyledSidebar>
<h1>Pixelium</h1>
Expand All @@ -91,7 +85,11 @@ function Sidebar({ setWebSource }: SidebarProps) {
<Button
key={source.name}
onClick={() => {
setSelectedSource(source.source);
handleWebSource(source.source).catch((error) => {
logger.error(
`Error while loading websource: ${error.message}`,
);
});
}}
fill
>
Expand Down
22 changes: 11 additions & 11 deletions src/demo/views/MainView.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import styled from '@emotion/styled';
import { WebSource } from 'filelist-utils';
import { memo, useState } from 'react';
import { memo } from 'react';

import Pixelium from '../../components/Pixelium';
import PixeliumApp from '../../components/PixeliumApp';
import PixeliumProvider from '../../components/PixeliumProvider';
import Sidebar from '../Sidebar';

const MainViewWrapper = styled.div`
Expand All @@ -21,15 +21,15 @@ const PixeliumWrapper = styled.div`
`;

function MainView() {
const [webSource, setWebSource] = useState<WebSource>();

return (
<MainViewWrapper>
<Sidebar setWebSource={setWebSource} />
<PixeliumWrapper>
<Pixelium webSource={webSource} />
</PixeliumWrapper>
</MainViewWrapper>
<PixeliumProvider>
<MainViewWrapper>
<Sidebar />
<PixeliumWrapper>
<PixeliumApp />
</PixeliumWrapper>
</MainViewWrapper>
</PixeliumProvider>
);
}

Expand Down

0 comments on commit 5418e3b

Please sign in to comment.