Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions src/ThreeEditor/components/Dialog/SaveFileDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Button, Checkbox, FormControlLabel, TextField } from '@mui/material';
import { ChangeEvent, useCallback, useEffect, useState } from 'react';

import { FullSimulationData } from '../../../services/ShSimulatorService';
import { FullSimulationData, JobResults } from '../../../services/ShSimulatorService';
import { StoreContext } from '../../../services/StoreService';
import { RequestGetJobResults } from '../../../types/RequestTypes';
import { saveString } from '../../../util/File';
import { ConcreteDialogProps, CustomDialog } from './CustomDialog';

Expand All @@ -26,21 +27,33 @@ export function SaveFileDialog({
name: defaultName = 'editor',
results: providedResults,
disableCheckbox = false,
yaptideEditor
yaptideEditor,
getJobResults,
expectedEstimatorsSize
}: ConcreteDialogProps<
{
name?: string;
results?: FullSimulationData;
disableCheckbox?: boolean;
getJobResults: (...args: RequestGetJobResults) => Promise<JobResults | undefined>;
expectedEstimatorsSize?: number;
} & Required<Pick<StoreContext, 'yaptideEditor'>>
>) {
const results: FullSimulationData | undefined = providedResults ?? yaptideEditor?.getResults();

const [controller] = useState(new AbortController());
const [keepResults, setKeepResults] = useState<boolean>(false);
const [fetchedResults, setFetchedResults] = useState<FullSimulationData | undefined>(results);
const jobId = fetchedResults?.jobId;
const shouldFetchEstimators =
jobId && fetchedResults?.estimators.length !== expectedEstimatorsSize;

const canKeepResults = useCallback(() => {
return disableCheckbox && results?.input.inputJson?.hash === yaptideEditor?.toJSON().hash;
}, [disableCheckbox, results?.input.inputJson?.hash, yaptideEditor]);
return (
disableCheckbox &&
fetchedResults?.input.inputJson?.hash === yaptideEditor?.toJSON().hash
);
}, [disableCheckbox, fetchedResults?.input.inputJson?.hash, yaptideEditor]);

useEffect(() => {
setKeepResults(canKeepResults() || !!providedResults);
Expand All @@ -58,6 +71,27 @@ export function SaveFileDialog({
setKeepResults(event.target.checked);
};

// Get results from the server if they are not provided
useEffect(() => {
if (shouldFetchEstimators) {
getJobResults({ jobId }, controller.signal, false)
.then(requestResults => {
if (requestResults) {
setFetchedResults(
prevResults =>
({
...prevResults,
estimators: requestResults.estimators
}) as FullSimulationData
);
}
})
.catch(error => {
console.error('Failed to fetch job results:', error);
});
}
}, [jobId, controller.signal, getJobResults, shouldFetchEstimators]);

return (
<CustomDialog
onClose={onClose}
Expand Down Expand Up @@ -103,7 +137,12 @@ export function SaveFileDialog({

if (yaptideEditor) {
yaptideEditor?.config.setKey('project/title', name);
saveJson(keepResults && results ? results : yaptideEditor?.toJSON(), name);
saveJson(
keepResults && fetchedResults
? fetchedResults
: yaptideEditor?.toJSON(),
name
);
}
}}>
Save
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';

import { useDialog } from '../../../../services/DialogService';
import { useLoader } from '../../../../services/LoaderService';
import { useShSimulation } from '../../../../services/ShSimulatorService';
import { useStore } from '../../../../services/StoreService';
import { YaptideEditor } from '../../../js/YaptideEditor';
import { EditorTitleBar } from './components/EditorTitlebar';
Expand All @@ -31,6 +32,7 @@ type AppBarOptions = {

function EditorAppBar({ editor }: AppBarProps) {
const { loadFromJson, loadFromFiles, loadFromUrl, loadFromJsonString } = useLoader();
const { getJobResults } = useShSimulation();
const { open: openTheOpenFileDialog } = useDialog('openFile');
const { open: openTheSaveFileDialog } = useDialog('saveFile');
const { open: openTheNewProjectDialog } = useDialog('newProject');
Expand Down Expand Up @@ -98,7 +100,8 @@ function EditorAppBar({ editor }: AppBarProps) {
label: 'Save as',
icon: <SaveAsIcon />,
disabled: false,
onClick: () => yaptideEditor && openTheSaveFileDialog({ yaptideEditor })
onClick: () =>
yaptideEditor && openTheSaveFileDialog({ yaptideEditor, getJobResults })
},
{
label: 'Redo (ctrl+y)',
Expand Down
4 changes: 1 addition & 3 deletions src/WrapperApp/components/InputEditor/InputFilesEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export function InputFilesEditor(props: InputFilesEditorProps) {
switch (props.simulator) {
case SimulatorType.SHIELDHIT:
return !(name in _defaultShInputFiles);
// Topas is not supported in current version of yaptide
// case SimulatorType.TOPAS:
// return !(name in _defaultTopasInputFiles);

case SimulatorType.FLUKA:
return !(name in _defaultFlukaInputFiles);
default:
Expand Down
77 changes: 77 additions & 0 deletions src/WrapperApp/components/Results/EstimatorTab/EstimatorTab.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Box, Card, CardContent, CircularProgress } from '@mui/material';

import { generateGraphs } from '../../../../JsRoot/GraphData';
import { FullSimulationData } from '../../../../services/ShSimulatorService';
import { TabPanel } from '../../Panels/TabPanel';
import { EstimatorResults } from '../ResultsPanel';
import TablePage0D from '../ResultsTable';

interface EstimatorTabProps {
estimator: EstimatorResults;
tabsValue: number;
resultsGeneratedFromProjectFile: boolean;
groupQuantities: boolean;
simulation: FullSimulationData;
}

const EstimatorTab = ({
estimator,
tabsValue,
resultsGeneratedFromProjectFile,
groupQuantities,
simulation
}: EstimatorTabProps) => {
return (
<Card
variant='outlined'
sx={{
'flexGrow': 1,
'margin': '0.5rem',
'bgcolor': theme =>
theme.palette.mode === 'dark' ? 'text.disabled' : 'background.paper',
'& .MuiCardContent-root div': {
bgcolor: 'transparent',
backgroundImage: 'none',
color: 'black'
}
}}>
{!estimator ? (
<Box
sx={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100vh'
}}>
<CircularProgress />
</Box>
) : (
<CardContent>
<TabPanel
key={`tab_panel_${estimator.name}_${tabsValue}`}
value={tabsValue}
index={tabsValue}
persistentIfVisited>
<Box
style={{
width: '100%',
display: 'flex',
flexDirection: 'column'
}}>
{estimator.tablePages.length > 0 && (
<TablePage0D estimator={estimator}></TablePage0D>
)}
{generateGraphs(
estimator,
resultsGeneratedFromProjectFile && groupQuantities,
simulation?.jobId
)}
</Box>
</TabPanel>
</CardContent>
)}
</Card>
);
};

export default EstimatorTab;
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { Card, CardContent, Tab, Tabs } from '@mui/material';
import { SyntheticEvent, useCallback, useEffect, useState } from 'react';

import { FullSimulationData, useShSimulation } from '../../../../services/ShSimulatorService';

interface EstimatorsTabProps {
tabsValue: number;
setTabsValue: (value: number) => void;
simulation: FullSimulationData;
setResultsSimulationData: React.Dispatch<React.SetStateAction<FullSimulationData | undefined>>;
estimatorsTabData: string[];
}

const EstimatorsSelect = ({
tabsValue,
setTabsValue,
simulation,
setResultsSimulationData,
estimatorsTabData
}: EstimatorsTabProps) => {
const [controller] = useState(new AbortController());

const { getJobResult } = useShSimulation();

const handleEstimatorTabChange = useCallback(
async (id: number) => {
const currentEstimatorData = simulation.estimators;
const estimatorExists =
currentEstimatorData[id] && currentEstimatorData[id].name !== '';

if (!estimatorExists) {
const simulationJobId = simulation.jobId;

const estimatorData = await getJobResult(
{
jobId: simulationJobId,
estimatorName: estimatorsTabData[id] + '_'
},
controller.signal,
true
);

const newEstimatorData = estimatorData?.estimators;

if (newEstimatorData) {
const newEstimators = [...currentEstimatorData];

while (newEstimators.length <= id) {
newEstimators.push({
name: '',
pages: []
});
}

newEstimators[id] = newEstimatorData[0];

const newSimulation = {
...simulation,
estimators: newEstimators
};
setResultsSimulationData(newSimulation);
}
}
},
[controller.signal, estimatorsTabData, getJobResult, setResultsSimulationData, simulation]
);

useEffect(() => {
handleEstimatorTabChange(0);
}, [handleEstimatorTabChange]);

const handleChange = (_event: SyntheticEvent, newValue: number) => {
setTabsValue(newValue);
};

return (
<Card
sx={{
margin: '0.5rem',
height: 'min-content',
overflow: 'unset',
position: 'sticky',
top: '8px',
zIndex: 1
}}>
<CardContent
sx={{
color: theme =>
theme.palette.mode === 'dark' ? 'secondary.contrastText' : 'secondary.dark'
}}>
<Tabs
textColor='inherit'
sx={{
'flexShrink': 0,
'& .MuiTabs-indicator': {
backgroundColor: theme =>
theme.palette.mode === 'dark'
? 'secondary.contrastText'
: 'secondary.dark'
},
'& .MuiButtonBase-root': {
fontWeight: 'bold'
}
}}
orientation='vertical'
variant='scrollable'
value={tabsValue}
onChange={handleChange}>
{estimatorsTabData.map((estimatorName, id) => {
return (
<Tab
key={`tab_${estimatorName}`}
label={estimatorName}
value={id}
onClick={() => handleEstimatorTabChange(id)}
/>
);
})}
</Tabs>
</CardContent>
</Card>
);
};

export default EstimatorsSelect;
Loading
Loading