Files
LinkDesk/frontend/src/components/layout/ProjectSwitcher.vue
T
indigo 4f9deeb57a Phase 4: Architecture & tooling investment
Fixes a real N+1 API-call bug (per-cell project-member fetches in shot/asset
task-status cells, now a shared cached store), replaces a polling-based
in-flight request de-dup with promise memoization, adds markRaw() around
icon components in reactive state, and fixes a deep watcher that re-scanned
the whole projects array just to trigger thumbnail loading.

Introduces useAsyncAction and usePermission composables to cut duplicated
store boilerplate and duplicated role/admin checks, applied only where the
existing code was a clean match rather than forced onto everything. Extracts
assets.ts's shared per-asset optimistic-update/rollback helper without
merging the single and bulk API paths, which hit genuinely different
endpoints. Adds toast-on-error for previously-silent secondary loads and
fixes a dead try/catch in the router's auth-init guard along the way.

Also fixes a thumbnail-loading regression introduced earlier in this same
pass: the new ID-keyed watcher never fired on a repeat visit to the Projects
page when the store already held the same project list, so thumbnails
(local component state, reset per mount) silently stopped loading after the
first visit. Replaced the watcher with a direct call after each fetch.

Table virtualization, splitting the two largest view files, broad store
caching, and moving domain types into types/ were scoped out as separate,
higher-risk follow-ups (documented in frontend_tasks.md).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-18 04:03:58 +08:00

231 lines
7.8 KiB
Vue

<template>
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger as-child>
<SidebarMenuButton
size="lg"
class="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
:disabled="isLoading"
>
<!-- Loading State -->
<div
v-if="isLoading"
class="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground"
>
<div class="animate-spin rounded-full h-4 w-4 border-b-2 border-current"></div>
</div>
<!-- Error State -->
<div
v-else-if="error"
class="flex aspect-square size-8 items-center justify-center rounded-lg bg-destructive text-destructive-foreground"
>
<AlertCircle class="size-4" />
</div>
<!-- Normal State -->
<div
v-else
class="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground"
>
<component :is="activeProject.icon" class="size-4" />
</div>
<div class="grid flex-1 text-left text-sm leading-tight">
<span class="truncate font-semibold">
{{ isLoading ? 'Loading...' : error ? 'Error' : activeProject.name }}
</span>
<span class="truncate text-xs">
{{ isLoading ? 'Fetching projects' : error ? 'Failed to load' : activeProject.status }}
</span>
</div>
<ChevronsUpDown class="ml-auto" />
</SidebarMenuButton>
</DropdownMenuTrigger>
<DropdownMenuContent
class="w-[--reka-dropdown-menu-trigger-width] min-w-56 rounded-lg"
align="start"
:side="isMobile ? 'bottom' : 'right'"
:side-offset="4"
>
<DropdownMenuLabel class="text-xs text-muted-foreground">
Projects
</DropdownMenuLabel>
<!-- Loading State in Dropdown -->
<DropdownMenuItem v-if="isLoading" class="gap-2 p-2" disabled>
<div class="animate-spin rounded-full h-4 w-4 border-b-2 border-current"></div>
<span class="text-muted-foreground">Loading projects...</span>
</DropdownMenuItem>
<!-- Error State in Dropdown -->
<template v-else-if="error">
<DropdownMenuItem class="gap-2 p-2" disabled>
<AlertCircle class="size-4 text-destructive" />
<span class="text-destructive text-sm">{{ error }}</span>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem class="gap-2 p-2" @click="refreshProjects">
<RefreshCw class="size-4" />
<span>Retry</span>
</DropdownMenuItem>
</template>
<!-- Projects List -->
<template v-else>
<DropdownMenuItem
v-for="(project, index) in projects"
:key="project.id"
class="gap-2 p-2"
@click="setActiveProject(project)"
>
<div
class="flex size-6 items-center justify-center rounded-sm border"
>
<component :is="project.icon" class="size-4 shrink-0" />
</div>
<div class="flex flex-col">
<span class="font-medium">{{ project.name }}</span>
<span class="text-xs text-muted-foreground">{{
project.status
}}</span>
</div>
<DropdownMenuShortcut>{{ index + 1 }}</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem class="gap-2 p-2" @click="refreshProjects">
<RefreshCw class="size-4" />
<div class="font-medium text-muted-foreground">Refresh projects</div>
</DropdownMenuItem>
<DropdownMenuItem
v-if="canCreateProjects"
class="gap-2 p-2"
@click="handleCreateProject"
>
<div
class="flex size-6 items-center justify-center rounded-md border bg-background"
>
<Plus class="size-4" />
</div>
<div class="font-medium text-muted-foreground">Create project</div>
</DropdownMenuItem>
</template>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
</template>
<script setup lang="ts">
import { computed, watch, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ChevronsUpDown, Plus, AlertCircle, RefreshCw } from "lucide-vue-next";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from "@/components/ui/sidebar";
import { useAuthStore } from "@/stores/auth";
import { useProjectsStore } from "@/stores/projects";
import { usePermission } from "@/composables/usePermission";
const router = useRouter();
const { isMobile } = useSidebar();
const authStore = useAuthStore();
const projectsStore = useProjectsStore();
const { isCoordinatorOrAdmin } = usePermission();
// Get projects and active project from store
const projects = computed(() => projectsStore.availableProjects);
const activeProject = computed(() => projectsStore.currentProject);
const isLoading = computed(() => projectsStore.isLoading);
const error = computed(() => projectsStore.error);
// Check if user can create projects
const canCreateProjects = computed(() => isCoordinatorOrAdmin.value);
const setActiveProject = (project: any) => {
projectsStore.setActiveProject(project);
// Navigate to project-specific view
if (project.id === 0) {
// "All Projects" view
router.push("/projects");
} else {
// Specific project view - navigate to overview tab
router.push(`/projects/${project.id}`);
}
};
const handleCreateProject = () => {
if (canCreateProjects.value) {
router.push("/projects/new");
}
};
const refreshProjects = async () => {
try {
await projectsStore.fetchProjects();
} catch (error) {
console.error('Failed to refresh projects:', error);
}
};
// Set active project based on current route
const updateActiveProjectFromRoute = () => {
const currentPath = router.currentRoute.value.path;
if (currentPath.startsWith("/projects/")) {
const pathSegments = currentPath.split("/");
if (pathSegments[2] === "new") {
// Creating new project, keep current active project
return;
}
const projectId = parseInt(pathSegments[2]);
if (!isNaN(projectId)) {
const project = projectsStore.getProjectById(projectId);
if (project) {
projectsStore.setActiveProject(project);
}
}
} else if (currentPath === "/projects") {
projectsStore.setActiveProject(projectsStore.allProjectsView);
}
};
// Watch for route changes
watch(
() => router.currentRoute.value.path,
() => {
updateActiveProjectFromRoute();
},
{ immediate: true }
);
// Watch for authentication changes to fetch projects
watch(
() => authStore.isAuthenticated,
(isAuthenticated) => {
if (isAuthenticated && projectsStore.projects.length === 0 && !projectsStore.isLoading) {
refreshProjects();
}
},
{ immediate: true }
);
// Ensure projects are loaded on component mount
onMounted(() => {
if (authStore.isAuthenticated && projectsStore.projects.length === 0 && !projectsStore.isLoading) {
refreshProjects();
}
});
</script>