-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathStoryboardSidebar.tsx
More file actions
218 lines (204 loc) · 6.65 KB
/
Copy pathStoryboardSidebar.tsx
File metadata and controls
218 lines (204 loc) · 6.65 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/** @jsxImportSource @emotion/react */
/**
* StoryboardSidebar
*
* Lists the user's server-persisted storyboards, newest first. Clicking a
* board opens/focuses its workspace tab; hovering exposes a delete. Boards
* autosave (see useStoryboardServerSync), so this doubles as the
* "recent work" view.
*/
import React, { memo, useCallback, useMemo, useState } from "react";
import { css } from "@emotion/react";
import { useTheme } from "@mui/material/styles";
import type { Theme } from "@mui/material/styles";
import AddRoundedIcon from "@mui/icons-material/AddRounded";
import DeleteOutlineRoundedIcon from "@mui/icons-material/DeleteOutlineRounded";
import {
FlexColumn,
FlexRow,
Text,
Caption,
Dialog,
ToolbarIconButton,
LoadingSpinner,
SPACING,
getSpacingPx,
BORDER_RADIUS,
MOTION
} from "../ui_primitives";
import {
useStoryboards,
useCreateStoryboard,
useDeleteStoryboard
} from "../../hooks/storyboard/useStoryboards";
import { useStoryboardStore } from "../../stores/storyboard/StoryboardStore";
import { tabId, useWorkspaceTabsStore } from "../../stores/WorkspaceTabsStore";
interface StoryboardSidebarProps {
/** Board shown in the surface this sidebar belongs to. */
activeBoardId: string;
}
const styles = (theme: Theme) =>
css({
width: "220px",
flexShrink: 0,
height: "100%",
overflowY: "auto",
borderRight: `1px solid ${theme.vars.palette.divider}`,
// On phones the sidebar is a full-width pane behind a segmented switcher
// (see StoryboardSurface), not a fixed rail beside the board.
[theme.breakpoints.down("sm")]: {
width: "100%",
borderRight: "none"
},
padding: getSpacingPx(SPACING.md),
display: "flex",
flexDirection: "column",
gap: getSpacingPx(SPACING.md),
".sidebar-title": {
textTransform: "uppercase",
letterSpacing: "0.08em",
color: theme.vars.palette.text.disabled
},
".board-row": {
padding: `${getSpacingPx(SPACING.sm)} ${getSpacingPx(SPACING.md)}`,
borderRadius: BORDER_RADIUS.md,
cursor: "pointer",
"&:hover": { backgroundColor: theme.vars.palette.action.hover },
"&.active": { backgroundColor: theme.vars.palette.action.selected },
".delete-button": { opacity: 0, transition: MOTION.opacity },
"&:hover .delete-button": { opacity: 1 },
// Touch devices have no hover; keep the delete affordance reachable.
"@media (pointer: coarse)": {
".delete-button": { opacity: 1 }
}
}
});
const StoryboardSidebarInner: React.FC<StoryboardSidebarProps> = ({
activeBoardId
}) => {
const theme = useTheme();
const { data: boards, isLoading } = useStoryboards();
const createStoryboard = useCreateStoryboard();
const deleteStoryboard = useDeleteStoryboard();
const removeLocalBoard = useStoryboardStore((state) => state.removeBoard);
const openTab = useWorkspaceTabsStore((state) => state.openTab);
const closeTab = useWorkspaceTabsStore((state) => state.closeTab);
const [pendingDelete, setPendingDelete] = useState<{
id: string;
name: string;
} | null>(null);
const openBoard = useCallback(
(id: string, title: string) => {
openTab({
type: "storyboard",
ref: id,
mode: "edit",
title: title || "Untitled storyboard"
});
},
[openTab]
);
const newBoard = useCallback(async () => {
try {
const created = await createStoryboard.mutateAsync({
name: "Untitled storyboard",
projectId: "default"
});
openBoard(created.id, created.name);
} catch (error) {
console.error("Failed to create storyboard", error);
}
}, [createStoryboard, openBoard]);
const confirmDelete = useCallback(() => {
if (!pendingDelete) {
return;
}
const { id } = pendingDelete;
deleteStoryboard.mutate({ id });
removeLocalBoard(id);
closeTab(tabId("storyboard", id));
setPendingDelete(null);
}, [pendingDelete, deleteStoryboard, removeLocalBoard, closeTab]);
const formatter = useMemo(
() =>
new Intl.DateTimeFormat(undefined, {
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit"
}),
[]
);
return (
<div css={styles(theme)} className="storyboard-sidebar">
<FlexRow align="center" justify="space-between">
<Text size="smaller" className="sidebar-title">
Storyboards
</Text>
<ToolbarIconButton
icon={<AddRoundedIcon fontSize="small" />}
tooltip="New storyboard"
onClick={() => void newBoard()}
/>
</FlexRow>
{isLoading ? (
<LoadingSpinner size={20} />
) : (
<FlexColumn gap={0.5}>
{(boards ?? []).map((board) => (
<FlexRow
key={board.id}
align="center"
justify="space-between"
gap={1}
className={`board-row${board.id === activeBoardId ? " active" : ""}`}
onClick={() => openBoard(board.id, board.name)}
>
<FlexColumn gap={0} sx={{ minWidth: 0 }}>
<Text size="small" truncate>
{board.name || "Untitled storyboard"}
</Text>
<Caption size="smaller" color="secondary">
{board.shotCount > 0
? `${board.shotCount} shots · ${formatter.format(new Date(board.updatedAt))}`
: formatter.format(new Date(board.updatedAt))}
</Caption>
</FlexColumn>
<ToolbarIconButton
className="delete-button"
icon={<DeleteOutlineRoundedIcon fontSize="small" />}
tooltip="Delete storyboard"
onClick={(e) => {
e.stopPropagation();
setPendingDelete({
id: board.id,
name: board.name || "Untitled storyboard"
});
}}
/>
</FlexRow>
))}
{(boards ?? []).length === 0 && (
<Caption size="smaller" color="secondary">
No storyboards yet.
</Caption>
)}
</FlexColumn>
)}
<Dialog
open={pendingDelete !== null}
onClose={() => setPendingDelete(null)}
title="Delete storyboard?"
onConfirm={confirmDelete}
confirmText="Delete"
destructive
>
<Text size="small">
{`Delete “${pendingDelete?.name ?? ""}” and its shots. Generated stills and clips stay in your asset library.`}
</Text>
</Dialog>
</div>
);
};
const StoryboardSidebar = memo(StoryboardSidebarInner);
export default StoryboardSidebar;