import type { CSSProperties, KeyboardEvent, MutableRefObject, PointerEvent } from "react"; import { useEffect, useRef } from "react"; import { ActionIcon, Badge, Box, Card, FocusTrap } from "@mantine/core"; import { useElementSize, useMergedRef, useViewportSize } from "@mantine/hooks"; import { IconMaximize, IconPin, IconX } from "@tabler/icons-react"; import { QueryErrorResetBoundary, useQueryClient } from "@tanstack/react-query"; import combineClasses from "clsx"; import { NoIntegrationSelectedError } from "@homarr/widgets/errors/classes"; import { ErrorBoundary } from "react-error-boundary"; import { useRequiredBoard } from "@homarr/boards/context"; import { useEditMode } from "@homarr/boards/edit-mode"; import { useSettings } from "@homarr/settings"; import { useI18n } from "@homarr/translation/client"; import { loadWidgetDynamic, reduceWidgetOptionsWithDefaultValues, widgetImports } from "@homarr/widgets"; import { WidgetError } from "@homarr/widgets/errors"; import type { SectionItem } from "~/app/[locale]/boards/_types"; import advancedFocusClasses from "../advanced-focus/advanced-focus.module.css"; import { useAdvancedFocus } from "../advanced-focus/context"; import { getAdvancedFocusRect } from "../advanced-focus/geometry"; import classes from "../sections/item.module.css"; import { useItemActions } from "./item-actions"; import itemContentClasses from "./item-content.module.css"; import { BoardItemMenu } from "./item-menu"; import { WidgetContextMenu } from "./widget-context-menu"; import { removePersistedWidgetQueries } from "./widget-query-recovery"; interface BoardItemContentProps { item: SectionItem; } const getOverflowFromKind = (kind: SectionItem["kind"]) => { if (kind === "iframe") return "hidden"; if (kind === "systemResources") return "visible"; return undefined; }; export const BoardItemContent = ({ item }: BoardItemContentProps) => { const { ref, width, height } = useElementSize(); const sourceRef = useRef(null); const mergedRef = useMergedRef(ref, sourceRef); const { width: viewportWidth, height: viewportHeight } = useViewportSize(); const board = useRequiredBoard(); const t = useI18n(); const [isEditMode] = useEditMode(); const widgetStateRef = useRef | null>(null); const { active, open, close, dismiss, pin, hover, leave } = useAdvancedFocus(); const activeFocus = active?.itemId === item.id ? active : null; const isAdvanced = activeFocus !== null; const advancedRect = activeFocus ? getAdvancedFocusRect(activeFocus.sourceRect, { width: viewportWidth, height: viewportHeight }) : null; // Responsive layout changes remount grid items. Never leave their focus backdrop orphaned. useEffect(() => () => dismiss(item.id), [dismiss, item.id]); const openPinned = () => { if (sourceRef.current) open(item.id, sourceRef.current, true); }; const handleKeyDown = (event: KeyboardEvent) => { if (event.currentTarget !== event.target || event.key !== "Enter" || !event.shiftKey || isEditMode) return; event.preventDefault(); openPinned(); }; const handleSurfacePointerDown = (event: PointerEvent) => { if (!activeFocus || activeFocus.pinned) return; const target = event.target instanceof Element ? event.target.closest( "a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), iframe, [tabindex]:not([tabindex='-1']), [contenteditable='true']", ) : null; const alreadyAutofocused = target?.hasAttribute("data-autofocus") ?? false; if (target && !alreadyAutofocused) target.setAttribute("data-autofocus", "true"); pin(false); if (target && !alreadyAutofocused) { window.setTimeout(() => target.removeAttribute("data-autofocus"), 250); } }; return ( <> sourceRef.current && hover(item.id, sourceRef.current)} onPointerLeave={() => leave(item.id)} className={combineClasses("grid-stack-item-content", isAdvanced && advancedFocusClasses.sourcePlaceholder)} >
{isAdvanced && ( close()} > )} {isAdvanced && ( pin(event.detail === 0)} > )} {!isEditMode && !isAdvanced && ( )}
{!isAdvanced && item.advancedOptions.title?.trim() && ( {item.advancedOptions.title} )} ); }; interface InnerContentProps { item: SectionItem; width: number; height: number; widgetStateRef: MutableRefObject | null>; displayMode: "compact" | "advanced"; } const InnerContent = ({ item, ...dimensions }: InnerContentProps) => { const settings = useSettings(); const board = useRequiredBoard(); const [isEditMode] = useEditMode(); const Comp = loadWidgetDynamic(item.kind); const { definition } = widgetImports[item.kind]; const options = reduceWidgetOptionsWithDefaultValues(item.kind, settings, item.options); const newItem = { ...item, options }; const { updateItemOptions } = useItemActions(); const updateOptions = ({ newOptions }: { newOptions: Record }) => updateItemOptions({ itemId: item.id, newOptions }); const widgetSupportsIntegrations = "supportedIntegrations" in definition && definition.supportedIntegrations.length >= 1; const queryClient = useQueryClient(); return ( {({ reset }) => ( { removePersistedWidgetQueries(queryClient); reset(); }} fallbackRender={({ resetErrorBoundary, error }) => ( <> )} > updateOptions({ newOptions: { ...options, ...partialNewOptions.newOptions, }, }) } {...dimensions} /> )} ); }; const Throw = ({ when, error }: { when: boolean; error: Error }) => { if (when) throw error; return null; };