-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHistoryPanel.tsx
More file actions
150 lines (144 loc) · 5.27 KB
/
Copy pathHistoryPanel.tsx
File metadata and controls
150 lines (144 loc) · 5.27 KB
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
150
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { Button, ScrollArea } from '@object-ui/components';
import { Undo2, Redo2, History as HistoryIcon, RotateCcw } from 'lucide-react';
import type { UndoRedoState } from '../hooks/useUndoRedo';
export interface HistoryPanelProps<T> {
/** The undo/redo state returned by `useUndoRedo` / `useDesignerHistory`. */
history: UndoRedoState<T>;
/**
* Render each entry's label. Receives the entry itself, its index in the
* combined `[past, current, future]` timeline, and a relative position
* (`-N` for past, `0` for current, `+N` for future).
*/
renderLabel?: (entry: T, index: number, position: number) => React.ReactNode;
/** Optional title shown at the top of the panel. */
title?: string;
/** Compact / minimal mode hides the title and action buttons. */
compact?: boolean;
className?: string;
}
/**
* Visual timeline of recent operations, paired with `useUndoRedo`.
*
* Renders the combined past + current + future stack as a vertical list. Each
* entry is clickable and jumps the underlying state via `history.jumpTo()`.
* The current entry is highlighted; future entries are dimmed to show that
* they will be re-applied if selected.
*
* Example:
* ```tsx
* const history = useDesignerHistory(initialDraft, { persistKey: 'designer' });
* <HistoryPanel
* history={history}
* renderLabel={(draft, idx) => `Step ${idx + 1}: ${draft.lastAction ?? 'edit'}`}
* />
* ```
*/
export function HistoryPanel<T>({
history,
renderLabel,
title = 'History',
compact = false,
className,
}: HistoryPanelProps<T>) {
const { timeline, currentIndex, undo, redo, jumpTo, canUndo, canRedo } = history;
const defaultRenderLabel = React.useCallback(
(_entry: T, _index: number, position: number) => {
if (position === 0) return 'Current';
if (position < 0) return `Earlier (${Math.abs(position)} step${Math.abs(position) > 1 ? 's' : ''} back)`;
return `Later (${position} step${position > 1 ? 's' : ''} forward)`;
},
[],
);
const labelFn = renderLabel ?? defaultRenderLabel;
return (
<div
className={['flex flex-col h-full min-h-0 border rounded-md bg-card', className]
.filter(Boolean)
.join(' ')}
data-testid="history-panel"
>
{!compact && (
<div className="flex items-center justify-between gap-2 px-3 py-2 border-b">
<div className="flex items-center gap-2 text-sm font-medium text-foreground">
<HistoryIcon className="size-4" aria-hidden="true" />
{title}
<span className="text-xs text-muted-foreground tabular-nums">
({timeline.length})
</span>
</div>
<div className="flex items-center gap-1">
<Button
type="button"
variant="ghost"
size="sm"
onClick={undo}
disabled={!canUndo}
aria-label="Undo"
data-testid="history-panel-undo"
>
<Undo2 className="size-4" />
</Button>
<Button
type="button"
variant="ghost"
size="sm"
onClick={redo}
disabled={!canRedo}
aria-label="Redo"
data-testid="history-panel-redo"
>
<Redo2 className="size-4" />
</Button>
</div>
</div>
)}
<ScrollArea className="flex-1 min-h-0">
<ol className="p-1.5 space-y-0.5">
{timeline.map((entry, idx) => {
const position = idx - currentIndex;
const isCurrent = idx === currentIndex;
const isFuture = idx > currentIndex;
return (
<li key={idx}>
<button
type="button"
onClick={() => jumpTo(idx)}
data-testid={`history-panel-entry-${idx}`}
data-current={isCurrent || undefined}
className={[
'group w-full flex items-center gap-2 px-2 py-1.5 text-left text-xs rounded transition-colors',
isCurrent
? 'bg-primary/10 text-primary font-medium'
: 'hover:bg-muted text-muted-foreground hover:text-foreground',
isFuture ? 'opacity-60' : '',
].filter(Boolean).join(' ')}
aria-current={isCurrent ? 'step' : undefined}
>
<span
className={[
'inline-block size-1.5 rounded-full shrink-0',
isCurrent ? 'bg-primary' : 'bg-muted-foreground/40',
].join(' ')}
aria-hidden="true"
/>
<span className="flex-1 truncate">{labelFn(entry, idx, position)}</span>
{isCurrent && (
<RotateCcw className="size-3 opacity-0 group-hover:opacity-100 transition-opacity" aria-hidden="true" />
)}
</button>
</li>
);
})}
</ol>
</ScrollArea>
</div>
);
}