diff --git a/src/modules/blocks/AutoBlocksRenderer.tsx b/src/modules/blocks/AutoBlocksRenderer.tsx index 528f4b4d7..e32384c21 100644 --- a/src/modules/blocks/AutoBlocksRenderer.tsx +++ b/src/modules/blocks/AutoBlocksRenderer.tsx @@ -122,6 +122,7 @@ export function AutoBlocksRenderer(props: { code={bkInput.code} title={bkInput.title} isPartial={bkInput.isPartial} contentScaling={props.contentScaling} fitScreen={props.fitScreen} + isMobile={props.isMobile} initialShowHTML={props.showUnsafeHtml} noCopyButton={props.blocksProcessor === 'diagram'} optimizeLightweight={optimizeMemoBeforeLastBlock} diff --git a/src/modules/blocks/enhanced-code/EnhancedRenderCode.tsx b/src/modules/blocks/enhanced-code/EnhancedRenderCode.tsx index 7b2328c7a..783d495a6 100644 --- a/src/modules/blocks/enhanced-code/EnhancedRenderCode.tsx +++ b/src/modules/blocks/enhanced-code/EnhancedRenderCode.tsx @@ -6,16 +6,14 @@ import CodeIcon from '@mui/icons-material/Code'; import MoreVertIcon from '@mui/icons-material/MoreVert'; import type { ContentScaling } from '~/common/app.theme'; -import type { LiveFileId } from '~/common/livefile/liveFile.types'; import { ExpanderControlledBox } from '~/common/components/ExpanderControlledBox'; import { TooltipOutlined } from '~/common/components/TooltipOutlined'; -import { useContextWorkspaceId } from '~/common/stores/workspace/WorkspaceIdProvider'; -import { useLiveFileSync } from '../../../apps/chat/components/message/fragments-attachment-doc/livefile-sync/useLiveFileSync'; import { EnhancedRenderCodeMenu } from './EnhancedRenderCodeMenu'; import { RenderCodeMemo } from '../code/RenderCode'; import { enhancedCodePanelTitleTooltipSx, RenderCodePanelFrame } from '../code/RenderCodePanelFrame'; import { getCodeCollapseManager } from './codeCollapseManager'; +import { useLiveFilePatch } from './livefile-patch/useLiveFilePatch'; export function EnhancedRenderCode(props: { @@ -25,7 +23,8 @@ export function EnhancedRenderCode(props: { code: string, isPartial: boolean, - fitScreen?: boolean, + fitScreen: boolean, + isMobile: boolean, initialShowHTML?: boolean, noCopyButton?: boolean, optimizeLightweight?: boolean, @@ -43,42 +42,14 @@ export function EnhancedRenderCode(props: { const [contextMenuAnchor, setContextMenuAnchor] = React.useState(null); const [isCodeCollapsed, setIsCodeCollapsed] = React.useState(false); - // external state - const workspaceId = useContextWorkspaceId(); - - - // LiveFile sync - - // const handleReplaceDocFragmentText = React.useCallback((newText: string) => { - // // create a new Doc Attachment Fragment - // const newData = createDMessageDataInlineText(newText, fragmentDocPart.data.mimeType); - // const newAttachment = createDocAttachmentFragment(fragmentTitle, fragment.caption, fragmentDocPart.vdt, newData, fragmentDocPart.ref, fragmentDocPart.meta, fragment.liveFileId); - // - // // reuse the same fragment ID, which makes the screen not flash (otherwise the whole editor would disappear as the ID does not exist anymore) - // newAttachment.fId = fragmentId; - // - // // replace this fragment with the new one - // onFragmentReplace(fragmentId, newAttachment); - // }, [fragment.caption, fragment.liveFileId, fragmentDocPart, fragmentId, fragmentTitle, onFragmentReplace]); - - /* Very Local State - * this will easily get wiped just on a component remount - so it's just a temporary 'solution'. - */ - const [liveFileId, setLiveFileId] = React.useState(null); - - console.warn('TODO: from here!') - - const { liveFileControlButton, liveFileActions } = useLiveFileSync( - liveFileId, - workspaceId, - props.fitScreen === true, // as a proxy for isMobile - props.code, - setLiveFileId, - (text: string) => null, + // LiveFile - patch state + const { button: liveFileButton, actionBar: liveFileActionBar } = useLiveFilePatch( + props.title, props.code, props.isPartial, + props.isMobile, ); - // hooks + // handlers const handleCloseContextMenu = React.useCallback(() => setContextMenuAnchor(null), []); @@ -155,10 +126,8 @@ export function EnhancedRenderCode(props: { - {/* LiveFile */} - - {liveFileControlButton} - + {/* LiveFile - Select */} + {liveFileButton} {/* Menu Options button */} - - {/*/!* Collapsing Button *!/*/} - - - {/**/} - {/* {isCodeCollapsed ? : }*/} - {/**/} - , [handleToggleCodeCollapse, handleToggleContextMenu, headerTooltipContents, isCodeCollapsed, liveFileControlButton, props.title]); + , [handleToggleCodeCollapse, handleToggleContextMenu, headerTooltipContents, isCodeCollapsed, liveFileButton, props.title]); // const toolbarRow = React.useMemo(() => <> // {props.onLiveFileCreate && ( @@ -219,7 +176,7 @@ export function EnhancedRenderCode(props: { noOuterShadow contentScaling={props.contentScaling} headerRow={headerRow} - subHeaderInline={liveFileActions} + subHeaderInline={liveFileActionBar} onHeaderClick={props.fitScreen ? handleToggleCodeCollapse : undefined} onHeaderContext={handleToggleContextMenu} > diff --git a/src/modules/blocks/enhanced-code/livefile-patch/useLiveFilePatch.tsx b/src/modules/blocks/enhanced-code/livefile-patch/useLiveFilePatch.tsx new file mode 100644 index 000000000..b7c92a95d --- /dev/null +++ b/src/modules/blocks/enhanced-code/livefile-patch/useLiveFilePatch.tsx @@ -0,0 +1,50 @@ +import * as React from 'react'; + +import { Typography } from '@mui/joy'; + +import { useContextWorkspaceId } from '~/common/stores/workspace/WorkspaceIdProvider'; + +import type { LiveFileId } from '~/common/livefile/liveFile.types'; +import { SxProps } from '@mui/joy/styles/types'; +import { isLiveFileSupported } from '~/common/livefile/store-live-file'; +import { useUXLabsStore } from '~/common/state/store-ux-labs'; + + +const buttonContainerSx: SxProps = { + ml: 'auto', +}; + + +export function useLiveFilePatch(title: string, code: string, isPartial: boolean, isMobile: boolean) { + + /** + * (very) local state + * This will get wiped just on a component remount - so it's just a temporary 'solution'. + */ + const [liveFileId, setLiveFileId] = React.useState(null); + + // state + const isEnabled = useUXLabsStore((state) => state.labsEnhanceCodeLiveFile && isLiveFileSupported()); + console.log('isEnabled', isEnabled); + + // components + + const button = React.useMemo(() => !isEnabled ? null : ( + + {liveFileId || 'aaa'} + + ), [isEnabled, liveFileId]); + + const actionBar = React.useMemo(() => (!isEnabled || !liveFileId) ? null : ( + + test + + ), [isEnabled, liveFileId]); + + const workspaceId = useContextWorkspaceId(); + + return { + button, + actionBar, + }; +}