Skip to content

Commit

Permalink
fix(CalloutBlock): unregister assets on switch off (#974)
Browse files Browse the repository at this point in the history
* fix(CalloutBlock): unregister assets on switch off

* fix review
  • Loading branch information
fulopdaniel authored Dec 5, 2024
1 parent 609001d commit 53f5630
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
14 changes: 12 additions & 2 deletions packages/callout-block/src/CalloutBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* (c) Copyright Frontify Ltd., all rights reserved. */

import { useBlockSettings, useEditorState } from '@frontify/app-bridge';
import { useBlockAssets, useBlockSettings, useEditorState } from '@frontify/app-bridge';

import {
BlockProps,
Expand All @@ -20,6 +20,7 @@ import { Appearance, BlockSettings, Icon, Width, alignmentMap, outerWidthMap, pa
import '@frontify/fondue/style';
import '@frontify/guideline-blocks-settings/styles';
import 'tailwindcss/tailwind.css';
import { ICON_ASSET_ID } from './settings';

export const CalloutBlock = ({ appBridge }: BlockProps): ReactElement => {
const [backgroundColor, setBackgroundColor] = useState<string>('');
Expand All @@ -28,6 +29,15 @@ export const CalloutBlock = ({ appBridge }: BlockProps): ReactElement => {
const { type, appearance } = blockSettings;
const isEditing = useEditorState(appBridge);

const { blockAssets, deleteAssetIdsFromKey } = useBlockAssets(appBridge);
const customIcon = blockAssets?.[ICON_ASSET_ID]?.[0];

useEffect(() => {
if (!blockSettings.iconSwitch && customIcon && isEditing) {
deleteAssetIdsFromKey(ICON_ASSET_ID, [customIcon.id]);
}
}, [blockSettings.iconSwitch, customIcon, deleteAssetIdsFromKey, isEditing]);

useEffect(() => {
const updateStyles = () => {
const { backgroundColor, textColor } = computeStyles(type, appearance);
Expand Down Expand Up @@ -116,9 +126,9 @@ export const CalloutBlock = ({ appBridge }: BlockProps): ReactElement => {
<div data-test-id="callout-content" className={textDivClassNames}>
{iconType !== Icon.None && (
<CalloutIcon
appBridge={appBridge}
isActive={hasRichTextValue(blockSettings.textValue)}
iconType={iconType}
customIcon={customIcon}
color={textColor}
type={type}
/>
Expand Down
12 changes: 6 additions & 6 deletions packages/callout-block/src/components/CalloutIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { joinClassNames } from '@frontify/guideline-blocks-settings';
import type { ReactNode } from 'react';

import { Icon, Type } from '../types';
import { AppBridgeBlock } from '@frontify/app-bridge';
import { Asset } from '@frontify/app-bridge';
import { CustomCalloutIcon } from './CustomCalloutIcon';
import { IconInfo, IconLightbulb, IconMegaphone } from './icons/';

type CalloutIconProps = {
iconType: Icon;
isActive: boolean;
appBridge: AppBridgeBlock;
customIcon: Asset | undefined;
color?: string;
type: Type;
};
export const CalloutIcon = ({ iconType, isActive, appBridge, color, type }: CalloutIconProps) => {
const icon = calloutIconMap(appBridge, type)[iconType];
export const CalloutIcon = ({ iconType, isActive, customIcon, color, type }: CalloutIconProps) => {
const icon = calloutIconMap(customIcon, type)[iconType];

return (
<div
Expand All @@ -34,10 +34,10 @@ export const CalloutIcon = ({ iconType, isActive, appBridge, color, type }: Call
);
};

const calloutIconMap = (appBridge: AppBridgeBlock, type: Type): Record<Icon, ReactNode> => ({
const calloutIconMap = (customIcon: Asset | undefined, type: Type): Record<Icon, ReactNode> => ({
none: null,
info: <IconInfo title={type} />,
lightbulb: <IconLightbulb title={type} />,
megaphone: <IconMegaphone title={type} />,
custom: <CustomCalloutIcon appBridge={appBridge} type={type} />,
custom: <CustomCalloutIcon customIcon={customIcon} type={type} />,
});
12 changes: 3 additions & 9 deletions packages/callout-block/src/components/CustomCalloutIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
/* (c) Copyright Frontify Ltd., all rights reserved. */

import { AppBridgeBlock, useBlockAssets } from '@frontify/app-bridge';

import { ICON_ASSET_ID } from '../settings';
import { Asset } from '@frontify/app-bridge';
import { Type } from '../types';

export const CustomCalloutIcon = ({ appBridge, type }: { appBridge: AppBridgeBlock; type: Type }) => {
const { blockAssets } = useBlockAssets(appBridge);

const icon = blockAssets?.[ICON_ASSET_ID]?.[0];

export const CustomCalloutIcon = ({ customIcon, type }: { customIcon: Asset | undefined; type: Type }) => {
const devicePixelRatio = Math.max(window.devicePixelRatio, 1);
const iconUrl = icon?.genericUrl.replace('{width}', (20 * devicePixelRatio).toString());
const iconUrl = customIcon?.genericUrl.replace('{width}', (20 * devicePixelRatio).toString());

return iconUrl ? (
<img data-test-id="callout-icon-custom" src={iconUrl} alt={type} className="tw-w-5 tw-h-5 tw-object-contain" />
Expand Down

0 comments on commit 53f5630

Please sign in to comment.