From 29347fc70fb6abdeb8cabe97ffb74889f09b969f Mon Sep 17 00:00:00 2001 From: Peter Szerzo Date: Wed, 31 Jul 2024 16:12:10 +0200 Subject: [PATCH] Fail-safe icon url --- .../src/ui/components/index.tsx | 38 ++++++++++++++----- packages/journey-manager/src/ui/style.css | 6 +++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/packages/journey-manager/src/ui/components/index.tsx b/packages/journey-manager/src/ui/components/index.tsx index 1b6146e9..0aed4a66 100644 --- a/packages/journey-manager/src/ui/components/index.tsx +++ b/packages/journey-manager/src/ui/components/index.tsx @@ -284,6 +284,34 @@ const retrieveNudgeState = (conversationId: string): NudgeState => { return "hidden"; }; +const PinIcon: FC<{ iconUrl?: string }> = ({ iconUrl }) => { + const [isIconUrlValid, setIsIconUrlValid] = useState(null); + + useEffect(() => { + if (iconUrl == null) { + return; + } + const image = new Image(); + image.onload = () => { + setIsIconUrlValid(true); + }; + image.onerror = () => { + setIsIconUrlValid(false); + }; + image.src = iconUrl; + }, [iconUrl, setIsIconUrlValid]); + + if (iconUrl == null || isIconUrlValid === false) { + return ; + } + + if (isIconUrlValid == null) { + return null; + } + + return ; +}; + export const ControlCenter: FC<{ config: UiConfig; conversationId: string; @@ -384,15 +412,7 @@ export const ControlCenter: FC<{ setIsOpen((prev) => !prev); }} > - {config.iconUrl != null ? ( - - ) : ( - - )} +