-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
packages/lib-subject-viewers/src/hooks/useVolumetricSubject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Inspired by useSubjectJSON.js in the lib-classifier package | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const useVolumetricSubject = ({ onError, onReady, subject }) => { | ||
const [error, setError] = useState() | ||
const [data, setData] = useState() | ||
|
||
useEffect(() => { | ||
setData(null) | ||
setError(null) | ||
|
||
if (!subject) return setError('No subject found') | ||
// subjectJSON is used for testing to avoid network requests | ||
if (subject?.subjectJSON) return setData(subject.subjectJSON) | ||
|
||
const jsonLocation = | ||
subject.locations.find( | ||
(l) => l.type === 'application' || l.type === 'text' | ||
) || {}; | ||
|
||
if (!jsonLocation.url) return setError('No JSON url found for this subject') | ||
|
||
fetch(jsonLocation.url) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setData(data) | ||
onReady(); | ||
}) | ||
.catch((err) => { | ||
console.log('useVolumetricSubject() error', err) | ||
onError(err) | ||
}); | ||
}, [subject]); | ||
|
||
return { | ||
data: data, | ||
loading: !data && !error, | ||
error | ||
}; | ||
}; |