Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

7.0 KB
Raw Blame
import { TEAMMATE_AVATAR } from '@cindy/maker-shared/teammate-status';
/**
 * 首页伙伴段的一行(伙伴与群聊共用,对齐任务行的几何):固定 78 高;44 头像距左 16,文字列从 72 开始,
 * 右内边距 16;分割线画在文字列底边,从 72 一直到屏幕右缘,最后一行不画。
 *
 * 第一行:名字(18/28 500)+ 同名时的「· 电脑名」+ 右侧时间位。时间位只放一样东西:时间。第二行:预览(15/26)+ 可选未读数量。
 * 伙伴与群聊的头像右下角共用实心状态点,待确认也通过预览前缀呈现。见 apps/mobile/docs/mobile-design-guide.md §3 的列表节奏例外。
 */
import type { ReactElement, ReactNode } from 'react';
import { Pressable, StyleSheet, View } from 'react-native';
import { Text } from '@/components/AppText';
import { StatusDot } from '@/components/MobilePrimitives';
import { fontWeight, lineHeight, radius, spacing, typeScale, useThemedStyles, type ThemeColors } from '@/theme';
import { SessionRightSpinner } from './SessionRightSpinner';

export const COMPANION_ROW_HEIGHT = 78;
export const COMPANION_ROW_AVATAR_SIZE = TEAMMATE_AVATAR.row;
/** Shared 10pt status point; its 2pt isolation border is outside this size. */
export const COMPANION_ROW_DOT_SIZE = TEAMMATE_AVATAR.dot;
const LEFT_INSET = spacing.lg;

export type CompanionRowTrailing = { kind: 'time'; text: string } | { kind: 'working' };

export function CompanionListRow({
  avatar, title, device, trailing, preview, previewTone = 'secondary', previewPrefix, previewPrefixTestID,
  unread = false, unreadCount, mentioned = false, unreadLabel, disabled = false, last = false, accessibilityLabel, onPress, testID,
}: {
  /** Content of the 44pt avatar box (the caller draws the shared status dot). */
  avatar: ReactNode;
  title: string;
  /** Computer name, only when two rows share a name. */
  device?: string;
  trailing: CompanionRowTrailing;
  /** Plain text, or a live element (the working copy) that renders outside the text node. */
  preview: string | ReactElement;
  previewTone?: 'secondary' | 'tertiary';
  /** 「等你确认」「需要关注」「离线」 — what needs the user, before the preview. */
  previewPrefix?: string;
  previewPrefixTestID?: string;
  unread?: boolean;
  unreadCount?: number;
  mentioned?: boolean;
  unreadLabel?: string;
  disabled?: boolean;
  last?: boolean;
  accessibilityLabel: string;
  onPress(): void;
  testID?: string;
}) {
  const styles = useThemedStyles(makeStyles);
  return <Pressable accessibilityRole="button" accessibilityLabel={accessibilityLabel} accessibilityState={{ disabled }}
    disabled={disabled} onPress={onPress} style={({ pressed }) => [styles.row, pressed && !disabled && styles.pressed]} testID={testID}>
    <View style={styles.avatar}>{avatar}</View>
    <View style={[styles.body, !last && styles.divider]}>
      <View style={styles.titleRow}>
        <Text numberOfLines={1} style={styles.title}>{title}</Text>
        {device ? <Text numberOfLines={1} style={styles.device}>{`· ${device}`}</Text> : null}
        <View style={styles.spacer} />
        <View style={styles.trailing}>
          {trailing.kind === 'working'
            ? <SessionRightSpinner testID="companion.row.working" />
            : <Text numberOfLines={1} style={styles.time}>{trailing.text}</Text>}
        </View>
      </View>
      <View style={styles.previewRow}>
        {typeof preview === 'string'
          ? <Text numberOfLines={1} style={[styles.preview, previewTone === 'tertiary' && styles.previewQuiet]}>
            {previewPrefix ? <Text style={styles.prefix} testID={previewPrefixTestID}>{`${previewPrefix} · `}</Text> : null}
            {preview}
          </Text>
          : <View style={styles.previewSlot}>{preview}</View>}
        {unread ? unreadCount !== undefined
          ? <Text style={styles.unreadCount} accessibilityLabel={unreadLabel} testID="companion.row.unread">
            {`${mentioned ? '@ ' : ''}${unreadCount > 99 ? '99+' : unreadCount}`}
          </Text>
          : <View style={styles.unread} accessibilityLabel={unreadLabel} testID="companion.row.unread" /> : null}
      </View>
    </View>
  </Pressable>;
}

/** Presence dot on the avatar's bottom-right, ringed with the page surface (desktop BotConnectionStatus). */
/** The shared status dot in a page-coloured ring, so it stays legible over the avatar. */
export function CompanionPresenceDot({ connected, testID = 'teammate.connection' }: { connected: boolean | null; testID?: string }) {
  const styles = useThemedStyles(makeStyles);
  return <View testID={testID} style={styles.presence}>
    <StatusDot tone={connected === null ? 'muted' : connected ? 'ready' : 'off'} />
  </View>;
}

const makeStyles = (colors: ThemeColors) => StyleSheet.create({
  row: { flexDirection: 'row', alignItems: 'center', gap: spacing.md, height: COMPANION_ROW_HEIGHT, paddingLeft: LEFT_INSET },
  pressed: { opacity: 0.72 },
  avatar: { width: COMPANION_ROW_AVATAR_SIZE, height: COMPANION_ROW_AVATAR_SIZE },
  body: { flex: 1, minWidth: 0, alignSelf: 'stretch', justifyContent: 'center', paddingRight: spacing.lg },
  divider: { borderBottomColor: colors.border, borderBottomWidth: StyleSheet.hairlineWidth },
  titleRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.xs, height: lineHeight.listTitle },
  // Home list rhythm exception (mobile guide §3): 18/28 title, 15/26 preview, 13/22 metadata.
  title: { flexShrink: 1, color: colors.textPrimary, fontSize: typeScale.subtitle, lineHeight: lineHeight.listTitle, fontWeight: fontWeight.medium },
  // Aligned to the title line (alignment exception): the name keeps its width, the computer name truncates first.
  device: { flexShrink: 3, color: colors.textTertiary, fontSize: typeScale.bodySmall, lineHeight: lineHeight.listTitle, fontWeight: fontWeight.regular },
  spacer: { flexGrow: 1, minWidth: spacing.sm },
  trailing: { minWidth: COMPANION_ROW_DOT_SIZE, alignItems: 'flex-end', justifyContent: 'center' },
  time: { color: colors.textTertiary, fontSize: typeScale.footnote, lineHeight: lineHeight.body, fontWeight: fontWeight.regular, fontVariant: ['tabular-nums'] },
  previewRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, height: lineHeight.subtitle },
  preview: { flex: 1, minWidth: 0, color: colors.textSecondary, fontSize: typeScale.bodySmall, lineHeight: lineHeight.subtitle, fontWeight: fontWeight.regular },
  previewQuiet: { color: colors.textTertiary },
  previewSlot: { flex: 1, minWidth: 0, flexDirection: 'row' },
  prefix: { color: colors.textPrimary, fontSize: typeScale.bodySmall, lineHeight: lineHeight.subtitle, fontWeight: fontWeight.medium },
  unreadCount: { color: colors.botUnread, fontSize: typeScale.footnote, lineHeight: lineHeight.body, fontWeight: fontWeight.medium, fontVariant: ['tabular-nums'] },
  unread: { width: COMPANION_ROW_DOT_SIZE, height: COMPANION_ROW_DOT_SIZE, borderRadius: radius.pill, backgroundColor: colors.botUnread },
  presence: { position: 'absolute', right: 0, bottom: 0, padding: 2, borderRadius: radius.pill, backgroundColor: colors.surface },
});