Files
tmc-test-task/frontend/app/components/LeftPanel.vue
Oscar 1c69ff56bb upd
2026-06-04 14:37:19 +03:00

642 lines
14 KiB
Vue

<template>
<div class="panel">
<div class="panel-header">
<div class="panel-title-row">
<div class="panel-title-row">
<h2 class="panel-title">
All items
</h2>
<span v-if="total" class="item-count">{{ total }}</span>
</div>
<button class="btn-primary" @click="openModal">
<svg width="11" height="11" viewBox="0 0 11 11" fill="none" aria-hidden="true">
<path d="M5.5 1V10M1 5.5H10" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
</svg>
Add item
</button>
</div>
<div class="search-wrap">
<span class="search-icon" aria-hidden="true">
<svg width="13" height="13" viewBox="0 0 13 13" fill="none">
<circle cx="5.5" cy="5.5" r="4" stroke="currentColor" stroke-width="1.4" />
<path d="M8.5 8.5L11.5 11.5" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" />
</svg>
</span>
<input
class="search-input"
type="text"
placeholder="Search..."
:value="search"
@input="onSearchInput"
>
</div>
</div>
<div ref="panelBody" class="panel-body" @scroll="onScroll">
<div :style="{ height: topSpacer + 'px' }" aria-hidden="true" />
<ul class="item-list">
<li
v-for="(item, vIdx) in visibleItems"
:key="item.id"
class="item-row"
:style="{ '--i': Math.min(vIdx, 14) }"
>
<span class="item-id">#{{ item.id }}</span>
<span class="item-value">{{ item.value }}</span>
<button class="action-btn" title="Move to selected" @click="emit('select', item.id!)">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
<path d="M2.5 7H11.5M8.5 3.5L12 7L8.5 10.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
</svg>
</button>
</li>
</ul>
<div :style="{ height: bottomSpacer + 'px' }" aria-hidden="true" />
<div v-if="loading" class="status-row">
<span class="status-dots">
<span /><span /><span />
</span>
</div>
<div v-if="hasMore && !loading" ref="sentinel" class="sentinel" />
<div v-if="!loading && !hasMore && items.length === 0" class="empty-state">
<span class="empty-label">No items found</span>
</div>
</div>
<Transition name="modal">
<div v-if="showAddModal" class="modal-overlay" @click.self="closeModal">
<div class="modal-card">
<div class="modal-header">
<p class="modal-title">
Add new item
</p>
<button class="modal-close" aria-label="Close" @click="closeModal">
<svg width="13" height="13" viewBox="0 0 13 13" fill="none" aria-hidden="true">
<path d="M1.5 1.5L11.5 11.5M11.5 1.5L1.5 11.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
</svg>
</button>
</div>
<input
ref="addInputEl"
v-model="addValueInput"
class="modal-input"
type="text"
placeholder="Enter item value"
@keydown.enter="submitAdd"
>
<div class="modal-footer">
<button class="btn-ghost" @click="closeModal">
Cancel
</button>
<button class="btn-primary" :disabled="!addValueInput.trim()" @click="submitAdd">
Add item
</button>
</div>
</div>
</div>
</Transition>
</div>
</template>
<script setup lang="ts">
import type { Item } from '~/services/api'
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
const ITEM_HEIGHT = 47
const OVERSCAN = 5
const props = defineProps<{
items: Item[]
total: number | string
loading: boolean
hasMore: boolean
search: string
}>()
const emit = defineEmits<{
(e: 'update:search', val: string): void
(e: 'loadMore'): void
(e: 'select', id: number): void
(e: 'add', value: string): void
}>()
// ── Virtual scroll ───────────────────────────────────────────────────────────
const panelBody = ref<HTMLElement | null>(null)
const scrollTop = ref(0)
const containerHeight = ref(500)
function onScroll() {
scrollTop.value = panelBody.value?.scrollTop ?? 0
}
const startIdx = computed(() =>
Math.max(0, Math.floor(scrollTop.value / ITEM_HEIGHT) - OVERSCAN),
)
const endIdx = computed(() =>
Math.min(
props.items.length,
Math.ceil((scrollTop.value + containerHeight.value) / ITEM_HEIGHT) + OVERSCAN,
),
)
const visibleItems = computed(() => props.items.slice(startIdx.value, endIdx.value))
const topSpacer = computed(() => startIdx.value * ITEM_HEIGHT)
const bottomSpacer = computed(() => (props.items.length - endIdx.value) * ITEM_HEIGHT)
let resizeObserver: ResizeObserver | null = null
onMounted(() => {
containerHeight.value = panelBody.value?.clientHeight ?? 500
resizeObserver = new ResizeObserver((entries) => {
const entry = entries[0]
if (entry)
containerHeight.value = entry.contentRect.height
})
if (panelBody.value)
resizeObserver.observe(panelBody.value)
})
// ── Infinite scroll sentinel ─────────────────────────────────────────────────
const sentinel = ref<HTMLElement | null>(null)
let observer: IntersectionObserver | null = null
watch(sentinel, (el) => {
if (observer) {
observer.disconnect()
observer = null
}
if (el) {
observer = new IntersectionObserver((entries) => {
if (entries[0]?.isIntersecting && props.hasMore && !props.loading) {
emit('loadMore')
}
}, { threshold: 0.1 })
observer.observe(el)
}
})
onUnmounted(() => {
observer?.disconnect()
resizeObserver?.disconnect()
})
// ── Search ───────────────────────────────────────────────────────────────────
let debounceTimer: ReturnType<typeof setTimeout> | null = null
function onSearchInput(e: Event) {
const val = (e.target as HTMLInputElement).value
if (debounceTimer)
clearTimeout(debounceTimer)
debounceTimer = setTimeout(() => {
emit('update:search', val)
}, 300)
}
// ── Add modal ────────────────────────────────────────────────────────────────
const showAddModal = ref(false)
const addValueInput = ref('')
const addInputEl = ref<HTMLInputElement | null>(null)
function openModal() {
showAddModal.value = true
nextTick(() => addInputEl.value?.focus())
}
function closeModal() {
showAddModal.value = false
addValueInput.value = ''
}
function submitAdd() {
const value = addValueInput.value.trim()
if (!value)
return
emit('add', value)
addValueInput.value = ''
showAddModal.value = false
}
</script>
<style scoped lang="scss">
.panel {
display: flex;
flex-direction: column;
height: 100%;
background: var(--surface);
border-right: 1px solid var(--border);
}
/* ── Header ── */
.panel-header {
height: 108px;
padding: 18px 20px 16px;
border-bottom: 1px solid var(--border);
background: var(--surface);
flex-shrink: 0;
}
.item-count {
display: inline-flex;
align-items: center;
justify-content: center;
margin-left: 8px;
min-width: 18px;
height: 18px;
padding: 0 5px;
background: var(--surface-subtle);
border: 1px solid var(--border);
border-radius: 9999px;
font-family: var(--font-mono);
font-size: 0.65rem;
color: var(--text-secondary);
}
.panel-title-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.panel-title {
margin: 0;
font-size: 0.72rem;
font-weight: 500;
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--text-secondary);
font-family: var(--font-sans);
}
.search-wrap {
position: relative;
display: flex;
align-items: center;
}
.search-icon {
position: absolute;
left: 10px;
display: flex;
align-items: center;
color: var(--text-muted);
pointer-events: none;
}
.search-input {
width: 100%;
padding: 7px 12px 7px 30px;
border: 1px solid var(--border);
border-radius: var(--radius-sm);
background: var(--surface-subtle);
font-family: var(--font-sans);
font-size: 0.825rem;
color: var(--text-primary);
outline: none;
transition:
border-color 150ms ease,
background 150ms ease;
&::placeholder {
color: var(--text-muted);
}
&:focus {
border-color: var(--text-primary);
background: var(--surface);
}
}
/* ── Buttons ── */
.btn-primary {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
background: var(--text-primary);
color: #ffffff;
border: none;
border-radius: var(--radius-sm);
font-family: var(--font-sans);
font-size: 0.78rem;
font-weight: 500;
cursor: pointer;
letter-spacing: 0.01em;
white-space: nowrap;
transition:
background 150ms ease,
transform 100ms ease;
&:hover {
background: #333333;
}
&:active {
transform: scale(0.97);
}
&:disabled {
opacity: 0.45;
cursor: not-allowed;
transform: none;
}
}
.btn-ghost {
padding: 7px 14px;
background: none;
border: 1px solid var(--border);
border-radius: var(--radius-sm);
font-family: var(--font-sans);
font-size: 0.825rem;
color: var(--text-secondary);
cursor: pointer;
transition:
color 150ms ease,
border-color 150ms ease,
background 150ms ease;
&:hover {
color: var(--text-primary);
border-color: #999;
background: var(--surface-subtle);
}
}
/* ── Body & list ── */
.panel-body {
flex: 1;
overflow-y: auto;
&::-webkit-scrollbar {
width: 3px;
}
&::-webkit-scrollbar-track {
background: transparent;
}
&::-webkit-scrollbar-thumb {
background: var(--border);
border-radius: 2px;
}
}
.item-list {
list-style: none;
margin: 0;
padding: 0;
}
@keyframes item-in {
from {
opacity: 0;
transform: translateY(6px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.item-row {
display: flex;
align-items: center;
gap: 10px;
padding: 9px 12px 9px 20px;
border-bottom: 1px solid var(--border);
animation: item-in 350ms cubic-bezier(0.16, 1, 0.3, 1) both;
animation-delay: calc(var(--i, 0) * 25ms);
transition: background 120ms ease;
&:hover {
background: var(--surface-subtle);
.action-btn {
opacity: 1;
}
}
}
.item-id {
font-family: var(--font-mono);
font-size: 0.72rem;
color: var(--text-muted);
flex-shrink: 0;
min-width: 44px;
}
.item-value {
flex: 1;
font-family: var(--font-mono);
font-size: 0.8rem;
color: var(--text-primary);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.action-btn {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
background: none;
border: none;
border-radius: var(--radius-sm);
color: var(--text-secondary);
cursor: pointer;
opacity: 0;
transition:
color 120ms ease,
background 120ms ease,
opacity 120ms ease;
&:hover {
color: var(--text-primary);
background: var(--border);
}
}
/* ── States ── */
.status-row {
display: flex;
justify-content: center;
padding: 24px;
}
.status-dots {
display: flex;
gap: 5px;
align-items: center;
span {
width: 4px;
height: 4px;
border-radius: 50%;
background: var(--text-muted);
animation: dot-blink 1.2s ease-in-out infinite;
&:nth-child(2) {
animation-delay: 0.2s;
}
&:nth-child(3) {
animation-delay: 0.4s;
}
}
}
@keyframes dot-blink {
0%,
80%,
100% {
opacity: 0.25;
}
40% {
opacity: 1;
}
}
.empty-state {
display: flex;
align-items: center;
justify-content: center;
padding: 56px 24px;
}
.empty-label {
font-size: 0.8rem;
color: var(--text-muted);
letter-spacing: 0.02em;
}
.sentinel {
height: 1px;
}
/* ── Modal ── */
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.24);
backdrop-filter: blur(2px);
display: flex;
align-items: center;
justify-content: center;
z-index: 200;
}
.modal-card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius-md);
padding: 28px;
min-width: 340px;
box-shadow: 0 4px 32px rgba(0, 0, 0, 0.06);
}
.modal-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
}
.modal-title {
margin: 0;
font-size: 0.88rem;
font-weight: 500;
color: var(--text-primary);
letter-spacing: -0.01em;
}
.modal-close {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
background: none;
border: none;
border-radius: var(--radius-sm);
color: var(--text-secondary);
cursor: pointer;
transition:
color 120ms ease,
background 120ms ease;
&:hover {
color: var(--text-primary);
background: var(--surface-subtle);
}
}
.modal-input {
width: 100%;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: var(--radius-sm);
background: var(--surface-subtle);
font-family: var(--font-mono);
font-size: 0.875rem;
color: var(--text-primary);
outline: none;
box-sizing: border-box;
transition:
border-color 150ms ease,
background 150ms ease;
&::placeholder {
color: var(--text-muted);
font-family: var(--font-sans);
}
&:focus {
border-color: var(--text-primary);
background: var(--surface);
}
}
.modal-footer {
display: flex;
gap: 8px;
justify-content: flex-end;
margin-top: 20px;
}
/* Modal enter/leave transition */
.modal-enter-active {
transition: opacity 180ms ease;
.modal-card {
transition:
transform 220ms cubic-bezier(0.16, 1, 0.3, 1),
opacity 180ms ease;
}
}
.modal-leave-active {
transition: opacity 150ms ease;
.modal-card {
transition:
transform 150ms ease,
opacity 150ms ease;
}
}
.modal-enter-from,
.modal-leave-to {
opacity: 0;
.modal-card {
transform: translateY(10px) scale(0.98);
opacity: 0;
}
}
</style>