-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new image component that loads the images via object URL. This is a step towards loading images that are cached locally in indexeddb.
- Loading branch information
1 parent
56005dc
commit 2a6ef1c
Showing
11 changed files
with
244 additions
and
89 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 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,65 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { IGalleryItem } from "../lib/gallery-item"; | ||
import { useGallery } from "../context/gallery-context"; | ||
|
||
export interface IImageProps { | ||
// | ||
// Test ID for the image attribute. | ||
// | ||
testId?: string; | ||
|
||
// | ||
// Class name for the image attribute. | ||
// | ||
imgClassName?: string; | ||
|
||
// | ||
// The asset being displayed. | ||
// | ||
asset: IGalleryItem; | ||
|
||
// | ||
// Event raised when an item in the gallery has been clicked. | ||
// | ||
onClick?: (() => void); | ||
} | ||
|
||
// | ||
// Renders an image. | ||
// | ||
export function Image({ testId, imgClassName, asset, onClick }: IImageProps) { | ||
|
||
const [objectURL, setObjectURL] = useState<string>(""); | ||
|
||
const { source } = useGallery(); | ||
|
||
useEffect(() => { | ||
source.loadAsset(asset._id, objectURL => { | ||
setObjectURL(objectURL); | ||
}); | ||
|
||
return () => { | ||
source.unloadAsset(asset._id); | ||
}; | ||
}, [asset]); | ||
|
||
return ( | ||
<> | ||
{objectURL | ||
&& <img | ||
data-testid={testId} | ||
className={imgClassName} | ||
src={objectURL} | ||
style={{ | ||
padding: "2px", | ||
}} | ||
onClick={() => { | ||
if (onClick) { | ||
onClick(); | ||
} | ||
}} | ||
/> | ||
} | ||
</> | ||
); | ||
}; |
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
Oops, something went wrong.