Add Twenty Shared & Fix profile image rendering (#8841)
PR Summary: 1. Added `Twenty Shared` Package to centralize utilitiies as mentioned in #8942 2. Optimization of `getImageAbsoluteURI.ts` to handle edge cases  --------- Co-authored-by: Antoine Moreaux <moreaux.antoine@gmail.com> Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
committed by
GitHub
parent
4e329d08b0
commit
08a9db2df6
@ -0,0 +1,38 @@
|
||||
import { getImageAbsoluteURI } from '../getImageAbsoluteURI';
|
||||
|
||||
describe('getImageAbsoluteURI', () => {
|
||||
it('should return baseUrl if imageUrl is empty string', () => {
|
||||
const imageUrl = '';
|
||||
const baseUrl = 'http://localhost:3000';
|
||||
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
|
||||
expect(result).toBe('http://localhost:3000/files/');
|
||||
});
|
||||
|
||||
it('should return absolute url if the imageUrl is an absolute url', () => {
|
||||
const imageUrl = 'https://XXX';
|
||||
const baseUrl = 'http://localhost:3000';
|
||||
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
|
||||
expect(result).toBe(imageUrl);
|
||||
});
|
||||
|
||||
it('should return fully formed url if imageUrl is a relative url starting with /', () => {
|
||||
const imageUrl = '/path/pic.png';
|
||||
const baseUrl = 'http://localhost:3000';
|
||||
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
|
||||
expect(result).toBe('http://localhost:3000/files/path/pic.png');
|
||||
});
|
||||
|
||||
it('should return fully formed url if imageUrl is a relative url nost starting with slash', () => {
|
||||
const imageUrl = 'pic.png';
|
||||
const baseUrl = 'http://localhost:3000';
|
||||
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
|
||||
expect(result).toBe('http://localhost:3000/files/pic.png');
|
||||
});
|
||||
|
||||
it('should handle queryParameters in the imageUrl', () => {
|
||||
const imageUrl = '/pic.png?token=XXX';
|
||||
const baseUrl = 'http://localhost:3000';
|
||||
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
|
||||
expect(result).toBe('http://localhost:3000/files/pic.png?token=XXX');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,19 @@
|
||||
type getImageAbsoluteURIProps = {
|
||||
imageUrl: string;
|
||||
baseUrl: string;
|
||||
};
|
||||
|
||||
export const getImageAbsoluteURI = ({
|
||||
imageUrl,
|
||||
baseUrl,
|
||||
}: getImageAbsoluteURIProps): string => {
|
||||
if (imageUrl.startsWith('https:') || imageUrl.startsWith('http:')) {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
if (imageUrl.startsWith('/')) {
|
||||
return new URL(`/files${imageUrl}`, baseUrl).toString();
|
||||
}
|
||||
|
||||
return new URL(`/files/${imageUrl}`, baseUrl).toString();
|
||||
};
|
||||
Reference in New Issue
Block a user