mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-03 08:53:36 +00:00
feat(sidebar): implement trash functionality for project management
- Added a new `trashItem` method in the Electron API to move projects to the system trash. - Enhanced the sidebar component to include a trash button and manage trashed projects. - Implemented functionality to restore and permanently delete projects from the trash. - Updated the application state to track trashed projects and provide user feedback through toast notifications. This update significantly improves project management by allowing users to easily manage deleted projects without permanent loss.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
import type { Project } from "@/lib/electron";
|
||||
import type { Project, TrashedProject } from "@/lib/electron";
|
||||
|
||||
export type ViewMode =
|
||||
| "welcome"
|
||||
@@ -92,6 +92,7 @@ export interface AppState {
|
||||
// Project state
|
||||
projects: Project[];
|
||||
currentProject: Project | null;
|
||||
trashedProjects: TrashedProject[];
|
||||
|
||||
// View state
|
||||
currentView: ViewMode;
|
||||
@@ -154,6 +155,10 @@ export interface AppActions {
|
||||
setProjects: (projects: Project[]) => void;
|
||||
addProject: (project: Project) => void;
|
||||
removeProject: (projectId: string) => void;
|
||||
moveProjectToTrash: (projectId: string) => void;
|
||||
restoreTrashedProject: (projectId: string) => void;
|
||||
deleteTrashedProject: (projectId: string) => void;
|
||||
emptyTrash: () => void;
|
||||
setCurrentProject: (project: Project | null) => void;
|
||||
reorderProjects: (oldIndex: number, newIndex: number) => void;
|
||||
|
||||
@@ -216,6 +221,7 @@ export interface AppActions {
|
||||
const initialState: AppState = {
|
||||
projects: [],
|
||||
currentProject: null,
|
||||
trashedProjects: [],
|
||||
currentView: "welcome",
|
||||
sidebarOpen: true,
|
||||
theme: "dark",
|
||||
@@ -269,6 +275,82 @@ export const useAppStore = create<AppState & AppActions>()(
|
||||
set({ projects: get().projects.filter((p) => p.id !== projectId) });
|
||||
},
|
||||
|
||||
moveProjectToTrash: (projectId) => {
|
||||
const project = get().projects.find((p) => p.id === projectId);
|
||||
if (!project) return;
|
||||
|
||||
const remainingProjects = get().projects.filter(
|
||||
(p) => p.id !== projectId
|
||||
);
|
||||
const existingTrash = get().trashedProjects.filter(
|
||||
(p) => p.id !== projectId
|
||||
);
|
||||
const trashedProject: TrashedProject = {
|
||||
...project,
|
||||
trashedAt: new Date().toISOString(),
|
||||
deletedFromDisk: false,
|
||||
};
|
||||
|
||||
const isCurrent = get().currentProject?.id === projectId;
|
||||
|
||||
set({
|
||||
projects: remainingProjects,
|
||||
trashedProjects: [trashedProject, ...existingTrash],
|
||||
currentProject: isCurrent ? null : get().currentProject,
|
||||
currentView: isCurrent ? "welcome" : get().currentView,
|
||||
});
|
||||
},
|
||||
|
||||
restoreTrashedProject: (projectId) => {
|
||||
const trashed = get().trashedProjects.find((p) => p.id === projectId);
|
||||
if (!trashed) return;
|
||||
|
||||
const remainingTrash = get().trashedProjects.filter(
|
||||
(p) => p.id !== projectId
|
||||
);
|
||||
const existingProjects = get().projects;
|
||||
const samePathProject = existingProjects.find(
|
||||
(p) => p.path === trashed.path
|
||||
);
|
||||
const projectsWithoutId = existingProjects.filter(
|
||||
(p) => p.id !== projectId
|
||||
);
|
||||
|
||||
// If a project with the same path already exists, keep it and just remove from trash
|
||||
if (samePathProject) {
|
||||
set({
|
||||
trashedProjects: remainingTrash,
|
||||
currentProject: samePathProject,
|
||||
currentView: "board",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const restoredProject: Project = {
|
||||
id: trashed.id,
|
||||
name: trashed.name,
|
||||
path: trashed.path,
|
||||
lastOpened: new Date().toISOString(),
|
||||
};
|
||||
|
||||
set({
|
||||
trashedProjects: remainingTrash,
|
||||
projects: [...projectsWithoutId, restoredProject],
|
||||
currentProject: restoredProject,
|
||||
currentView: "board",
|
||||
});
|
||||
},
|
||||
|
||||
deleteTrashedProject: (projectId) => {
|
||||
set({
|
||||
trashedProjects: get().trashedProjects.filter(
|
||||
(p) => p.id !== projectId
|
||||
),
|
||||
});
|
||||
},
|
||||
|
||||
emptyTrash: () => set({ trashedProjects: [] }),
|
||||
|
||||
reorderProjects: (oldIndex, newIndex) => {
|
||||
const projects = [...get().projects];
|
||||
const [movedProject] = projects.splice(oldIndex, 1);
|
||||
@@ -495,6 +577,7 @@ export const useAppStore = create<AppState & AppActions>()(
|
||||
partialize: (state) => ({
|
||||
projects: state.projects,
|
||||
currentProject: state.currentProject,
|
||||
trashedProjects: state.trashedProjects,
|
||||
currentView: state.currentView,
|
||||
theme: state.theme,
|
||||
sidebarOpen: state.sidebarOpen,
|
||||
|
||||
Reference in New Issue
Block a user