Skip to content

Commit e793bd9

Browse files
committed
feat: optimize position calculation
1 parent 287db84 commit e793bd9

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/components/EncapsulatedPacket.tsx

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
import { useMemo } from 'react'
22
import { TCP_IP_LAYERS } from '@/data'
3-
import { Mail } from 'lucide-react'
43
import type { PacketStep } from '@/utils/getPacketForStep'
54

5+
const CLIENT_X = 20
6+
const SERVER_X = 80
7+
const TRANSMISSION_X = 50
8+
9+
const STAGE_TO_X = [
10+
CLIENT_X,
11+
CLIENT_X,
12+
CLIENT_X,
13+
CLIENT_X,
14+
TRANSMISSION_X,
15+
SERVER_X,
16+
SERVER_X,
17+
SERVER_X,
18+
SERVER_X,
19+
] as const
20+
621
interface Props {
722
packet: PacketStep
823
stage: number
924
}
1025

1126
function EncapsulatedPacket(props: Props) {
27+
const currentPosition = useMemo(
28+
() => ({
29+
x: STAGE_TO_X[props.stage],
30+
y: `calc(160px + ((var(--spacing) * 20) * ${props.stage}))`,
31+
}),
32+
[props.stage],
33+
)
34+
1235
const visibleHeaders = useMemo(() => {
1336
const visibleHeaders: number[] = []
1437

@@ -19,32 +42,7 @@ function EncapsulatedPacket(props: Props) {
1942
return visibleHeaders
2043
}, [props.stage])
2144

22-
const currentPosition = useMemo(() => {
23-
const isClientToServer = props.packet.from === 'client'
24-
const clientX = 16
25-
const serverX = 84
26-
27-
// Calculate positions for each stage
28-
const positions = [
29-
{ x: isClientToServer ? clientX : serverX, y: 160 }, // Application
30-
{ x: isClientToServer ? clientX : serverX, y: 260 }, // Transport
31-
{ x: isClientToServer ? clientX : serverX, y: 360 }, // Internet
32-
{ x: isClientToServer ? clientX : serverX, y: 460 }, // Network Interface
33-
{
34-
x: isClientToServer ? (clientX + serverX) / 2 : (serverX + clientX) / 2,
35-
y: 500,
36-
}, // Transmission
37-
{ x: isClientToServer ? serverX : clientX, y: 460 }, // Network Interface (receiver)
38-
{ x: isClientToServer ? serverX : clientX, y: 360 }, // Internet (receiver)
39-
{ x: isClientToServer ? serverX : clientX, y: 260 }, // Transport (receiver)
40-
{ x: isClientToServer ? serverX : clientX, y: 160 }, // Application (receiver)
41-
]
42-
43-
// Get position for current stage
44-
const position = positions[props.stage]
45-
46-
return position
47-
}, [props.stage, props.packet.from])
45+
const Icon = props.packet.type.icon
4846

4947
return (
5048
<div
@@ -59,8 +57,8 @@ function EncapsulatedPacket(props: Props) {
5957
{/* Base packet (envelope) */}
6058
<div className="relative">
6159
{/* Data payload (envelope) */}
62-
<div className="w-16 h-12 bg-white border-2 border-black rounded flex items-center justify-center">
63-
<Mail className="w-8 h-8 text-black" />
60+
<div className="w-16 h-12 mt-2 bg-white border-2 border-black rounded flex items-center justify-center">
61+
<Icon className="w-8 h-8 text-black" />
6462
</div>
6563

6664
{/* Layer headers (colored borders) */}
@@ -89,7 +87,7 @@ function EncapsulatedPacket(props: Props) {
8987

9088
{/* Packet type label */}
9189
<div
92-
className="absolute -bottom-5 left-1/2 transform -translate-x-1/2 text-[10px] font-bold px-1 rounded"
90+
className="absolute -bottom-6 left-1/2 transform -translate-x-1/2 whitespace-nowrap text-sm font-bold text-center px-1 rounded"
9391
style={{ backgroundColor: props.packet.type.color, color: 'white' }}
9492
>
9593
{props.packet.type.name}

0 commit comments

Comments
 (0)