Home

dev / openkara

publicthedavidweng/OpenKara· sync paused
Overview Code History Branches Pull requestsIssuesInsights
main
HomeOverview Code PRsIssues
dev/openkara/src/components/Layout/Sidebar.tsx
Raw
1import { useEffect, useState, type ReactNode } from "react";2import { useTranslation } from "react-i18next";3import {4  ArrowLeft,5  Folder,6  CheckCircle2,7  Layers,8  ListMusic,9
} from "lucide-react";
10import { ConfirmationDialog } from "@/components/Settings/ConfirmationDialog";
11import { InputDialog } from "@/components/Settings/InputDialog";
12import { SearchBox } from "@/components/Library/SearchBox";
13import { SongList } from "@/components/Library/SongList";
14import { SortModeSelector } from "@/components/Library/SortModeSelector";
15import { songCanBeSeparated } from "@/lib/song-media";
16import { useLibraryStore } from "@/stores/library-store";
17import { usePlaylistStore } from "@/stores/playlist-store";
18import { useSettingsStore } from "@/stores/settings-store";
19import * as api from "@/lib/tauri";
20import { notifyError } from "@/lib/errors";
21 
22interface SidebarProps {
23 header?: ReactNode;
24 previewMode?: boolean;
25}
26 
27const batchActionClassName =
28 "rounded-[12px] border border-[var(--sidebar-control-border)] bg-[var(--sidebar-control-bg)] px-3 py-2 text-[13px] hover:border-[var(--sidebar-control-border)] hover:bg-[var(--sidebar-row-overlay-bg)]";
29 
30export function Sidebar({ header, previewMode = false }: SidebarProps = {}) {
31 const { t } = useTranslation();
32 const songs = useLibraryStore((s) => s.songs);
33 const filter = useLibraryStore((s) => s.filter);
34 const setFilter = useLibraryStore((s) => s.setFilter);
35 const separationStatuses = useLibraryStore((s) => s.separationStatuses);
36 const batchSeparation = useLibraryStore((s) => s.batchSeparation);
37 
38 const hideBatchSeparate = useSettingsStore((s) => s.hideBatchSeparate);
39 const hideUpgradeAll = useSettingsStore((s) => s.hideUpgradeAll);
40 const stemMode = useSettingsStore((s) => s.stemMode);
41 const [showUpgradeConfirm, setShowUpgradeConfirm] = useState(false);
42 const [showCreatePlaylist, setShowCreatePlaylist] = useState(false);
43 
44 const playlists = usePlaylistStore((s) => s.playlists);
45 const activePlaylistId = usePlaylistStore((s) => s.activePlaylistId);
46 const activePlaylist = playlists.find((p) => p.id === activePlaylistId);
47 const loadPlaylists = usePlaylistStore((s) => s.loadPlaylists);
48 const createPlaylist = usePlaylistStore((s) => s.createPlaylist);
49 const setActivePlaylist = usePlaylistStore((s) => s.setActivePlaylist);
50 
51 useEffect(() => {
52 if (previewMode) {
53 return;
54 }
55 loadPlaylists();
56 }, [loadPlaylists, previewMode]);
57 
58 const handleCreatePlaylist = async (name: string) => {
59 try {
60 await createPlaylist(name.trim());
61 } catch {
62 notifyError(new Error("Failed to create playlist"));
63 }
64 };
65 const separableSongs = songs.filter(songCanBeSeparated);
66 
67 const separatedCount = songs.filter(
68 (s) => separationStatuses[s.hash]?.state === "completed",
69 ).length;
70 
71 // Check if all songs are separated in the current stem mode.
72 // Treat "running" as separated — a song being re-separated was previously completed.
73 const allSeparated =
74 separableSongs.length > 0 &&
75 separableSongs.every((s) => {
76 const status = separationStatuses[s.hash];
77 return status?.state === "completed" || status?.state === "running";
78 });
79 
80 const allMatchCurrentMode =
81 allSeparated &&
82 separableSongs.every((s) => {
83 const status = separationStatuses[s.hash];
84 if (!status) return false;
85 if (status.state === "running") return true;
86 if (status.state !== "completed") return false;
87 if (stemMode === "four_stem") return !!status.drums_path;
88 return true;
89 });
90 
91 const needsUpgrade =
92 allSeparated && !allMatchCurrentMode && stemMode === "four_stem";
93 
94 const showUpgradeButton = needsUpgrade && !hideUpgradeAll;
95 
96 const shouldHideButton =
97 hideBatchSeparate ||
98 separableSongs.length === 0 ||
99 (allSeparated && allMatchCurrentMode) ||
100 (needsUpgrade && hideUpgradeAll);
101 
102 const handleSeparateAll = () => {
103 if (previewMode) {
104 return;
105 }
106 api.batchSeparate([]).catch(notifyError);
107 };
108 
109 const isBatchRunning =
110 batchSeparation != null &&
111 batchSeparation.completed + batchSeparation.failed < batchSeparation.total;
112 
113 return (
114 <div
115 className="flex h-full w-[var(--window-shell-sidebar-width)] shrink-0 flex-col bg-[var(--color-sidebar)]"
116 data-window-shell-section="sidebar"
117 data-sidebar-visual-variant="unified"
118 >
119 {header ? <div className="shrink-0 px-3 pb-2 pt-3">{header}</div> : null}
120 
121 <div className={`shrink-0 px-3 pb-3 ${header ? "pt-1" : "pt-3"}`}>
122 <SearchBox />
123 </div>
124 
125 {activePlaylistId && (
126 <div className="shrink-0 flex items-center gap-2 border-b border-[color-mix(in_srgb,var(--color-border)_86%,transparent)] px-4 py-2">
127 <button
128 onClick={() => setActivePlaylist(null)}
129 className="shrink-0 text-[var(--color-text-dim)] hover:text-[var(--color-control-primary)] transition-colors"
130 aria-label={t("setup.back")}
131 data-preview-playlist-switch={previewMode ? "true" : undefined}
132 >
133 <ArrowLeft size={14} />
134 </button>
135 <span className="text-[13px] font-medium truncate text-[var(--color-control-primary)]">
136 {activePlaylist?.name}
137 </span>
138 </div>
139 )}
140 {!activePlaylistId && (
141 <div className="shrink-0 space-y-0.5 px-2">
142 <div className="px-2 pb-1 text-[11px] font-semibold tracking-wide text-[var(--color-text-dim)]">
143 {t("sidebar.library")}
144 </div>
145 <button
146 type="button"
147 aria-label={t("sidebar.allTracks")}
148 onClick={() => {
149 setFilter("all");
150 setActivePlaylist(null);
151 }}
152 className={`sidebar-source-list-row motion-surface flex w-full items-center justify-between px-2 py-1.5 text-[13px] ${
153 filter === "all" && !activePlaylistId
154 ? "border border-[var(--sidebar-row-selected-border)] bg-[var(--sidebar-row-selected-bg)] text-[var(--color-text)] shadow-[var(--shadow-control-selected)]"
155 : "border border-transparent text-[var(--color-text)] hover:bg-[var(--sidebar-row-overlay-bg)]"
156 }`}
157 >
158 <span className="flex items-center gap-2">
159 <Folder
160 size={14}
161 className="text-[var(--color-accent)]"
162 fill="currentColor"
163 fillOpacity={0.2}
164 />
165 <span>{t("sidebar.allTracks")}</span>
166 </span>
167 <span className="text-[11px] text-[var(--color-text-dim)]">
168 {songs.length}
169 </span>
170 </button>
171 <button
172 type="button"
173 aria-label={t("sidebar.separated")}
174 onClick={() => {
175 setFilter("separated");
176 setActivePlaylist(null);
177 }}
178 className={`sidebar-source-list-row motion-surface flex w-full items-center justify-between px-2 py-1.5 text-[13px] ${
179 filter === "separated" && !activePlaylistId
180 ? "border border-[var(--sidebar-row-selected-border)] bg-[var(--sidebar-row-selected-bg)] text-[var(--color-text)] shadow-[var(--shadow-control-selected)]"
181 : "border border-transparent text-[var(--color-text)] hover:bg-[var(--sidebar-row-overlay-bg)]"
182 }`}
183 >
184 <span className="flex items-center gap-2">
185 <CheckCircle2 size={14} className="text-[var(--color-accent)]" />
186 <span>{t("sidebar.separated")}</span>
187 </span>
188 <span className="text-[11px] text-[var(--color-text-dim)]">
189 {separatedCount}
190 </span>
191 </button>
192 </div>
193 )}
194 
195 <div
196 className={`shrink-0 space-y-0.5 px-2 ${previewMode ? "mt-3" : "mt-4"}`}
197 >
198 <div className="flex items-center justify-between px-2 pb-1">
199 <span className="text-[11px] font-semibold tracking-wide text-[var(--color-text-dim)]">
200 {t("playlist.section")}
201 </span>
202 <button
203 onClick={() => setShowCreatePlaylist(true)}
204 className="text-[11px] text-[var(--color-accent)] hover:text-[var(--color-accent-hover)] transition-colors"
205 title={t("playlist.create")}
206 >
207 + {t("playlist.create")}
208 </button>
209 </div>
210 {playlists.length === 0 ? (
211 <div className="px-2 py-1 text-[11px] text-[var(--color-text-dim)]">
212 {t("playlist.empty")}
213 </div>
214 ) : (
215 <div className="space-y-0.5">
216 {playlists.map((playlist) => (
217 <button
218 key={playlist.id}
219 onClick={() => setActivePlaylist(playlist.id)}
220 data-preview-playlist-switch={previewMode ? "true" : undefined}
221 aria-current={
222 activePlaylistId === playlist.id ? "page" : undefined
223 }
224 className={`sidebar-source-list-row motion-surface flex w-full items-center justify-between rounded-lg px-2 py-1.5 text-[13px] ${
225 activePlaylistId === playlist.id
226 ? "border border-[var(--sidebar-row-selected-border)] bg-[var(--sidebar-row-selected-bg)] text-[var(--color-text)] shadow-[var(--shadow-control-selected)]"
227 : "border border-transparent text-[var(--color-text)] hover:bg-[var(--sidebar-row-overlay-bg)]"
228 }`}
229 >
230 <span className="flex items-center gap-2 truncate">
231 <ListMusic
232 size={14}
233 className="text-[var(--color-text-dim)] shrink-0"
234 />
235 <span className="truncate">{playlist.name}</span>
236 </span>
237 <span className="text-[11px] text-[var(--color-text-dim)] shrink-0 ml-2">
238 {playlist.song_count}
239 </span>
240 </button>
241 ))}
242 </div>
243 )}
244 </div>
245 
246 {!activePlaylistId && (
247 <div className="mt-4 flex flex-1 flex-col overflow-hidden px-2">
248 <div className="flex items-center justify-between px-2 pb-1">
249 <span className="text-[11px] font-semibold tracking-wide text-[var(--color-text-dim)]">
250 {t("sidebar.localMusic")}
251 </span>
252 <SortModeSelector />
253 </div>
254 <SongList />
255 </div>
256 )}
257 {activePlaylistId && (
258 <div className="mt-4 flex flex-1 flex-col overflow-hidden px-2">
259 <SongList />
260 </div>
261 )}
262 
263 {!(shouldHideButton && !isBatchRunning && batchSeparation == null) && (
264 <div className="shrink-0 border-t border-[var(--color-border)] px-3 py-3">
265 {isBatchRunning ? (
266 <div className="text-center text-[11px] text-[var(--color-text-dim)]">
267 {t("sidebar.separating", {
268 current: Math.min(
269 batchSeparation.completed + 1,
270 batchSeparation.total,
271 ),
272 total: batchSeparation.total,
273 })}
274 </div>
275 ) : batchSeparation != null ? (
276 <div className="text-center text-[11px] text-[var(--color-text-dim)]">
277 {t("sidebar.separationComplete", {
278 done: batchSeparation.completed,
279 })}
280 {batchSeparation.skipped > 0 &&
281 `, ${t("sidebar.skipped", { count: batchSeparation.skipped })}`}
282 {batchSeparation.failed > 0 &&
283 `, ${t("sidebar.failed", { count: batchSeparation.failed })}`}
284 </div>
285 ) : showUpgradeButton ? (
286 <button
287 onClick={() => setShowUpgradeConfirm(true)}
288 className={`motion-surface flex w-full items-center justify-center gap-2 ${batchActionClassName} text-[var(--color-text)] hover:text-[var(--color-text)]`}
289 >
290 <Layers size={12} />
291 {t("sidebar.upgradeAll")}
292 </button>
293 ) : (
294 <button
295 onClick={handleSeparateAll}
296 disabled={separableSongs.length === 0}
297 className={`motion-surface flex w-full items-center justify-center gap-2 ${batchActionClassName} text-[var(--color-text)] hover:text-[var(--color-text)] disabled:opacity-40`}
298 >
299 <Layers size={12} />
300 {t("sidebar.separateAll")}
301 <span className="text-[10px] text-[var(--color-text-dimmer)]">
302 (
303 {stemMode === "four_stem"
304 ? t("sidebar.fourStem")
305 : t("sidebar.twoStem")}
306 )
307 </span>
308 </button>
309 )}
310 </div>
311 )}
312 
313 {!previewMode && showCreatePlaylist && (
314 <InputDialog
315 title={t("playlist.create")}
316 placeholder={t("playlist.name")}
317 confirmLabel={t("common.save")}
318 onConfirm={(name) => {
319 setShowCreatePlaylist(false);
320 handleCreatePlaylist(name);
321 }}
322 onCancel={() => setShowCreatePlaylist(false)}
323 />
324 )}
325 
326 {!previewMode && showUpgradeConfirm && (
327 <ConfirmationDialog
328 title={t("sidebar.confirmUpgrade.title")}
329 message={t("sidebar.confirmUpgrade.message")}
330 confirmLabel={t("sidebar.confirmUpgrade.confirm")}
331 onConfirm={() => {
332 setShowUpgradeConfirm(false);
333 if (!previewMode) {
334 api.batchSeparate([]).catch(notifyError);
335 }
336 }}
337 onCancel={() => setShowUpgradeConfirm(false)}
338 />
339 )}
340 </div>
341 );
342}
343