get current project id and folder id

This commit is contained in:
Takeshi Kimata
2024-02-19 20:36:27 +09:00
parent 31c74ae769
commit 60d5b6f031
4 changed files with 50 additions and 7 deletions

View File

@@ -0,0 +1,41 @@
'use client'
import { usePathname } from 'next/navigation'
import { useState, useEffect } from "react";
type ProjectFolderIds = {
projectId: number | null;
folderId: number | null;
};
/**
* Custom hook that extracts projectId and folderId from the current path.
* Example: For the path '/projects/1/folders/3/cases', projectId would be 1 and folderId would be 3.
*/
const useGetCurrentIds = (): ProjectFolderIds => {
const pathname = usePathname()
const [projectId, setProjectId] = useState<number | null>(null);
const [folderId, setFolderId] = useState<number | null>(null);
useEffect(() => {
const currentPath = pathname;
const pathSegments = currentPath.split("/").filter(Boolean);
const projectIdIndex = pathSegments.indexOf("projects") + 1;
const folderIdIndex = pathSegments.indexOf("folders") + 1;
const newProjectId =
projectIdIndex !== -1 ? parseInt(pathSegments[projectIdIndex], 10) : null;
const newFolderId =
folderIdIndex !== -1 ? parseInt(pathSegments[folderIdIndex], 10) : null;
setProjectId(newProjectId);
setFolderId(newFolderId);
}, [pathname]);
return {
projectId,
folderId,
};
};
export default useGetCurrentIds;