2024-07-17 18:54:46 +00:00
|
|
|
import { useStore } from '@nanostores/react';
|
2024-08-05 14:49:02 +00:00
|
|
|
import { memo, useCallback, useEffect, useRef, useState } from 'react';
|
2024-07-24 15:43:32 +00:00
|
|
|
import { IconButton } from '~/components/ui/IconButton';
|
|
|
|
import { workbenchStore } from '~/lib/stores/workbench';
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
export const Preview = memo(() => {
|
|
|
|
const iframeRef = useRef<HTMLIFrameElement>(null);
|
2024-08-05 14:49:02 +00:00
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
2024-07-17 18:54:46 +00:00
|
|
|
const [activePreviewIndex] = useState(0);
|
|
|
|
const previews = useStore(workbenchStore.previews);
|
|
|
|
const activePreview = previews[activePreviewIndex];
|
|
|
|
|
|
|
|
const [url, setUrl] = useState('');
|
|
|
|
const [iframeUrl, setIframeUrl] = useState<string | undefined>();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-07-22 15:40:28 +00:00
|
|
|
if (!activePreview) {
|
|
|
|
setUrl('');
|
|
|
|
setIframeUrl(undefined);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iframeUrl) {
|
2024-07-17 18:54:46 +00:00
|
|
|
const { baseUrl } = activePreview;
|
|
|
|
|
|
|
|
setUrl(baseUrl);
|
|
|
|
setIframeUrl(baseUrl);
|
|
|
|
}
|
|
|
|
}, [activePreview, iframeUrl]);
|
|
|
|
|
2024-08-05 14:49:02 +00:00
|
|
|
const validateUrl = useCallback(
|
|
|
|
(value: string) => {
|
|
|
|
if (!activePreview) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { baseUrl } = activePreview;
|
|
|
|
|
|
|
|
if (value === baseUrl) {
|
|
|
|
return true;
|
|
|
|
} else if (value.startsWith(baseUrl)) {
|
|
|
|
return ['/', '?', '#'].includes(value.charAt(baseUrl.length));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
[activePreview],
|
|
|
|
);
|
|
|
|
|
2024-07-17 18:54:46 +00:00
|
|
|
const reloadPreview = () => {
|
|
|
|
if (iframeRef.current) {
|
|
|
|
iframeRef.current.src = iframeRef.current.src;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="w-full h-full flex flex-col">
|
2024-07-18 09:10:12 +00:00
|
|
|
<div className="bg-white p-2 flex items-center gap-1.5">
|
2024-07-17 18:54:46 +00:00
|
|
|
<IconButton icon="i-ph:arrow-clockwise" onClick={reloadPreview} />
|
|
|
|
<div className="flex items-center gap-1 flex-grow bg-gray-100 rounded-full px-3 py-1 text-sm text-gray-600 hover:bg-gray-200 hover:focus-within:bg-white focus-within:bg-white focus-within:ring-2 focus-within:ring-accent">
|
|
|
|
<div className="bg-white rounded-full p-[2px] -ml-1">
|
2024-07-22 15:40:28 +00:00
|
|
|
<div className="i-ph:info-bold text-lg" />
|
2024-07-17 18:54:46 +00:00
|
|
|
</div>
|
|
|
|
<input
|
2024-08-05 14:49:02 +00:00
|
|
|
ref={inputRef}
|
2024-07-17 18:54:46 +00:00
|
|
|
className="w-full bg-transparent outline-none"
|
|
|
|
type="text"
|
|
|
|
value={url}
|
|
|
|
onChange={(event) => {
|
|
|
|
setUrl(event.target.value);
|
|
|
|
}}
|
2024-08-05 14:49:02 +00:00
|
|
|
onKeyDown={(event) => {
|
|
|
|
if (event.key === 'Enter' && validateUrl(url)) {
|
|
|
|
setIframeUrl(url);
|
|
|
|
|
|
|
|
if (inputRef.current) {
|
|
|
|
inputRef.current.blur();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
2024-07-17 18:54:46 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="flex-1 bg-white border-t">
|
|
|
|
{activePreview ? (
|
2024-07-22 15:40:28 +00:00
|
|
|
<iframe ref={iframeRef} className="border-none w-full h-full" src={iframeUrl} />
|
2024-07-17 18:54:46 +00:00
|
|
|
) : (
|
|
|
|
<div className="flex w-full h-full justify-center items-center">No preview available</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|