refactor: Convert Frame component to functional component

This commit is contained in:
codegen-sh[bot] committed 2025-04-12 17:49:31 +00:00
1 parent 45f0885533
commit 3554e713d1
1 file changed
+74 -77
+74 -77
View File
@@ -1,8 +1,7 @@
import { observable } from "mobx";
import { observer } from "mobx-react";
import { OpenIcon } from "outline-icons";
import * as React from "react";
import { useState, useEffect, useRef } from "react";
import styled from "styled-components";
import { OpenIcon } from "outline-icons";
import { Optional } from "utility-types";
import { s } from "../../styles";
import { sanitizeUrl } from "../../utils/urls";
@@ -30,84 +29,82 @@ type PropsWithRef = Props & {
forwardedRef: React.Ref<HTMLIFrameElement>;
};
@observer
class Frame extends React.Component<PropsWithRef> {
mounted: boolean;
function Frame({
border,
style = {},
forwardedRef,
icon,
title,
canonicalUrl,
isSelected,
referrerPolicy,
className = "",
src,
...rest
}: PropsWithRef) {
const [isLoaded, setIsLoaded] = useState(false);
const mountedRef = useRef(true);
@observable
isLoaded = false;
useEffect(() => {
// Set mounted flag
mountedRef.current = true;
// Load iframe after a small delay
const timer = setTimeout(() => {
if (mountedRef.current) {
setIsLoaded(true);
}
}, 0);
componentDidMount() {
this.mounted = true;
setTimeout(this.loadIframe, 0);
}
// Cleanup function
return () => {
mountedRef.current = false;
clearTimeout(timer);
};
}, []);
componentWillUnmount() {
this.mounted = false;
}
const showBottomBar = !!(icon || canonicalUrl);
loadIframe = () => {
if (!this.mounted) {
return;
}
this.isLoaded = true;
};
render() {
const {
border,
style = {},
forwardedRef,
icon,
title,
canonicalUrl,
isSelected,
referrerPolicy,
className = "",
src,
} = this.props;
const showBottomBar = !!(icon || canonicalUrl);
return (
<Rounded
style={style}
$showBottomBar={showBottomBar}
$border={border}
className={
isSelected ? `ProseMirror-selectednode ${className}` : className
}
>
{this.isLoaded && (
<Iframe
ref={forwardedRef}
$showBottomBar={showBottomBar}
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-downloads allow-storage-access-by-user-activation"
style={style}
frameBorder="0"
title="embed"
loading="lazy"
src={sanitizeUrl(src)}
referrerPolicy={referrerPolicy}
allowFullScreen
/>
)}
{showBottomBar && (
<Bar>
{icon} <Title>{title}</Title>
{canonicalUrl && (
<Open
href={canonicalUrl}
target="_blank"
rel="noopener noreferrer"
>
<OpenIcon size={18} /> Open
</Open>
)}
</Bar>
)}
</Rounded>
);
}
return (
<Rounded
style={style}
$showBottomBar={showBottomBar}
$border={border}
className={
isSelected ? `ProseMirror-selectednode ${className}` : className
}
>
{isLoaded && (
<Iframe
ref={forwardedRef}
$showBottomBar={showBottomBar}
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-downloads allow-storage-access-by-user-activation"
style={style}
frameBorder="0"
title="embed"
loading="lazy"
src={sanitizeUrl(src)}
referrerPolicy={referrerPolicy}
allowFullScreen
{...rest}
/>
)}
{showBottomBar && (
<Bar>
{icon} <Title>{title}</Title>
{canonicalUrl && (
<Open
href={canonicalUrl}
target="_blank"
rel="noopener noreferrer"
>
<OpenIcon size={18} /> Open
</Open>
)}
</Bar>
)}
</Rounded>
);
}
const Iframe = styled.iframe<{ $showBottomBar: boolean }>`