Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import type { GalleryItem } from '@features/gallery';
import type { WidgetLabelProps, WorkbenchRegion } from '@workbench/widgetContracts';
import { Box, HStack, Text } from '@chakra-ui/react';
import { getGalleryItemView } from '@features/gallery/contracts';
import {
GalleryPickerPopover,
type GalleryPickerAccept,
type GalleryPickerOpeningScope,
} from '@features/gallery/picker';
import { Button, IconButton } from '@platform/ui/Button';
import { MiddleTruncate } from '@platform/ui/MiddleTruncate';
import { ClippedTooltip, useTooltipTriggerIds } from '@platform/ui/Tooltip';
import { useWorkbenchFocus } from '@workbench/focusRegions';
import { isWidgetRegion } from '@workbench/layoutContracts';
import { useWorkbenchCommands } from '@workbench/WorkbenchContext';
import { ChevronDownIcon, XIcon } from 'lucide-react';
import { useCallback, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { usePreviewHeaderContext } from './previewHeaderStore';
/** Board and item context from previewHeaderStore, except where the center trigger already names it. */
export const PreviewWidgetLabel = ({ region }: WidgetLabelProps) => {
const { t } = useTranslation();
const { actionItem, boardName, itemName } = usePreviewHeaderContext();
if (!actionItem || !itemName || !boardName) {
if (region === 'center') {
return null;
}
return (
<Text fontSize="md" fontWeight="700">
{t('widgets.labels.preview')}
</Text>
);
}
// Pad the truncated label away from the first header action in a panel header.
return (
<HStack flex="1" gap="1" minW="0" pe={region === 'center' ? undefined : '2'}>
<PreviewItemPicker boardName={boardName} item={actionItem} itemName={itemName} />
<ClearSelectionButton region={region} />
</HStack>
);
};
const PREVIEW_PICKER_ACCEPT: GalleryPickerAccept = ['image', 'video'];
/** Board truncates, then "Board /" drops whole via a clipped wrapping `row-reverse` row; CSS only. */
const NAME_ROW_CSS = {
alignContent: 'flex-start',
alignItems: 'center',
columnGap: '1',
display: 'flex',
flexDirection: 'row-reverse',
flexWrap: 'wrap',
// One line of the trigger's text; the wrapped board group sits below it, out of view.
height: '1lh',
// Main-end of a reversed row: packs the line against the left edge.
justifyContent: 'flex-end',
minW: '0',
overflow: 'hidden',
} as const;
// The floors decide when the board drops: room for a few head characters plus the name's kept tail, and a few
// characters of board. The lopsided grow weights hand the name all free space until its max-content clamp freezes it; a
// modest ratio would leave it a fraction of a pixel short, and ellipsized, while the board still grows.
const ITEM_NAME_CSS = { flex: '1000000 1 16ch', maxW: 'max-content', minW: '0' } as const;
const BOARD_GROUP_CSS = { columnGap: '1', display: 'flex', flex: '1 1 8ch', maxW: 'max-content', minW: '0' } as const;
/** Opens a mini gallery at the item's board; a pick selects within its own board. */
const PreviewItemPicker = ({
boardName,
item,
itemName,
}: {
boardName: string;
item: GalleryItem;
itemName: string;
}) => {
const { t } = useTranslation();
const { gallery } = useWorkbenchCommands();
const triggerIds = useTooltipTriggerIds();
const rowRef = useRef<HTMLDivElement>(null);
const selectItem = useCallback((picked: GalleryItem) => gallery.selectItemInItsBoard(picked), [gallery]);
// Open on the item's board and the view that lists it.
const itemView = getGalleryItemView(item);
const openingScope = useMemo<GalleryPickerOpeningScope>(
() => (itemView ? { boardId: item.boardId, galleryView: itemView } : { boardId: item.boardId }),
[item.boardId, itemView]
);
// The tooltip names the item only while the row hides some of it.
const isLabelClipped = useCallback(() => !!rowRef.current && isRowClipped(rowRef.current), []);
const label = t('widgets.preview.boardAndItem', { board: boardName, name: itemName });
return (
<GalleryPickerPopover
accept={PREVIEW_PICKER_ACCEPT}
ids={triggerIds}
label={t('widgets.gallery.picker.chooseMedia')}
openingScope={openingScope}
onPick={selectItem}
>
<ClippedTooltip content={label} ids={triggerIds} isClipped={isLabelClipped}>
<Button
aria-label={t('widgets.preview.chooseAnotherItem', { label })}
// The button recipe refuses to shrink; the label gives way instead of pushing over the clear button.
flexShrink={1}
fontSize="md"
gap="1"
minW="0"
overflow="hidden"
px="1.5"
size="sm"
variant="ghost"
>
<Box ref={rowRef} css={NAME_ROW_CSS}>
<MiddleTruncate
css={ITEM_NAME_CSS}
color="fg.muted"
fontWeight="normal"
text={itemName}
title={undefined}
/>
<Box css={BOARD_GROUP_CSS}>
<Text flexShrink={1} fontWeight="700" minW="0" truncate>
{boardName}
</Text>
<Text color="fg.muted" flexShrink={0} fontWeight="normal">
/
</Text>
</Box>
</Box>
<ChevronDownIcon aria-hidden size={12} style={CHEVRON_STYLE} />
</Button>
</ClippedTooltip>
</GalleryPickerPopover>
);
};
/** True when the board group has wrapped out of the row's one visible line, or any of its text is truncated. */
const isRowClipped = (row: HTMLElement): boolean => {
const boardGroup = row.lastElementChild;
if (!boardGroup || boardGroup.getBoundingClientRect().top >= row.getBoundingClientRect().bottom) {
return true;
}
return [...row.querySelectorAll('*')].some((element) => element.scrollWidth > element.clientWidth);
};
const CHEVRON_STYLE = { flexShrink: 0 } as const;
const CLEAR_SELECTION_HOVER = { color: 'fg' } as const;
const ClearSelectionButton = ({ region }: { region: WorkbenchRegion }) => {
const { t } = useTranslation();
const { gallery } = useWorkbenchCommands();
const { focusRegion } = useWorkbenchFocus();
const label = t('widgets.gallery.commands.clearSelection');
// The gallery's clear-selection command.
const clearSelection = useCallback(() => {
if (isWidgetRegion(region)) {
focusRegion(region, 'preview');
}
gallery.clearSelection();
}, [focusRegion, gallery, region]);
return (
<IconButton
_hover={CLEAR_SELECTION_HOVER}
aria-label={label}
color="fg.muted"
flexShrink={0}
size="sm"
tooltip={label}
variant="ghost"
onClick={clearSelection}
>
<XIcon />
</IconButton>
);
};