Fix broken image urls in Settings > Profile and Invite To Workspace Email (#8942)

Fixes #8601

We had 3 implementations of getImageAbsoluteURI: in twenty-front, in
twenty-ui and in twenty-emails. I was able to remove the one in
twenty-front but I could not remove it from twenty-emails as this is a
standalone for now. The vision is to introduce shared utils in a
twenty-shared package
This commit is contained in:
Charles Bochet
2024-12-07 14:57:32 +01:00
committed by GitHub
parent ab8ad46685
commit 99f53a5030
17 changed files with 72 additions and 84 deletions

View File

@ -1,21 +0,0 @@
import { getImageAbsoluteURI } from '../getImageAbsoluteURI';
describe('getImageAbsoluteURI', () => {
it('should return null if imageUrl is null', () => {
const imageUrl = null;
const result = getImageAbsoluteURI(imageUrl);
expect(result).toBeNull();
});
it('should return absolute url if the imageUrl is an absolute url', () => {
const imageUrl = 'https://XXX';
const result = getImageAbsoluteURI(imageUrl);
expect(result).toBe(imageUrl);
});
it('should return fully formed url if imageUrl is a relative url', () => {
const imageUrl = 'XXX';
const result = getImageAbsoluteURI(imageUrl);
expect(result).toBe('http://localhost:3000/files/XXX');
});
});

View File

@ -1,26 +0,0 @@
import { REACT_APP_SERVER_BASE_URL } from '~/config';
type ImageAbsoluteURI<T extends string | null | undefined> = T extends string
? string
: null;
export const getImageAbsoluteURI = <T extends string | null | undefined>(
imageUrl: T,
): ImageAbsoluteURI<T> => {
if (!imageUrl) {
return null as ImageAbsoluteURI<T>;
}
if (imageUrl.startsWith('https:') || imageUrl.startsWith('http:')) {
return imageUrl as ImageAbsoluteURI<T>;
}
const serverFilesUrl = new URL(REACT_APP_SERVER_BASE_URL);
serverFilesUrl.pathname = `/files/`;
serverFilesUrl.pathname += imageUrl.startsWith('/')
? imageUrl.slice(1)
: imageUrl;
return serverFilesUrl.toString() as ImageAbsoluteURI<T>;
};