import React, { ReactElement, useState, useEffect, useCallback } from 'react'; import { VolumeLightingProps } from '../../types/ViewportPresets'; import { Numeric } from '@ohif/ui-next'; import { useSystem } from '@ohif/core'; import { useTranslation } from 'react-i18next'; export function VolumeLighting({ viewportId, hasShade }: VolumeLightingProps): ReactElement { const { servicesManager, commandsManager } = useSystem(); const { cornerstoneViewportService } = servicesManager.services; const [lightingValues, setLightingValues] = useState({ ambient: null, diffuse: null, specular: null, }); const { t } = useTranslation('WindowLevelActionMenu'); // Single callback to handle all lighting property changes const onLightingChange = useCallback( (property, value) => { commandsManager.runCommand('setVolumeLighting', { viewportId, options: { [property]: value }, }); setLightingValues(prev => ({ ...prev, [property]: value, })); }, [commandsManager, viewportId] ); useEffect(() => { const viewport = cornerstoneViewportService.getCornerstoneViewport(viewportId); const { actor } = viewport.getActors()[0]; const property = actor.getProperty(); const values = { ambient: property.getAmbient(), diffuse: property.getDiffuse(), specular: property.getSpecular(), }; setLightingValues(values); }, [viewportId, cornerstoneViewportService]); const disableOption = hasShade ? '' : 'ohif-disabled !opacity-40'; // Configuration for our lighting properties const lightingProperties = [ { key: 'ambient', label: t('Ambient') }, { key: 'diffuse', label: t('Diffuse') }, { key: 'specular', label: t('Specular') }, ]; return (
{lightingProperties.map( ({ key, label }) => lightingValues[key] !== null && (
onLightingChange(key, value)} >
{label}
) )}
); }