Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

6.4 KB
Raw Blame
'use client';

import { ActionIcon, createStaticStyles, cssVar, cx, Flexbox, Tag, Tooltip } from '@lobehub/ui';
import dayjs from 'dayjs';
import { ArrowLeftRightIcon, RotateCcwIcon } from 'lucide-react';
import type { MouseEvent } from 'react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { useAuthorInfo } from '@/business/client/hooks/useAuthorInfo';
import { useFetchWorkspaceMembers } from '@/business/client/hooks/useFetchWorkspaceMembers';
import MemberProfilePopup from '@/features/MemberProfileCard/MemberProfilePopup';
import { useEventCallback } from '@/hooks/useEventCallback';
import type { DocumentHistorySaveSource } from '@/server/routers/lambda/_schema/documentHistory';
import { densityVars } from '@/styles/densityVars';

import { formatHistoryAbsoluteTime, formatHistoryRowTime } from './formatHistoryDate';
import { historyItemSelectors, useHistoryItemsStore } from './HistoryItemsProvider';

type TagColor = 'default' | 'success' | 'purple' | 'geekblue' | 'gold' | 'processing';

const SOURCE_TAG_COLOR: Record<DocumentHistorySaveSource, TagColor> = {
  autosave: 'default',
  llm_call: 'purple',
  manual: 'success',
  restore: 'geekblue',
  system: 'gold',
};

const styles = createStaticStyles(({ css }) => ({
  description: css`
    overflow: hidden;

    font-size: 12px;
    line-height: 1.2;
    color: ${cssVar.colorTextTertiary};
    text-overflow: ellipsis;
    white-space: nowrap;
  `,
  rowMain: css`
    overflow: hidden;
    flex: 1;
    min-width: 0;
  `,
  actions: css`
    pointer-events: none;

    position: absolute;
    inset-block: 50%;
    inset-inline-end: 10px;
    transform: translateY(-50%);

    display: flex;
    gap: 2px;
    align-items: center;

    opacity: 0;

    transition: opacity ${cssVar.motionDurationMid} ${cssVar.motionEaseInOut};
  `,
  currentBadge: css`
    flex-shrink: 0;
    margin: 0;
  `,
  row: css`
    cursor: pointer;

    position: relative;

    display: flex;
    gap: 8px;
    align-items: center;

    padding-block: 8px;
    padding-inline: ${densityVars.cardPadding};

    transition: background ${cssVar.motionDurationMid} ${cssVar.motionEaseInOut};

    &:hover,
    &:focus-within {
      background: ${cssVar.colorFillQuaternary};

      .history-source-tag {
        opacity: 0;
      }

      .history-actions {
        pointer-events: auto;
        opacity: 1;
      }
    }
  `,
  rowCurrent: css`
    cursor: default;

    &:hover,
    &:focus-within {
      background: transparent;

      .history-source-tag {
        opacity: 1;
      }
    }
  `,
  rowRight: css`
    position: relative;

    display: flex;
    flex-shrink: 0;
    gap: 8px;
    align-items: center;

    margin-inline-start: auto;
  `,
  rowTime: css`
    flex-shrink: 0;

    font-size: 13px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    line-height: 1;
    color: ${cssVar.colorText};
  `,
  rowTimeCurrent: css`
    font-size: ${densityVars.fontSize};
    color: ${cssVar.colorPrimary};
  `,
  sourceTag: css`
    flex-shrink: 0;
    margin: 0;
    transition: opacity ${cssVar.motionDurationMid} ${cssVar.motionEaseInOut};
  `,
}));

export interface HistoryListItemProps {
  historyId: string;
  onCompare: (historyId: string) => void;
  onRestore: (historyId: string) => void;
}

export const HistoryListItem = memo<HistoryListItemProps>(({ historyId, onCompare, onRestore }) => {
  const { t } = useTranslation(['common', 'file']);
  const item = useHistoryItemsStore(historyItemSelectors.itemById(historyId));
  const isRestoring = useHistoryItemsStore(historyItemSelectors.isRestoring(historyId));
  const authorInfo = useAuthorInfo(item?.userId);
  useFetchWorkspaceMembers({ enabled: !!item?.userId });

  const handleCompare = useEventCallback(() => {
    onCompare(historyId);
  });

  const handleRestore = useEventCallback(() => {
    onRestore(historyId);
  });

  const handleStopPropagation = useEventCallback((event: MouseEvent) => {
    event.stopPropagation();
  });

  if (!item) return null;

  const timeLabel = formatHistoryRowTime(item.savedAt);
  const saveSourceLabel = t(`pageEditor.history.saveSource.${item.saveSource}`, { ns: 'file' });

  return (
    <div
      className={cx(styles.row, item.isCurrent && styles.rowCurrent)}
      onClick={item.isCurrent ? undefined : handleCompare}
    >
      <Flexbox align={'flex-start'} className={styles.rowMain} gap={4}>
        <Tooltip title={formatHistoryAbsoluteTime(item.savedAt)}>
          <span className={cx(styles.rowTime, item.isCurrent && styles.rowTimeCurrent)}>
            {timeLabel}
          </span>
        </Tooltip>
        <span className={styles.description}>
          {authorInfo?.fullName && (
            <>
              <MemberProfilePopup placement={'left'} userId={item.userId}>
                {authorInfo.fullName}
              </MemberProfilePopup>
              {' · '}
            </>
          )}
          {dayjs(item.savedAt).fromNow()}
        </span>
      </Flexbox>

      <div className={styles.rowRight}>
        {item.isCurrent && (
          <Tag
            className={styles.currentBadge}
            color={'processing'}
            size={'small'}
            variant={'borderless'}
          >
            {t('pageEditor.history.current', { ns: 'file' })}
          </Tag>
        )}

        {!item.isCurrent && (
          <Tag
            className={cx(styles.sourceTag, 'history-source-tag')}
            color={SOURCE_TAG_COLOR[item.saveSource]}
            size={'small'}
            variant={'borderless'}
          >
            {saveSourceLabel}
          </Tag>
        )}
        {!item.isCurrent && (
          <Flexbox
            horizontal
            align={'center'}
            className={cx(styles.actions, 'history-actions')}
            gap={2}
            onClick={handleStopPropagation}
          >
            <ActionIcon
              icon={ArrowLeftRightIcon}
              size={{ blockSize: 26, borderRadius: '50%', size: 14 }}
              title={t('pageEditor.history.compare', { ns: 'file' })}
              onClick={handleCompare}
            />
            <ActionIcon
              icon={RotateCcwIcon}
              loading={isRestoring}
              size={{ blockSize: 26, borderRadius: '50%', size: 14 }}
              title={t('pageEditor.history.restore', { ns: 'file' })}
              onClick={handleRestore}
            />
          </Flexbox>
        )}
      </div>
    </div>
  );
});

HistoryListItem.displayName = 'HistoryListItem';