Skip to content

Commit

Permalink
fix font installation
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Mar 15, 2024
1 parent 7271ec3 commit 06c19fa
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 42 deletions.
Expand Up @@ -9,7 +9,7 @@ import {
useContext,
useState,
} from '@wordpress/element';
import { dispatch, useSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { Link } from '@woocommerce/components';
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
import { recordEvent } from '@woocommerce/tracks';
Expand All @@ -24,6 +24,7 @@ import { ADMIN_URL } from '~/utils/admin-settings';
import { FontPairing } from './global-styles';
import { CustomizeStoreContext } from '..';
import { FlowType } from '~/customize-store/types';
import { isIframe, sendMessageToParent } from '~/customize-store/utils';
export const SidebarNavigationScreenTypography = () => {
const { context, sendEvent } = useContext( CustomizeStoreContext );
const aiOnline = context.flowType === FlowType.AIOnline;
Expand Down Expand Up @@ -202,9 +203,15 @@ export const SidebarNavigationScreenTypography = () => {
<Button
onClick={ async () => {
optIn();
window.parent.__wcCustomizeStore.sendEventToIntroMachine(
{ type: 'INSTALL_FONTS' }
);
if ( isIframe( window ) ) {
sendMessageToParent( {
type: 'INSTALL_FONTS',
} );
} else {
sendEvent(
'INSTALL_FONTS'
);
}
} }
variant="primary"
disabled={ ! OptInDataSharing }
Expand Down
Expand Up @@ -15,7 +15,7 @@ import {
} from '../types';
import { designWithNoAiStateMachineDefinition } from './state-machine';
import { findComponentMeta } from '~/utils/xstate/find-component';
import { AssembleHubLoader } from '../design-with-ai/pages';
import { AssembleHubLoader } from './pages/ApiCallLoader';

export type DesignWithoutAiComponent = typeof AssembleHubLoader;
export type DesignWithoutAiComponentMeta = {
Expand All @@ -30,15 +30,18 @@ export const DesignWithNoAiController = ( {
sendEventToParent?: Sender< customizeStoreStateMachineEvents >;
parentContext?: customizeStoreStateMachineContext;
} ) => {
const [ , , service ] = useMachine( designWithNoAiStateMachineDefinition, {
devTools: process.env.NODE_ENV === 'development',
parent: parentMachine,
context: {
...designWithNoAiStateMachineDefinition.context,
isFontLibraryAvailable:
parentContext?.isFontLibraryAvailable ?? false,
},
} );
const [ , send, service ] = useMachine(
designWithNoAiStateMachineDefinition,
{
devTools: process.env.NODE_ENV === 'development',
parent: parentMachine,
context: {
...designWithNoAiStateMachineDefinition.context,
isFontLibraryAvailable:
parentContext?.isFontLibraryAvailable ?? false,
},
}
);

// eslint-disable-next-line react-hooks/exhaustive-deps -- false positive due to function name match, this isn't from react std lib
const currentNodeMeta = useSelector( service, ( currentState ) =>
Expand All @@ -59,7 +62,11 @@ export const DesignWithNoAiController = ( {
return (
<>
<div className={ `woocommerce-design-without-ai__container` }>
{ CurrentComponent ? <CurrentComponent /> : <div /> }
{ CurrentComponent ? (
<CurrentComponent sendEvent={ send } />
) : (
<div />
) }
</div>
</>
);
Expand Down
Expand Up @@ -17,6 +17,7 @@ import {
createAugmentedSteps,
onIframeLoad,
} from '~/customize-store/utils';
import { DesignWithoutAIStateMachineEvents } from '../state-machine';

const loaderSteps = [
{
Expand Down Expand Up @@ -103,11 +104,21 @@ export const ApiCallLoader = () => {
);
};

const AssemblerHub = () => {
type SendEventFn = ( event: DesignWithoutAIStateMachineEvents ) => void;

const AssemblerHub = ( { sendEvent }: { sendEvent: SendEventFn } ) => {
const assemblerUrl = getNewPath( {}, '/customize-store/assembler-hub', {} );
const iframe = useRef< HTMLIFrameElement >( null );
const [ isVisible, setIsVisible ] = useState( false );

useEffect( () => {
window.addEventListener( 'message', ( event ) => {
if ( event.data?.type === 'INSTALL_FONTS' ) {
sendEvent( { type: 'INSTALL_FONTS' } );
}
} );
}, [ sendEvent ] );

return (
<iframe
ref={ iframe }
Expand All @@ -127,7 +138,11 @@ const AssemblerHub = () => {
);
};

export const AssembleHubLoader = () => {
export const AssembleHubLoader = ( {
sendEvent,
}: {
sendEvent: SendEventFn;
} ) => {
// Show the last two steps of the loader so that the last frame is the shortest time possible
const augmentedSteps = createAugmentedSteps( loaderSteps.slice( -2 ), 10 );

Expand Down Expand Up @@ -160,7 +175,7 @@ export const AssembleHubLoader = () => {
progress={ progress || 0 }
/>
</Loader>
<AssemblerHub />
<AssemblerHub sendEvent={ sendEvent } />
</>
);
};
Expand Up @@ -29,15 +29,6 @@ export const hasStepInUrl = (
);
};

export const hasInstallFontsInUrl = () => {
const { path = '' } = getQuery() as { path: string };
const pathFragments = path.split( '/' );
return (
pathFragments[ 2 ] === 'design' &&
pathFragments[ 3 ] === 'install-fonts'
);
};

const installFontFamiliesState = {
initial: 'checkFontLibrary',
states: {
Expand Down Expand Up @@ -106,12 +97,6 @@ export const designWithNoAiStateMachineDefinition = createMachine(
states: {
navigate: {
always: [
{
cond: {
type: 'hasInstallFontsInUrl',
},
target: 'installFontFamilies',
},
{
cond: {
type: 'hasStepInUrl',
Expand Down Expand Up @@ -139,10 +124,9 @@ export const designWithNoAiStateMachineDefinition = createMachine(
installFontFamiliesState.states.checkFontLibrary,
pending: installFontFamiliesState.states.pending,
success: {
meta: {
component: AssembleHubLoader,
onDone: {
target: '#designWithoutAI.showAssembleHub',
},
entry: [ 'redirectToAssemblerHub' ],
type: 'final',
},
},
Expand Down Expand Up @@ -243,7 +227,6 @@ export const designWithNoAiStateMachineDefinition = createMachine(
component: AssembleHubLoader,
},
entry: [ 'redirectToAssemblerHub' ],
type: 'final',
},
},
},
Expand All @@ -252,7 +235,6 @@ export const designWithNoAiStateMachineDefinition = createMachine(
services,
guards: {
hasStepInUrl,
hasInstallFontsInUrl,
isFontLibraryAvailable,
},
}
Expand Down
3 changes: 0 additions & 3 deletions plugins/woocommerce-admin/client/customize-store/index.tsx
Expand Up @@ -205,9 +205,6 @@ export const customizeStoreStateMachineDefinition = createMachine( {
},
INSTALL_FONTS: {
target: 'designWithoutAi',
actions: [
{ type: 'updateQueryStep', step: 'design/install-fonts' },
],
},
},
states: {
Expand Down
Expand Up @@ -22,7 +22,6 @@ import { Product, ProductType } from '../product-list/types';
import { appendURLParams } from '../../utils/functions';
import { ADMIN_URL, getAdminSetting } from '~/utils/admin-settings';
import { NoAIBanner } from '~/customize-store/intro/intro-banners';
import { addQueryArgs } from '@wordpress/url';

export default function ProductListContent( props: {
products: Product[];
Expand Down

0 comments on commit 06c19fa

Please sign in to comment.