From 0b00ae065d3272f1dfaa4f6c6656c7a8f8c02548 Mon Sep 17 00:00:00 2001 From: gitstart-twenty Date: Fri, 26 Jul 2024 16:42:03 +0000 Subject: [PATCH] use `getImageAbsoluteURI` inside Avatar-like components --- .../calendar/components/CalendarEventRow.tsx | 3 +- .../activities/comment/CommentHeader.tsx | 3 +- .../activities/components/ParticipantChip.tsx | 3 +- .../emails/components/EmailThreadPreview.tsx | 9 ++---- .../timeline/components/TimelineActivity.tsx | 5 +--- .../favorites/components/Favorites.tsx | 3 +- .../components/GenericEntityFilterChip.tsx | 4 +-- .../MultipleObjectRecordSelectItem.tsx | 3 +- .../components/SelectableMenuItemSelect.tsx | 3 +- .../MultipleRecordSelectDropdown.tsx | 3 +- .../components/ProfilePictureUploader.tsx | 3 +- .../components/WorkspaceLogoUploader.tsx | 3 +- .../ui/input/components/ImageInput.tsx | 15 ++++++---- .../src/modules/users/components/UserChip.tsx | 4 +-- .../components/WorkspaceMemberCard.tsx | 3 +- .../src/display/avatar/components/Avatar.tsx | 20 ++++++++----- .../twenty-ui/src/utilities/config/index.ts | 30 +++++++++++++++++++ .../utilities/image/getImageAbsoluteURI.ts | 15 ++++++++++ packages/twenty-ui/src/utilities/index.ts | 1 + 19 files changed, 84 insertions(+), 49 deletions(-) create mode 100644 packages/twenty-ui/src/utilities/config/index.ts create mode 100644 packages/twenty-ui/src/utilities/image/getImageAbsoluteURI.ts diff --git a/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventRow.tsx b/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventRow.tsx index 3185c3c71b50..bdaf54830ddd 100644 --- a/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventRow.tsx +++ b/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventRow.tsx @@ -16,7 +16,6 @@ import { Card } from '@/ui/layout/card/components/Card'; import { CardContent } from '@/ui/layout/card/components/CardContent'; import { TimelineCalendarEvent } from '~/generated-metadata/graphql'; import { CalendarChannelVisibility } from '~/generated/graphql'; -import { getImageAbsoluteURI } from '~/utils/image/getImageAbsoluteURI'; import { isDefined } from '~/utils/isDefined'; type CalendarEventRowProps = { @@ -163,7 +162,7 @@ export const CalendarEventRow = ({ key={[participant.workspaceMemberId, participant.displayName] .filter(isDefined) .join('-')} - avatarUrl={getImageAbsoluteURI(participant.avatarUrl)} + avatarUrl={participant.avatarUrl} placeholder={ participant.firstName && participant.lastName ? `${participant.firstName} ${participant.lastName}` diff --git a/packages/twenty-front/src/modules/activities/comment/CommentHeader.tsx b/packages/twenty-front/src/modules/activities/comment/CommentHeader.tsx index 86a02f2b1e7a..c40cd8f26a57 100644 --- a/packages/twenty-front/src/modules/activities/comment/CommentHeader.tsx +++ b/packages/twenty-front/src/modules/activities/comment/CommentHeader.tsx @@ -6,7 +6,6 @@ import { beautifyExactDateTime, beautifyPastDateRelativeToNow, } from '~/utils/date-utils'; -import { getImageAbsoluteURI } from '~/utils/image/getImageAbsoluteURI'; const StyledContainer = styled.div` align-items: center; @@ -60,7 +59,7 @@ export const CommentHeader = ({ comment, actionBar }: CommentHeaderProps) => { theme.spacing(1)}; @@ -74,7 +73,7 @@ export const ParticipantChip = ({ ) : ( {thread?.lastTwoParticipants?.[0] && ( )} {finalDisplayedName && ( { Icon={() => ( ); diff --git a/packages/twenty-front/src/modules/object-record/relation-picker/components/MultipleObjectRecordSelectItem.tsx b/packages/twenty-front/src/modules/object-record/relation-picker/components/MultipleObjectRecordSelectItem.tsx index 7d73fb6ab06f..fc4061cfc9fd 100644 --- a/packages/twenty-front/src/modules/object-record/relation-picker/components/MultipleObjectRecordSelectItem.tsx +++ b/packages/twenty-front/src/modules/object-record/relation-picker/components/MultipleObjectRecordSelectItem.tsx @@ -10,7 +10,6 @@ import { SelectableItem } from '@/ui/layout/selectable-list/components/Selectabl import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList'; import { MenuItemMultiSelectAvatar } from '@/ui/navigation/menu-item/components/MenuItemMultiSelectAvatar'; import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId'; -import { getImageAbsoluteURI } from '~/utils/image/getImageAbsoluteURI'; import { isDefined } from '~/utils/isDefined'; export const StyledSelectableItem = styled(SelectableItem)` @@ -65,7 +64,7 @@ export const MultipleObjectRecordSelectItem = ({ selected={selected} avatar={ { return ( { @@ -57,7 +56,7 @@ export const WorkspaceLogoUploader = () => { return ( diff --git a/packages/twenty-front/src/modules/ui/input/components/ImageInput.tsx b/packages/twenty-front/src/modules/ui/input/components/ImageInput.tsx index c992e5381197..df318dd3b6e7 100644 --- a/packages/twenty-front/src/modules/ui/input/components/ImageInput.tsx +++ b/packages/twenty-front/src/modules/ui/input/components/ImageInput.tsx @@ -1,9 +1,10 @@ -import React from 'react'; import { useTheme } from '@emotion/react'; import styled from '@emotion/styled'; +import React, { useMemo } from 'react'; import { IconFileUpload, IconTrash, IconUpload, IconX } from 'twenty-ui'; import { Button } from '@/ui/input/button/components/Button'; +import { getImageAbsoluteURI } from '~/utils/image/getImageAbsoluteURI'; import { isDefined } from '~/utils/isDefined'; const StyledContainer = styled.div` @@ -105,16 +106,18 @@ export const ImageInput = ({ hiddenFileInput.current?.click(); }; + const pictureURI = useMemo(() => getImageAbsoluteURI(picture), [picture]); + return ( - {picture ? ( + {pictureURI ? ( profile ) : ( @@ -139,7 +142,7 @@ export const ImageInput = ({ onClick={onAbort} variant="secondary" title="Abort" - disabled={!picture || disabled} + disabled={!pictureURI || disabled} fullWidth /> ) : ( @@ -157,7 +160,7 @@ export const ImageInput = ({ onClick={onRemove} variant="secondary" title="Remove" - disabled={!picture || disabled} + disabled={!pictureURI || disabled} fullWidth /> diff --git a/packages/twenty-front/src/modules/users/components/UserChip.tsx b/packages/twenty-front/src/modules/users/components/UserChip.tsx index a14f0ae4eb5a..58460c4d1dc7 100644 --- a/packages/twenty-front/src/modules/users/components/UserChip.tsx +++ b/packages/twenty-front/src/modules/users/components/UserChip.tsx @@ -1,7 +1,5 @@ import { AvatarChip } from 'twenty-ui'; -import { getImageAbsoluteURI } from '~/utils/image/getImageAbsoluteURI'; - export type UserChipProps = { id: string; name: string; @@ -13,6 +11,6 @@ export const UserChip = ({ id, name, avatarUrl }: UserChipProps) => ( placeholderColorSeed={id} name={name} avatarType="rounded" - avatarUrl={getImageAbsoluteURI(avatarUrl) || ''} + avatarUrl={avatarUrl} /> ); diff --git a/packages/twenty-front/src/modules/workspace/components/WorkspaceMemberCard.tsx b/packages/twenty-front/src/modules/workspace/components/WorkspaceMemberCard.tsx index 94c9dcee1ac3..6ed05ce15f39 100644 --- a/packages/twenty-front/src/modules/workspace/components/WorkspaceMemberCard.tsx +++ b/packages/twenty-front/src/modules/workspace/components/WorkspaceMemberCard.tsx @@ -2,7 +2,6 @@ import styled from '@emotion/styled'; import { Avatar, OverflowingTextWithTooltip } from 'twenty-ui'; import { WorkspaceMember } from '@/workspace-member/types/WorkspaceMember'; -import { getImageAbsoluteURI } from '~/utils/image/getImageAbsoluteURI'; const StyledContainer = styled.div` background: ${({ theme }) => theme.background.secondary}; @@ -39,7 +38,7 @@ export const WorkspaceMemberCard = ({ }: WorkspaceMemberCardProps) => ( getImageAbsoluteURI(avatarUrl), + [avatarUrl], + ); + + const noAvatarUrl = !isNonEmptyString(avatarImageURI); const placeholderChar = placeholder?.[0]?.toLocaleUpperCase(); - const showPlaceholder = noAvatarUrl || invalidAvatarUrls.includes(avatarUrl); + const showPlaceholder = + noAvatarUrl || invalidAvatarUrls.includes(avatarImageURI); const handleImageError = () => { - if (isNonEmptyString(avatarUrl)) { - setInvalidAvatarUrls((prev) => [...prev, avatarUrl]); + if (isNonEmptyString(avatarImageURI)) { + setInvalidAvatarUrls((prev) => [...prev, avatarImageURI]); } }; @@ -105,7 +111,7 @@ export const Avatar = ({ {showPlaceholder ? ( placeholderChar ) : ( - + )} ); diff --git a/packages/twenty-ui/src/utilities/config/index.ts b/packages/twenty-ui/src/utilities/config/index.ts new file mode 100644 index 000000000000..4525e814d61b --- /dev/null +++ b/packages/twenty-ui/src/utilities/config/index.ts @@ -0,0 +1,30 @@ +declare global { + interface Window { + _env_?: Record; + __APOLLO_CLIENT__?: any; + } +} + +const getDefaultUrl = () => { + if ( + window.location.hostname === 'localhost' || + window.location.hostname === '127.0.0.1' + ) { + // In development environment front and backend usually run on separate ports + // we set the default value to localhost:3000. + // It dev context, we use env vars to overwrite it + return 'http://localhost:3000'; + } else { + // Outside of localhost we assume that they run on the same port + // because the backend will serve the frontend + // It prod context, we use env-config.js + window var to ovewrite it + return `${window.location.protocol}//${window.location.hostname}${ + window.location.port ? `:${window.location.port}` : '' + }`; + } +}; + +export const REACT_APP_SERVER_BASE_URL = + window._env_?.REACT_APP_SERVER_BASE_URL || + process.env.REACT_APP_SERVER_BASE_URL || + getDefaultUrl(); diff --git a/packages/twenty-ui/src/utilities/image/getImageAbsoluteURI.ts b/packages/twenty-ui/src/utilities/image/getImageAbsoluteURI.ts new file mode 100644 index 000000000000..879447d30215 --- /dev/null +++ b/packages/twenty-ui/src/utilities/image/getImageAbsoluteURI.ts @@ -0,0 +1,15 @@ +import { REACT_APP_SERVER_BASE_URL } from '@ui/utilities/config'; + +export const getImageAbsoluteURI = (imageUrl?: string | null) => { + if (!imageUrl) { + return null; + } + + if (imageUrl?.startsWith('https:')) { + return imageUrl; + } + + const serverFilesUrl = REACT_APP_SERVER_BASE_URL; + + return `${serverFilesUrl}/files/${imageUrl}`; +}; diff --git a/packages/twenty-ui/src/utilities/index.ts b/packages/twenty-ui/src/utilities/index.ts index 3d99deafeead..38cd4a1b600a 100644 --- a/packages/twenty-ui/src/utilities/index.ts +++ b/packages/twenty-ui/src/utilities/index.ts @@ -1,4 +1,5 @@ export * from './color/utils/stringToHslColor'; +export * from './image/getImageAbsoluteURI'; export * from './isDefined'; export * from './state/utils/createState'; export * from './types/Nullable';