All files / src/entities/layer/ui LayersPanel.tsx

51.11% Statements 23/45
36% Branches 9/25
41.66% Functions 5/12
55.55% Lines 20/36

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                          1x       7x   7x   7x   7x   7x 7x   7x 7x         7x 7x   6x 6x 6x 6x       7x         6x                   6x   1x 1x                                                                                                                                                            
import React, { useEffect, useMemo, useState } from 'react';
import { useAppDispatch, useAppSelector } from '@/store/hooks';
import { motion, Reorder } from 'framer-motion';
import {
	createLayer,
	deleteLayer,
	fetchLayersByProject,
	setActiveLayerId,
	updateLayer,
} from '@/entities/layer/model/slice';
import { layersSelectors, makeSelectByProject } from '@/entities/layer/model/selectors';
import { AddLayerButton, LayerItem, OpacitySlider } from '@/entities/layer/ui/components';
import { LAYER_DEFAULTS } from '@/shared/constants';
import { store } from '@/store';
import { selectHistoryIsPreview } from '@/entities/history/model';
 
interface LayersPanelProps {
	projectId: string;
	open: boolean;
}
 
export const LayersPanel = React.memo(function LayersPanel({
	projectId,
	open,
}: LayersPanelProps) {
	const dispatch = useAppDispatch();
 
	const selectByProject = useMemo(makeSelectByProject, [projectId]);
 
	const layers = useAppSelector((s) => selectByProject(s, projectId))
		.slice()
		.sort((a, b) => b.zIndex - a.zIndex);
 
	const activeLayer = useAppSelector(layersSelectors.selectActiveLayer);
	const isPreview = useAppSelector(selectHistoryIsPreview);
 
	const [localNameId, setLocalNameId] = useState<string | null>(null);
	const [openMenuId, setOpenMenuId] = useState<string | null>(null);
 
	// ───────────────────────────────
	// Fetch layers when first open
	// ───────────────────────────────
	useEffect(() => {
		if (!open || isPreview) return;
 
		const state = store.getState();
		const existingCount = Object.keys(state.layers.entities).length;
		Eif (existingCount === 0) {
			void dispatch(fetchLayersByProject(projectId));
		}
	}, [dispatch, projectId, open, isPreview]);
 
	if (!open) return null;
 
	// ───────────────────────────────
	// Handlers
	// ───────────────────────────────
	const handleReorder = (newOrder: typeof layers) => {
		if (isPreview) return;
		newOrder.forEach((layer, idx) => {
			const newZ = newOrder.length - 1 - idx;
			if (layer.zIndex !== newZ) {
				dispatch(updateLayer({ id: layer.id, changes: { zIndex: newZ } }));
			}
		});
	};
 
	const handlers = {
		onCreate: () => {
			Iif (isPreview) return;
			void dispatch(createLayer({ projectId }));
		},
		onDelete: (id: string) => {
			if (isPreview) return;
			void dispatch(deleteLayer(id));
			setOpenMenuId(null);
		},
		onOpacityChange: (id: string, value: number) => {
			if (isPreview) return;
			void dispatch(updateLayer({ id, changes: { opacity: value } }));
		},
		onSetActive: (id: string) => dispatch(setActiveLayerId(id)),
		onToggleVisibility: (id: string, visible: boolean) => {
			if (isPreview) return;
			void dispatch(updateLayer({ id, changes: { visible: !visible } }));
		},
		onRenameSubmit: async (id: string, name: string) => {
			if (isPreview) return;
			setLocalNameId(null);
			await dispatch(
				updateLayer({
					id,
					changes: {
						name: name.trim() || `${LAYER_DEFAULTS.UNTITLED_PROJECT}`,
					},
				}),
			);
		},
	};
 
	// ───────────────────────────────
	// UI
	// ───────────────────────────────
	return (
		<motion.aside
			initial={{ x: '100%', opacity: 0 }}
			animate={{ x: 0, opacity: 1 }}
			exit={{ x: '100%', opacity: 0 }}
			transition={{ type: 'spring', stiffness: 220, damping: 25 }}
			className="fixed top-0 right-16 bottom-16 w-72 border-l border-gray-200 bg-white/90 backdrop-blur-sm shadow-xl p-3 flex flex-col gap-3 z-40"
		>
			<header className="flex items-center justify-between">
				<h3 className="text-sm font-semibold text-gray-800">
					Layers{' '}
					{isPreview && <span className="text-amber-600 ml-1">(Preview)</span>}
				</h3>
			</header>
 
			<OpacitySlider
				activeLayer={activeLayer}
				onOpacityChange={handlers.onOpacityChange}
			/>
 
			<AddLayerButton onCreate={handlers.onCreate} disabled={isPreview} />
 
			{/* The list of layers */}
			<Reorder.Group axis="y" values={layers} onReorder={handleReorder}>
				{layers.map((layer) => (
					<Reorder.Item key={layer.id} value={layer}>
						<LayerItem
							layer={layer}
							isActive={activeLayer?.id === layer.id}
							isEditing={localNameId === layer.id}
							openMenuId={openMenuId}
							onSetActive={handlers.onSetActive}
							onToggleVisibility={handlers.onToggleVisibility}
							onRenameSubmit={handlers.onRenameSubmit}
							onDelete={handlers.onDelete}
							setLocalNameId={setLocalNameId}
							setOpenMenuId={setOpenMenuId}
							disabled={isPreview}
						/>
					</Reorder.Item>
				))}
			</Reorder.Group>
		</motion.aside>
	);
});