import React from 'react'; import { View, Text, StyleSheet, ScrollView } from 'react-native'; import { Colors } from '../../ui/theme/colors'; import { FontSize, FontWeight, Radius, Spacing } from '../../ui/theme/layout'; import { safeStringify } from '../../utils/safeStringify'; import { CollapsibleSection } from '../../ui/shared/CollapsibleSection'; import { JsonView } from '../../ui/shared/JsonView'; import { CopyButton } from '../../ui/shared/CopyButton'; import { LogListScreen } from '../../ui/shared/LogListScreen'; import { LogRow, LogRowMetaText } from '../../ui/shared/LogRow'; import type { DebugFeatureRenderProps } from '../../types'; import type { StateLogEntry } from '../../types/logs'; import { t } from '../../i18n'; /** Middleware / subscribe fallbacks that are not useful as a list title. */ const GENERIC_ACTIONS = new Set(['change']); const getActionColor = (action: string): string => { if (action.includes('add') || action.includes('create')) {return Colors.success;} if (action.includes('remove') || action.includes('delete')) {return Colors.error;} if (action.includes('update') || action.includes('set')) {return Colors.primary;} return Colors.info; }; const getActionBgColor = (action: string): string => { if (action.includes('add') || action.includes('create')) {return Colors.successDim;} if (action.includes('remove') || action.includes('delete')) {return Colors.errorDim;} if (action.includes('update') || action.includes('set')) {return Colors.primaryGhost;} return 'rgba(14,165,233,0.12)'; }; export function findStateChanges(prev: unknown, next: unknown): string[] { if (typeof prev !== 'object' || typeof next !== 'object' || !prev || !next) {return [];} const allKeys = new Set([...Object.keys(prev as object), ...Object.keys(next as object)]); const changed: string[] = []; allKeys.forEach((key) => { const pv = (prev as Record)[key]; const nv = (next as Record)[key]; // Skip action methods — only surface state field diffs. if (typeof pv === 'function' || typeof nv === 'function') {return;} if (safeStringify(pv) !== safeStringify(nv)) {changed.push(key);} }); return changed; } /** * List title = what changed (keys / named action). * Store name belongs in the footer — same split as Navigation (route vs action). */ export function resolveStateLogTitle(item: StateLogEntry): { title: string; colorKey: string; namedAction: string | null; changes: string[]; } { const changes = findStateChanges(item.before, item.after); const namedAction = item.action && !GENERIC_ACTIONS.has(item.action) ? item.action : null; if (changes.length > 0) { return { title: changes.join(', '), colorKey: namedAction ?? 'update', namedAction, changes, }; } if (namedAction) { return { title: namedAction, colorKey: namedAction, namedAction, changes, }; } return { title: t('state.stateChange'), colorKey: 'update', namedAction: null, changes, }; } export function renderStateLogRow(item: StateLogEntry) { const { title, colorKey, namedAction } = resolveStateLogTitle(item); return ( {item.storeId ? ( {item.storeId} ) : null} {namedAction && namedAction !== title ? ( {namedAction} ) : null} )} trailingMetadata={( <> {new Date(item.timestamp).toLocaleTimeString()} )} /> ); } export const StateLogTab: React.FC> = React.memo(({ snapshot, }) => ( { const { title, colorKey } = resolveStateLogTitle(item); return ( {title} {item.storeId ? ( {item.storeId} ) : null} ); }} renderDetailBody={(item) => { const { namedAction } = resolveStateLogTitle(item); const changes = findStateChanges(item.before, item.after); return ( {t('common.time')} {new Date(item.timestamp).toLocaleString()} {(item.storeId || namedAction) ? ( {item.storeId ? ( {t('common.store')} {item.storeId} ) : null} {item.storeId && namedAction ? : null} {namedAction ? ( {t('common.action')} {namedAction} ) : null} ) : null} {changes.length > 0 && ( {t('common.changedKeys')} {changes.map((key) => ( {key} ))} )} ); }} /> )); const s = StyleSheet.create({ actionIcon: { width: 26, height: 26, borderRadius: Radius.SM, alignItems: 'center', justifyContent: 'center', }, actionDot: { width: 8, height: 8, borderRadius: 4 }, action: { fontSize: FontSize.MD, fontWeight: FontWeight.semibold, color: Colors.text }, storeId: { color: Colors.textSecondary, fontSize: FontSize.XS, fontWeight: FontWeight.medium, }, actionBadge: { backgroundColor: Colors.primaryGhost, paddingHorizontal: Spacing.SM, paddingVertical: 1, borderRadius: Radius.XS, }, actionBadgeText: { fontSize: FontSize.XS, color: Colors.primary, fontWeight: FontWeight.semibold }, time: { fontSize: FontSize.XS, color: Colors.textSecondary }, detailHeaderCenter: { flexDirection: 'row', alignItems: 'center', gap: Spacing.SM, flex: 1, }, detailAction: { fontSize: FontSize.LG, fontWeight: FontWeight.bold }, detailBody: { flex: 1 }, detailBodyContent: { padding: Spacing.MD, paddingBottom: 40 }, sectionWithCopy: { gap: Spacing.SM }, metaCard: { backgroundColor: Colors.surface, borderRadius: Radius.LG, padding: Spacing.MD, marginBottom: Spacing.SM, flexDirection: 'row', alignItems: 'center', }, metaItem: { flex: 1 }, metaDivider: { width: 1, height: 28, backgroundColor: Colors.border, marginHorizontal: Spacing.MD }, metaLabel: { fontSize: FontSize.XS, color: Colors.textSecondary, fontWeight: FontWeight.semibold, textTransform: 'uppercase', marginBottom: 2 }, metaValue: { fontSize: FontSize.MD, color: Colors.text, fontWeight: FontWeight.medium }, changesCard: { backgroundColor: Colors.surface, borderRadius: Radius.LG, padding: Spacing.MD, marginBottom: Spacing.SM, }, changesTitle: { fontSize: FontSize.SM, fontWeight: FontWeight.semibold, color: Colors.textSecondary, marginBottom: Spacing.SM }, changesTags: { flexDirection: 'row', flexWrap: 'wrap', gap: Spacing.XXS }, changeTag: { backgroundColor: Colors.warningDim, paddingHorizontal: Spacing.SM, paddingVertical: Spacing.XXS, borderRadius: Radius.XS, borderWidth: 1, borderColor: Colors.warning, }, changeTagText: { fontSize: FontSize.XS, color: Colors.warning, fontWeight: FontWeight.semibold }, });