2026-01-28 16:52:10 +00:00
|
|
|
import { ThreadWebWorker, release, retain } from '@quilted/threads';
|
|
|
|
|
import { RemoteReceiver } from '@remote-dom/core/receivers';
|
2026-02-09 13:38:35 +00:00
|
|
|
import { useEffect, useRef } from 'react';
|
2026-03-05 10:13:46 +00:00
|
|
|
import { type ActionConfirmationModalResult } from '../../../sdk/front-component-api/globals/frontComponentHostCommunicationApi';
|
2026-02-09 13:38:35 +00:00
|
|
|
import { type FrontComponentHostCommunicationApi } from '../../types/FrontComponentHostCommunicationApi';
|
2026-01-28 16:52:10 +00:00
|
|
|
import { type WorkerExports } from '../../types/WorkerExports';
|
2026-02-13 14:58:19 +00:00
|
|
|
import { createRemoteWorker } from '../worker/utils/createRemoteWorker';
|
2026-01-28 16:52:10 +00:00
|
|
|
|
2026-03-05 10:13:46 +00:00
|
|
|
// Must match ACTION_MENU_CONFIRMATION_MODAL_RESULT_BROWSER_EVENT_NAME in twenty-front
|
|
|
|
|
const ACTION_MENU_CONFIRMATION_MODAL_RESULT_BROWSER_EVENT_NAME =
|
|
|
|
|
'action-menu-confirmation-modal-result';
|
|
|
|
|
|
|
|
|
|
type ActionMenuConfirmationModalResultBrowserEventDetail = {
|
|
|
|
|
frontComponentId: string;
|
|
|
|
|
confirmationResult: ActionConfirmationModalResult;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-28 16:52:10 +00:00
|
|
|
type FrontComponentWorkerEffectProps = {
|
|
|
|
|
componentUrl: string;
|
2026-02-13 14:18:09 +00:00
|
|
|
applicationAccessToken?: string;
|
|
|
|
|
apiUrl?: string;
|
2026-03-05 10:13:46 +00:00
|
|
|
frontComponentId: string;
|
2026-02-09 13:38:35 +00:00
|
|
|
frontComponentHostCommunicationApi: FrontComponentHostCommunicationApi;
|
2026-01-28 16:52:10 +00:00
|
|
|
setReceiver: React.Dispatch<React.SetStateAction<RemoteReceiver | null>>;
|
2026-02-05 10:44:02 +00:00
|
|
|
setThread: React.Dispatch<
|
2026-02-09 13:38:35 +00:00
|
|
|
React.SetStateAction<ThreadWebWorker<
|
|
|
|
|
WorkerExports,
|
|
|
|
|
FrontComponentHostCommunicationApi
|
|
|
|
|
> | null>
|
2026-02-05 10:44:02 +00:00
|
|
|
>;
|
|
|
|
|
setError: React.Dispatch<React.SetStateAction<Error | null>>;
|
2026-01-28 16:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const FrontComponentWorkerEffect = ({
|
|
|
|
|
componentUrl,
|
2026-02-13 14:18:09 +00:00
|
|
|
applicationAccessToken,
|
|
|
|
|
apiUrl,
|
2026-03-05 10:13:46 +00:00
|
|
|
frontComponentId,
|
2026-02-09 13:38:35 +00:00
|
|
|
frontComponentHostCommunicationApi,
|
2026-01-28 16:52:10 +00:00
|
|
|
setReceiver,
|
2026-02-05 10:44:02 +00:00
|
|
|
setThread,
|
|
|
|
|
setError,
|
2026-01-28 16:52:10 +00:00
|
|
|
}: FrontComponentWorkerEffectProps) => {
|
2026-02-20 14:14:42 +00:00
|
|
|
const isInitializedRef = useRef(false);
|
2026-02-09 13:38:35 +00:00
|
|
|
|
2026-01-28 16:52:10 +00:00
|
|
|
useEffect(() => {
|
2026-02-20 14:14:42 +00:00
|
|
|
if (isInitializedRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 16:52:10 +00:00
|
|
|
const newReceiver = new RemoteReceiver({ retain, release });
|
|
|
|
|
|
|
|
|
|
const worker = createRemoteWorker();
|
|
|
|
|
|
|
|
|
|
worker.onerror = (event: ErrorEvent) => {
|
2026-02-13 14:58:19 +00:00
|
|
|
const workerError =
|
2026-02-20 14:14:42 +00:00
|
|
|
event.error ?? new Error(event.message || 'Unknown worker error');
|
2026-02-13 14:58:19 +00:00
|
|
|
|
|
|
|
|
console.error('[FrontComponentRenderer] Worker error:', workerError);
|
|
|
|
|
setError(workerError);
|
2026-01-28 16:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
2026-02-09 13:38:35 +00:00
|
|
|
const thread = new ThreadWebWorker<
|
|
|
|
|
WorkerExports,
|
|
|
|
|
FrontComponentHostCommunicationApi
|
|
|
|
|
>(worker, {
|
2026-02-20 14:14:42 +00:00
|
|
|
exports: frontComponentHostCommunicationApi,
|
2026-02-09 13:38:35 +00:00
|
|
|
});
|
2026-02-20 14:14:42 +00:00
|
|
|
|
2026-03-05 10:13:46 +00:00
|
|
|
const handleActionMenuConfirmationModalResultBrowserEvent = (
|
|
|
|
|
event: CustomEvent<ActionMenuConfirmationModalResultBrowserEventDetail>,
|
|
|
|
|
) => {
|
|
|
|
|
const actionMenuConfirmationModalResultBrowserEventDetail = event.detail;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
actionMenuConfirmationModalResultBrowserEventDetail.frontComponentId !==
|
|
|
|
|
frontComponentId
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thread.imports
|
|
|
|
|
.onConfirmationModalResult(
|
|
|
|
|
actionMenuConfirmationModalResultBrowserEventDetail.confirmationResult,
|
|
|
|
|
)
|
|
|
|
|
.catch((error: Error) => {
|
|
|
|
|
setError(error);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
|
ACTION_MENU_CONFIRMATION_MODAL_RESULT_BROWSER_EVENT_NAME,
|
|
|
|
|
handleActionMenuConfirmationModalResultBrowserEvent as EventListener,
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-05 10:44:02 +00:00
|
|
|
setThread(thread);
|
2026-01-28 16:52:10 +00:00
|
|
|
|
|
|
|
|
thread.imports
|
2026-02-13 14:18:09 +00:00
|
|
|
.render(newReceiver.connection, {
|
|
|
|
|
componentUrl,
|
|
|
|
|
applicationAccessToken,
|
|
|
|
|
apiUrl,
|
|
|
|
|
})
|
2026-01-28 16:52:10 +00:00
|
|
|
.catch((error: Error) => {
|
2026-02-05 10:44:02 +00:00
|
|
|
setError(error);
|
2026-01-28 16:52:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setReceiver(newReceiver);
|
2026-02-20 14:14:42 +00:00
|
|
|
isInitializedRef.current = true;
|
2026-01-28 16:52:10 +00:00
|
|
|
|
|
|
|
|
return () => {
|
2026-03-05 10:13:46 +00:00
|
|
|
window.removeEventListener(
|
|
|
|
|
ACTION_MENU_CONFIRMATION_MODAL_RESULT_BROWSER_EVENT_NAME,
|
|
|
|
|
handleActionMenuConfirmationModalResultBrowserEvent as EventListener,
|
|
|
|
|
);
|
2026-02-05 10:44:02 +00:00
|
|
|
setThread(null);
|
2026-01-28 16:52:10 +00:00
|
|
|
worker.terminate();
|
2026-02-20 15:03:10 +00:00
|
|
|
isInitializedRef.current = false;
|
2026-01-28 16:52:10 +00:00
|
|
|
};
|
2026-02-13 14:18:09 +00:00
|
|
|
}, [
|
|
|
|
|
componentUrl,
|
|
|
|
|
applicationAccessToken,
|
|
|
|
|
apiUrl,
|
2026-03-05 10:13:46 +00:00
|
|
|
frontComponentId,
|
2026-02-13 14:18:09 +00:00
|
|
|
setError,
|
|
|
|
|
setReceiver,
|
|
|
|
|
setThread,
|
2026-02-20 14:14:42 +00:00
|
|
|
frontComponentHostCommunicationApi,
|
2026-02-13 14:18:09 +00:00
|
|
|
]);
|
2026-01-28 16:52:10 +00:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|