All files / apps/host/src/components/ApprovalTimelineItem index.tsx

100% Statements 21/21
100% Branches 21/21
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                      3x 13x   13x 13x 13x           13x 13x       13x 13x 5x 5x 2x   3x 1x   2x 1x   1x   8x           13x                                                       13x                                                                                                    
import React, { memo, useMemo } from 'react';
import { Text, View } from 'react-native';
 
import { CachedImage } from '@repo/ui/components/CachedImage';
import { makeStyles } from '@repo/ui/themes/makeStyles';
 
import { PROCESSING_BY_RESPONSE } from '@/constants/apis';
import { ACTION_MAP, HISTORY_ACTION_MESSAGES } from '@/constants/request';
 
import { Activity } from '@/types/request';
import { ActivityState, RequestCategory } from '@/types/request';
 
type Props = {
  activity: Activity;
  isLast?: boolean;
  processingBy?: string;
  category?: string;
};
 
export const ApprovalTimelineItem = memo(({ activity, isLast, processingBy, category }: Props) => {
  const styles = useStyles();
 
  const { day, time } = useMemo(() => {
    const date = new Date(activity.occurredAt);
    return {
      day: date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }).toUpperCase(),
      time: date.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: false }),
    };
  }, [activity.occurredAt]);
 
  const avatarSource = useMemo(
    () => ({ uri: activity.avatar, cache: 'immutable' as const, priority: 'normal' as const }),
    [activity.avatar],
  );
 
  const actionLabel = useMemo(() => {
    if (category === RequestCategory.MAINTENANCE) {
      const isProcessing = processingBy !== PROCESSING_BY_RESPONSE.NONE;
      if (activity.action === ActivityState.CANCEL || activity.state === ActivityState.CANCEL) {
        return HISTORY_ACTION_MESSAGES.CANCELLED;
      }
      if (isProcessing && activity.state === ActivityState.PENDING) {
        return HISTORY_ACTION_MESSAGES.PROCEEDED;
      }
      if (isProcessing && activity.action === ActivityState.DONE) {
        return HISTORY_ACTION_MESSAGES.COMPLETED;
      }
      return HISTORY_ACTION_MESSAGES.CLOSED;
    }
    return (
      (activity.action && ACTION_MAP[activity.action]) ||
      (activity.isApproved ? HISTORY_ACTION_MESSAGES.APPROVED : HISTORY_ACTION_MESSAGES.REJECTED)
    );
  }, [category, processingBy, activity.action, activity.state, activity.isApproved]);
 
  return (
    <View style={styles.container}>
      {/* LEFT: AVATAR + TIMELINE */}
      <View style={styles.left}>
        <CachedImage source={avatarSource} style={styles.avatar} resizeMode="cover" />
 
        {!isLast && (
          <View style={styles.timeline}>
            <View style={styles.line} />
            <View style={styles.dot} />
          </View>
        )}
      </View>
 
      {/* RIGHT: CONTENT */}
      <View style={styles.content}>
        <Text style={styles.title}>
          <Text style={styles.name}>{activity.name}</Text> {actionLabel}
        </Text>
        <Text style={styles.meta}>
          {day} | {time}
        </Text>
        <Text style={styles.meta}>{activity.note}</Text>
      </View>
    </View>
  );
});
 
const useStyles = makeStyles(theme => ({
  container: {
    flexDirection: 'row',
    paddingVertical: theme.metrics.spacing[2],
  },
  left: {
    alignItems: 'center',
  },
  avatar: {
    width: theme.metrics.spacing[12.5],
    height: theme.metrics.spacing[12.5],
    borderRadius: theme.metrics.borderRadius[6],
    marginBottom: theme.metrics.spacing[1],
  },
  timeline: {
    alignItems: 'center',
    flex: 1,
  },
  dot: {
    width: theme.metrics.spacing[2],
    height: theme.metrics.spacing[2],
    borderRadius: theme.metrics.borderRadius[1],
    backgroundColor: theme.colors.text.tertiary,
  },
  line: {
    flex: 1,
    width: theme.metrics.spacing[0.25],
    height: theme.metrics.spacing[5],
    backgroundColor: theme.colors.text.tertiary,
    marginBottom: theme.metrics.spacing[0.5],
  },
  content: {
    flex: 1,
    paddingLeft: theme.metrics.spacing[2],
    paddingTop: theme.metrics.spacing[1],
  },
  title: {
    fontSize: theme.metrics.textSize[18],
    color: theme.colors.text.primary,
  },
  name: {
    fontWeight: theme.metrics.fontWeight.semiBold,
  },
  meta: {
    marginTop: theme.metrics.spacing[1],
    fontSize: theme.metrics.textSize[12],
    color: theme.colors.text.tertiary,
    letterSpacing: 0.5,
  },
}));