-
Notifications
You must be signed in to change notification settings - Fork 10
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
19 changed files
with
124 additions
and
6 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
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,39 @@ | ||
import { Image, THUMBNAIL_RESIZE_RATIO } from '@monkvision/types'; | ||
import { useCallback } from 'react'; | ||
import { useObjectMemo } from './useObjectMemo'; | ||
|
||
/** | ||
* The result of the useThumbnail. It contains a function which takes a Image object and return the | ||
* thumbnail url. | ||
*/ | ||
export interface ThumbnailResult { | ||
/** | ||
* Function that generates the thumbnail image URL. | ||
*/ | ||
getThumbnailUrl: (image: Image) => string; | ||
} | ||
|
||
function getResizedDimension(originalDimension: number): number { | ||
return Math.round(originalDimension * THUMBNAIL_RESIZE_RATIO); | ||
} | ||
|
||
/** | ||
* Custom hook used to get a function getThumbnailUrl that generates a thumbnail URL. | ||
*/ | ||
export function useThumbnail(thumbnailDomain: string): ThumbnailResult { | ||
const getThumbnailUrl = useCallback( | ||
(image: Image) => { | ||
const baseThumbnailUrl = `https://${thumbnailDomain}${ | ||
thumbnailDomain.endsWith('/') ? '' : '/' | ||
}`; | ||
const imageUrlParam = `image_url=${encodeURIComponent(image.path)}`; | ||
const widthUrlParam = `width=${getResizedDimension(image.width)}`; | ||
const heightUrlParam = `height=${getResizedDimension(image.height)}`; | ||
|
||
return `${baseThumbnailUrl}?${imageUrlParam}&${widthUrlParam}&${heightUrlParam}`; | ||
}, | ||
[thumbnailDomain], | ||
); | ||
|
||
return useObjectMemo({ getThumbnailUrl }); | ||
} |
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,26 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { Image, THUMBNAIL_RESIZE_RATIO } from '@monkvision/types'; | ||
import { useThumbnail } from '../../src'; | ||
|
||
const thumbnailDomainMock = 'test-thumbnail-domain'; | ||
|
||
describe('useThumbnail hook', () => { | ||
it('should return a getThumbnailUrl function', () => { | ||
const { result, unmount } = renderHook(useThumbnail); | ||
|
||
expect(typeof result.current.getThumbnailUrl).toBe('function'); | ||
unmount(); | ||
}); | ||
|
||
it('should return a formated url', () => { | ||
const { result, unmount } = renderHook(useThumbnail, { initialProps: thumbnailDomainMock }); | ||
|
||
const imageMock = { path: 'test-path', width: 20, height: 40 } as Image; | ||
expect(result.current.getThumbnailUrl(imageMock)).toEqual( | ||
`https://${thumbnailDomainMock}/?image_url=${imageMock.path}&width=${ | ||
imageMock.width * THUMBNAIL_RESIZE_RATIO | ||
}&height=${imageMock.height * THUMBNAIL_RESIZE_RATIO}`, | ||
); | ||
unmount(); | ||
}); | ||
}); |
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