Issue in getting the patient name in my new extension

Describe Your Question

I am Shivayogi. Trying to develop my own extension for my one of the use case. I am using ohif 3.10 version. I followed all the steps on creating the extensions. I am able to register the extension without any issues. But as part of extension I suppose to show the patient details in the viewer (that is basic mode .i.e logitudinal). How do i get the patient info in my extension? I can see the code exist in the extension default. Since I have pearDependecy set in the cli created code I cannot access the hook provided in the default extension by importing the default extension. What i am trying to do to access the usePatientInfo through below code
const usePatientInfo = () => {
console.log(‘TEST Yogi usePatientInfo called’);
if (typeof window === ‘undefined’) {
return { patientInfo: null, isMixedPatients: false };
}

const ohif = (window as any).OHIF;

if (!ohif) {
return { patientInfo: null, isMixedPatients: false };
}

if (ohif.extensions) {
if (ohif.extensions[‘@ohif/extension-default’]) {
if (ohif.extensions[‘@ohif/extension-default’].usePatientInfo) {
try {
const result = ohif.extensions[‘@ohif/extension-default’].usePatientInfo();
return result;
} catch (error) {
return { patientInfo: null, isMixedPatients: false };
}
}
}
}

// Fallback for build time
return { patientInfo: null, isMixedPatients: false };
};

const { patientInfo: headerPatientInfo, isMixedPatients } = usePatientInfo();

// Alternative: Try to get patient info directly from services if hook fails
const [alternativePatientInfo, setAlternativePatientInfo] = useState(null);

useEffect(() => {
if (!headerPatientInfo && servicesManager) {
try {
const { displaySetService } = servicesManager.services;
if (displaySetService) {
const activeDisplaySets = displaySetService.getActiveDisplaySets();
if (activeDisplaySets && activeDisplaySets.length > 0) {
const firstDisplaySet = activeDisplaySets[0];
if (firstDisplaySet.instances && firstDisplaySet.instances.length > 0) {
const instance = firstDisplaySet.instances[0];
setAlternativePatientInfo(instance);
}
}
}
} catch (e) {
// Alternative approach failed
}
}
}, [headerPatientInfo, servicesManager]);
// Helper function to get OHIF utilities
const getOHIFUtils = () => {
if (typeof window === ‘undefined’) {
return null;
}

const ohif = (window as any).OHIF;

if (!ohif) {
  return null;
}

// Try to get utils from main OHIF object
if (ohif.utils?.formatPN) {
  return { formatPN: ohif.utils.formatPN, source: 'main' };
}

// Try to get utils from extension
if (ohif.extensions?.['@ohif/extension-default']?.utils?.formatPN) {
  return { formatPN: ohif.extensions['@ohif/extension-default'].utils.formatPN, source: 'extension' };
}

return null;

};

// Helper functions to format DICOM data
const _formatPatientName = (patientName: any): string => {
console.log(‘TEST Yogi _formatPatientName called with:’, patientName, ‘Type:’, typeof patientName);

if (!patientName) return 'Unknown';

// Try to use OHIF's built-in formatPN utility function first
const ohifUtils = getOHIFUtils();


if (ohifUtils) {
  try {
    const formattedName = ohifUtils.formatPN(patientName);
    
    if (formattedName && formattedName !== '[object Object]') {
      return formattedName;
    }
  } catch (error) {
    // Error using OHIF formatPN utility
  }
}

// Fallback to our custom logic if OHIF utilities are not available
if (typeof patientName === 'string') {
  return patientName;
}
if (Array.isArray(patientName)) {
  return patientName.join(' ');
}

// Handle DICOM person name object
if (typeof patientName === 'object') {
  try {
    // OHIF 3.10 typically stores the formatted name in specific properties
    // Try the most common patterns first
    if (patientName.Alphabetic) {
      return patientName.Alphabetic;
    }
    
    if (patientName.value) {
      return patientName.value;
    }

    // Try to access properties that might be hidden (for Proxy objects)
    // Check for common DICOM person name properties
    const possibleNames = [
      patientName.Alphabetic,
      patientName.Ideographic,
      patientName.Phonetic,
      patientName.value,
      patientName.name,
      patientName.displayName,
      patientName.text,
      patientName._value,
      patientName._alphabetic,
      patientName.toString && patientName.toString(),
      patientName.toString && patientName.toString()
    ];

    for (const name of possibleNames) {
      if (name && typeof name === 'string' && name.trim() && name !== '[object Object]') {
        return name.trim();
      }
    }

    // If no direct properties, try to extract from the object
    const keys = Object.keys(patientName);
    for (const key of keys) {
      const value = patientName[key];
      if (value && typeof value === 'string' && value.trim() && value !== '[object Object]') {
        return value.trim();
      }
    }

    // Try to access Symbol properties (some objects use symbols for internal properties)
    try {
      const symbols = Object.getOwnPropertySymbols(patientName);
      for (const symbol of symbols) {
        const value = patientName[symbol];
        if (value && typeof value === 'string' && value.trim() && value !== '[object Object]') {
          return value.trim();
        }
      }
    } catch (e) {
      // Could not access symbol properties
    }

    // Last resort: try to get the string representation
    const stringRep = String(patientName);
    if (stringRep && stringRep !== '[object Object]' && stringRep !== 'Unknown') {
      return stringRep;
    }
    
    // Last resort: try to extract any string value from the object
    try {
      const allValues = [];
      for (const key in patientName) {
        const value = patientName[key];
        if (typeof value === 'string' && value.trim() && value !== '[object Object]') {
          allValues.push(value);
        }
      }
      if (allValues.length > 0) {
        return allValues[0]; // Return the first valid string value
      }
    } catch (e) {
      // Error extracting string values
    }
  } catch (error) {
    // Error processing patient name object
  }
}

return 'Unknown';

};
I alway get patient name : [object object] instead of the actual name.
I am able to get the patient id and modality without any issues.
Please help me to fix the issue.
Also I need to know can i set default extension as dependency instead of pearDependency in my package.json?