From dc362a54025ac7a5e9fd487869d93cb216f6aa3b Mon Sep 17 00:00:00 2001 From: HugoRCD Date: Sun, 31 Mar 2024 17:51:14 +0200 Subject: [PATCH] feat: major performance upgrade --- apps/app/components/project/Create.vue | 9 +++++---- apps/app/composables/useProjects.ts | 18 +++++++++++++++++ .../middleware/set-current-project.global.ts | 13 ++++++++++++ .../pages/app/project/[projectId]/index.vue | 20 +++++++++++-------- .../project/[projectId]/index/settings.vue | 14 ++++++------- apps/app/pages/app/projects.vue | 3 ++- apps/app/plugins/analytics.client.ts | 10 +++++++++- apps/app/server/api/project/[id]/index.put.ts | 6 +----- 8 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 apps/app/middleware/set-current-project.global.ts diff --git a/apps/app/components/project/Create.vue b/apps/app/components/project/Create.vue index d02bc6a8..56800e12 100644 --- a/apps/app/components/project/Create.vue +++ b/apps/app/components/project/Create.vue @@ -36,12 +36,13 @@ function removeTeam() { } const { - fetchTeams, - loading + teams, + loading, + fetchTeams } = useTeams() -fetchTeams() -const teams = useUserTeams() +if (!teams.value) + fetchTeams() function importProject() { const input = document.createElement('input') diff --git a/apps/app/composables/useProjects.ts b/apps/app/composables/useProjects.ts index 4e110f21..f1e6a965 100644 --- a/apps/app/composables/useProjects.ts +++ b/apps/app/composables/useProjects.ts @@ -4,9 +4,16 @@ export const useUserProjects = () => { return useState('projects') } +export const useCurrentProject = () => { + return useState('currentProject') +} + export function useProjects() { const projects = useUserProjects() + const currentProject = useCurrentProject() + const loading = ref(false) + const currentLoading = ref(false) async function fetchProjects() { loading.value = true @@ -16,6 +23,14 @@ export function useProjects() { loading.value = false } + async function fetchCurrentProject(projectId: number) { + currentLoading.value = true + currentProject.value = await $fetch(`/api/project/${projectId}`, { + method: 'GET', + }) + currentLoading.value = false + } + async function createProject(createProjectInput: CreateProjectInput) { try { const response = await $fetch('/api/project', { @@ -57,8 +72,11 @@ export function useProjects() { return { projects, + currentProject, loading, + currentLoading, fetchProjects, + fetchCurrentProject, createProject, updateProject, deleteProject, diff --git a/apps/app/middleware/set-current-project.global.ts b/apps/app/middleware/set-current-project.global.ts new file mode 100644 index 00000000..05a4f7c3 --- /dev/null +++ b/apps/app/middleware/set-current-project.global.ts @@ -0,0 +1,13 @@ +import { useCurrentProject } from '~/composables/useProjects' + +export default defineNuxtRouteMiddleware(((to) => { + const userProjects = useUserProjects() + const currentProject = useCurrentProject() + const projectId = to.params.projectId || '' + if (to.path === `/app/project/${projectId}`) { + const project = userProjects.value.find((project) => project.id === parseInt(projectId as string)) + if (project) { + currentProject.value = project + } + } +})) diff --git a/apps/app/pages/app/project/[projectId]/index.vue b/apps/app/pages/app/project/[projectId]/index.vue index ad1a606e..b741aff9 100644 --- a/apps/app/pages/app/project/[projectId]/index.vue +++ b/apps/app/pages/app/project/[projectId]/index.vue @@ -5,14 +5,18 @@ definePageMeta({ const { projectId } = useRoute().params -const {data: project, status, refresh} = useFetch(`/api/project/${projectId}`, { - method: 'GET', - watch: false, -}) +const { + currentProject, + currentLoading, + fetchCurrentProject +} = useProjects() + +if (!currentProject.value) + fetchCurrentProject(+projectId) -provide('project', project) -provide('status', status) -provide('refresh', refresh) +provide('project', currentProject) +provide('loading', currentLoading) +provide('refresh', fetchCurrentProject) const links = [ { @@ -40,7 +44,7 @@ const links = [