Skip to content

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.

6.4 KB
Raw Blame
import type { ImageIndexCounts } from '@workbench/image-map/indexProgress';

import { Button, HStack, Progress, Stack, Text } from '@chakra-ui/react';
import { Tooltip } from '@platform/ui';
import { describeIndexProgress } from '@workbench/image-map/indexProgress';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';

/** Update age coarsely for minute-scale stall messages shared by panel and footer. */
const TICK_MS = 5_000;

/** How long the counts have been standing, re-read on a timer. */
const useCountsAge = (updatedAt: number | null): number => {
  const [now, setNow] = useState(() => Date.now());

  useEffect(() => {
    if (updatedAt === null) {
      return;
    }

    const timer = setInterval(() => setNow(Date.now()), TICK_MS);

    return () => clearInterval(timer);
  }, [updatedAt]);

  // Clamped rather than reset on a new report: until the next tick `now` still predates it, and a negative age would
  // read as the future.
  return updatedAt === null ? 0 : Math.max(0, now - updatedAt);
};

interface ImageIndexProgressProps {
  counts: ImageIndexCounts;
  /** When the index last moved, or null if it never has been reported. */
  updatedAt: number | null;
}

/** Server-wide progress for admins receiving status events only; non-admins would see a frozen bar. */
export const ImageIndexProgressPanel = ({
  counts,
  error,
  onRetry,
  updatedAt,
}: ImageIndexProgressProps & { error?: string | null; onRetry: () => void }) => {
  const { t } = useTranslation();
  const progress = describeIndexProgress(counts, t, useCountsAge(updatedAt));

  return (
    <Stack align="center" gap="2" maxW="sm" textAlign="center" w="full">
      <Text fontSize="xl" fontWeight="semibold">
        {t('widgets.imageMap.progress.title')}
      </Text>
      <Text color="fg.muted" fontSize="lg">
        {t('widgets.imageMap.progress.description')}
      </Text>
      <Stack gap="1" mt="2" w="full">
        <Progress.Root max={100} size="lg" value={progress.percent}>
          {/* The name goes on the track: that is the element carrying role="progressbar", and Chakra otherwise names it
              "25%", which tells a screen reader the number but never what it counts. */}
          <Progress.Track aria-label={t('widgets.imageMap.progress.label')} aria-valuenow={progress.percent}>
            <Progress.Range />
          </Progress.Track>
        </Progress.Root>
        <HStack color="fg.muted" fontSize="md" justify="space-between">
          <Text fontVariantNumeric="tabular-nums">{progress.counts}</Text>
          <Text fontVariantNumeric="tabular-nums">{progress.percent}%</Text>
        </HStack>
        {/* States the fact and stops there. Nothing here can tell an indexer waiting out a generation from one that has
            died, and the first is routine, so neither may be claimed. */}
        {progress.stale ? (
          <Text color="fg.subtle" fontSize="md">
            {progress.stale}
          </Text>
        ) : null}
        {progress.skipped ? (
          <Text color="fg.subtle" fontSize="md">
            {progress.skipped}
          </Text>
        ) : null}
      </Stack>
      {error ? (
        // Wrap server URLs/identifiers anywhere to prevent min-content overflow in narrow panels.
        <Text color="fg.error" fontSize="md" maxW="full" minW="0" mt="2" overflowWrap="anywhere" role="alert">
          {error}
        </Text>
      ) : null}
      {/* Refresh is the only control before map readiness and recovers counts missed while offline. */}
      <Button mt={error ? '0' : '2'} onClick={onRetry} variant="outline">
        {error ? t('common.retry') : t('widgets.imageMap.checkAgain')}
      </Button>
    </Stack>
  );
};

/** Overlays rebuild progress over the usable stale map and explains missing labels during rebuilds. */
export const ImageIndexActivityBadge = ({ counts, updatedAt }: ImageIndexProgressProps) => {
  const { t } = useTranslation();
  const progress = describeIndexProgress(counts, t, useCountsAge(updatedAt));
  const label = progress.stale
    ? t('widgets.imageMap.progress.badgeStale', { counts: progress.counts, stale: progress.stale })
    : t('widgets.imageMap.progress.badge', { counts: progress.counts });

  return (
    // Positioned by the map's overlay column, which passes pointer events through to the plot; the badge takes its own
    // back so the tooltip opens.
    <Tooltip content={label}>
      <HStack
        bg="bg.subtle"
        borderColor="border.subtle"
        borderRadius="md"
        borderWidth="1px"
        color="fg.muted"
        fontSize="xs"
        gap="1.5"
        maxW="full"
        minW="0"
        pointerEvents="auto"
        px="2"
        py="1"
      >
        <Progress.Root flexShrink="0" max={100} value={progress.percent} w="10">
          <Progress.Track
            aria-label={`${t('widgets.imageMap.progress.label')}: ${label}`}
            aria-valuenow={progress.percent}
          >
            <Progress.Range />
          </Progress.Track>
        </Progress.Root>
        <Text fontVariantNumeric="tabular-nums" truncate>
          {t('widgets.imageMap.progress.compact', { compact: progress.compact })}
        </Text>
      </HStack>
    </Tooltip>
  );
};

export const ImageIndexProgressInline = ({ counts, updatedAt }: ImageIndexProgressProps) => {
  const { t } = useTranslation();
  const progress = describeIndexProgress(counts, t, useCountsAge(updatedAt));
  const label = progress.stale
    ? t('widgets.imageMap.progress.badgeStale', { counts: progress.counts, stale: progress.stale })
    : t('widgets.imageMap.progress.inline', { counts: progress.counts });

  return (
    <Tooltip content={label}>
      {/* Use minW=0 and truncation so six-digit footer counts cannot cover refresh in narrow panels. */}
      <HStack gap="1.5" minW="0" overflow="hidden">
        <Progress.Root flexShrink="0" max={100} value={progress.percent} w="10">
          {/* The counts go in the name too: at the widget's minimum width the label beside it truncates to a couple of
              characters, and the tooltip carrying the full text is hover-only. */}
          <Progress.Track
            aria-label={`${t('widgets.imageMap.progress.label')}: ${label}`}
            aria-valuenow={progress.percent}
          >
            <Progress.Range />
          </Progress.Track>
        </Progress.Root>
        <Text fontVariantNumeric="tabular-nums" truncate>
          {t('widgets.imageMap.progress.compact', { compact: progress.compact })}
        </Text>
      </HStack>
    </Tooltip>
  );
};