UIPackage React Native
Search

Activity Log

A native audit feed for actor, action, time, and optional filtered event details.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add activity-log

Source files

Expand a file to read the source the installer copies into your app.

components/blocks/activity-log/ActivityLog.tsx
import * as React from 'react'
import { FlatList, View, useWindowDimensions, type ViewProps } from 'react-native'
import { ListItem, ListItemContent, ListItemDescription, ListItemTitle, ListSeparator } from '@/components/ui/list'
import { SegmentedControl, SegmentedControlItem } from '@/components/ui/segmented-control'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export interface ActivityLogEntry {
  id: string
  actor: string
  action: string
  target?: string
  /** A caller-localized time or relative date. */
  time: string
  category?: string
}

export interface ActivityLogCategory {
  /** A unique nonempty ID. */
  id: string
  label: string
}

const defaultLabels = {
  filter: 'Filter by action category',
  all: 'All actions',
  empty: 'No activity yet.',
  noMatches: 'No activity in this category.',
  matches: (count: number) => `${count} ${count === 1 ? 'event' : 'events'}`,
}

export interface ActivityLogProps extends Omit<ViewProps, 'children'> {
  className?: string
  /** Display order and event IDs are caller-owned. */
  entries?: readonly ActivityLogEntry[]
  categories?: readonly ActivityLogCategory[]
  /** Pass null for no category filter. Omit for local filter state. */
  category?: string | null
  onCategoryChange?: (id: string | null) => void
  onOpen?: (id: string) => void
  header?: React.ReactNode
  labels?: Partial<typeof defaultLabels>
}

export function ActivityLog({
  className,
  entries = [],
  categories = [],
  category,
  onCategoryChange,
  onOpen,
  header,
  labels,
  ...props
}: ActivityLogProps) {
  const copy = { ...defaultLabels, ...labels }
  const [internalCategory, setInternalCategory] = React.useState<string | null>(null)
  const requestedCategory = category === undefined ? internalCategory : category
  const validCategories = categories.filter(({ id }) => Boolean(id))
  const activeCategory = validCategories.find(({ id }) => id === requestedCategory)
  const visible = activeCategory ? entries.filter((entry) => entry.category === activeCategory.id) : entries
  const { width, fontScale } = useWindowDimensions()
  const stackMeta = width < 360 || fontScale >= 1.4
  const summary = activeCategory ? (visible.length ? copy.matches(visible.length) : copy.noMatches) : undefined
  useAnnouncement(summary)

  return (
    <View {...props} className={cn('min-h-0 flex-1', className)}>
      <FlatList
        data={visible}
        keyExtractor={(entry) => entry.id}
        ItemSeparatorComponent={ListSeparator}
        contentContainerStyle={{ paddingBottom: 16 }}
        ListHeaderComponent={
          <View className="gap-3 pb-3">
            {header}
            {validCategories.length ? (
              <SegmentedControl
                accessibilityLabel={copy.filter}
                value={activeCategory?.id ?? ''}
                onValueChange={(value) => {
                  const next = validCategories.find(({ id }) => id === value)?.id
                  if (value !== '' && !next) return
                  if (category === undefined) setInternalCategory(next ?? null)
                  onCategoryChange?.(next ?? null)
                }}
              >
                <SegmentedControlItem value="">{copy.all}</SegmentedControlItem>
                {validCategories.map(({ id, label }) => (
                  <SegmentedControlItem key={id} value={id}>
                    {label}
                  </SegmentedControlItem>
                ))}
              </SegmentedControl>
            ) : null}
            {activeCategory && visible.length ? (
              <Text size="sm" variant="muted">
                {summary}
              </Text>
            ) : null}
          </View>
        }
        ListEmptyComponent={
          <Text className="rounded-xl border border-border px-4 py-6 text-center">
            {activeCategory ? copy.noMatches : copy.empty}
          </Text>
        }
        renderItem={({ item }) => (
          <ListItem
            accessible
            accessibilityLabel={[item.actor, item.action, item.target, item.time].filter(Boolean).join(', ')}
            onPress={onOpen ? () => onOpen(item.id) : undefined}
            className="items-start px-0"
          >
            <ListItemContent className="gap-1">
              <View className={cn('gap-1', !stackMeta && 'flex-row items-baseline justify-between gap-3')}>
                <ListItemTitle className={cn(!stackMeta && 'min-w-0 flex-1')}>{item.actor}</ListItemTitle>
                <Text size="sm" variant="muted" className={cn(!stackMeta && 'max-w-[45%]')}>
                  {item.time}
                </Text>
              </View>
              <Text>{item.action}</Text>
              {item.target ? <ListItemDescription>{item.target}</ListItemDescription> : null}
            </ListItemContent>
          </ListItem>
        )}
      />
    </View>
  )
}
components/blocks/activity-log/index.ts
export { ActivityLog, type ActivityLogCategory, type ActivityLogEntry, type ActivityLogProps } from './ActivityLog'

Dependencies

Browse accounts and messaging