import { useRegenerateProjectScreenshot } from '@intlayer/design-system/api';
import { Button } from '@intlayer/design-system/button';
import { Container } from '@intlayer/design-system/container';
import { ImageOff, RefreshCw } from 'lucide-react';
import { type FC, type ReactNode, useState } from 'react';
import { useIntlayer } from 'react-intlayer';
export interface ProjectScreenshotProps {
/** Public URL of the stored screenshot. */
imageUrl: string;
/** Project name, used for the alt text. */
projectName: string;
/** Whether the viewer may regenerate the screenshot. */
canRegenerate: boolean;
/** Site URL the screenshot was captured from; makes the image a link to it. */
applicationURL?: string;
}
/**
* Opens the project site in a new tab when a URL is known, otherwise renders
* the children as is.
*/
export const ScreenshotLink: FC<{ href?: string; children: ReactNode }> = ({
href,
children,
}) =>
href ? (
{children}
) : (
children
);
/**
* Project screenshot. When the image fails to load (e.g. a 404 from storage),
* shows a placeholder with a button to capture the application URL again.
*/
export const ProjectScreenshot: FC = ({
imageUrl,
projectName,
canRegenerate,
applicationURL,
}) => {
const [failedImageUrl, setFailedImageUrl] = useState(null);
const { mutate: regenerateScreenshot, isPending: isRegenerating } =
useRegenerateProjectScreenshot();
const { screenshotUnavailable, regenerateScreenshotButton } =
useIntlayer('project-form');
// A new URL (after regeneration) gets a fresh load attempt
const hasLoadFailed = failedImageUrl === imageUrl;
return (
{hasLoadFailed ? (