-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from recogito/develop
Develop to main
- Loading branch information
Showing
5 changed files
with
212 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { supabase } from '@backend/supabaseBrowserClient'; | ||
import type { Document } from 'src/Types'; | ||
import { Spinner } from '@components/Spinner'; | ||
|
||
const CANTALOUPE_PATH: string | undefined = import.meta.env.PUBLIC_IIIF_CANTALOUPE_PATH; | ||
|
||
interface IIIFImageThumbnailProps { | ||
|
||
document: Document; | ||
|
||
} | ||
|
||
export const IIIFImageThumbnail = (props: IIIFImageThumbnailProps) => { | ||
|
||
const { document } = props; | ||
|
||
const [authToken, setAuthToken] = useState<string | undefined>(); | ||
|
||
const [thumbnailBlob, setThumbnailBlob] = useState<string | undefined>(); | ||
|
||
const isUploadedFile = document.content_type?.startsWith('image/'); | ||
|
||
const imageManifest = isUploadedFile | ||
? `${CANTALOUPE_PATH}/${document.id}/info.json` | ||
: document.meta_data?.url; | ||
|
||
const thumbnailURL = imageManifest?.replace('/info.json', '/square/max/0/default.jpg'); | ||
|
||
useEffect(() => { | ||
// Standard image tag | ||
if (!isUploadedFile || !thumbnailURL) return; | ||
|
||
supabase.auth.getSession().then(({ error, data }) => { | ||
if (error) { | ||
// Shouldn't really happen at this point | ||
console.error(error); | ||
} else { | ||
setAuthToken(data.session?.access_token); | ||
} | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!authToken || !thumbnailURL) return; | ||
|
||
fetch(thumbnailURL, { | ||
headers: { | ||
'Authorization': `Bearer ${authToken}` | ||
} | ||
}).then(res => { | ||
if (!res.ok) { | ||
console.error('Failed thumbnail download', res); | ||
} else { | ||
res.blob().then(blob => { | ||
const objectURL = URL.createObjectURL(blob); | ||
setThumbnailBlob(objectURL); | ||
}); | ||
} | ||
}).catch(error => { | ||
console.error('Failed thumbnail download', error); | ||
}); | ||
}, [authToken]); | ||
|
||
useEffect(() => { | ||
// Cleanup: free resources properly on unmount | ||
if (!thumbnailBlob) return; | ||
|
||
return () => { | ||
URL.revokeObjectURL(thumbnailBlob); | ||
} | ||
}, [thumbnailBlob]); | ||
|
||
return isUploadedFile ? ( | ||
<div className='document-card-image-container'> | ||
{!thumbnailBlob ? ( | ||
<div className="spinner-container"> | ||
<Spinner className='search-icon spinner' size={14} /> | ||
</div> | ||
) : ( | ||
<img | ||
src={thumbnailBlob} | ||
height={200} | ||
width={200} | ||
/> | ||
)} | ||
</div> | ||
) : ( | ||
<div className='document-card-image-container'> | ||
<img | ||
src={thumbnailURL} | ||
height={200} | ||
width={200} | ||
/> | ||
</div> | ||
); | ||
|
||
} |
Oops, something went wrong.