'use client' import React, { Fragment, useEffect, useState } from 'react' import { v4 as uuidv4 } from 'uuid' import { Menu, Transition } from '@headlessui/react' import { BellIcon } from '@heroicons/react/24/outline' import { MagnifyingGlassIcon } from '@heroicons/react/20/solid' import { GetDocuments } from '../../wailsjs/wailsjs/go/ipc/Channel' import { LogPrint } from '../../wailsjs/wailsjs/runtime/runtime' import { ipc } from '../../wailsjs/wailsjs/go/models' type NavigationItem = { id: string, name: string, children: { id: string, name: string }[] } // const navigation: NavigationItem[] = [ // { // id: uuidv4(), // name: 'Chapter One', // children: [ // { name: 'Overview', id: uuidv4() }, // { name: 'Members', id: uuidv4() }, // { name: 'Calendar', id: uuidv4() }, // { name: 'Settings', id: uuidv4() }, // ], // }, // ] const userNavigation = [ { name: 'Your Profile' }, { name: 'Settings' }, { name: 'Sign out' }, ] const getNavigationProps = ( documents: ipc.Document[], documentGroups: ipc.DocumentGroup[]): NavigationItem[] => { const groupsWithDocuments = documentGroups.map(g => { const childrenDocuments = documents .filter(d => d.groupId === g.id) .map(d => ({ id: d.id, name: d.name })) return { id: g.id, name: g.name, children: childrenDocuments } }) const documentsWithoutGroup = documents .filter(d => !d.groupId) .map(d => ({ id: d.id, name: d.name })) return [ ...groupsWithDocuments, { id: 'Uncategorized', name: 'Uncategorized', children: documentsWithoutGroup } ] } function classNames(...classes: any[]) { return classes.filter(Boolean).join(' ') } function WorkspaceNavigation () { const [selectedItemId, setSelectedItemId] = useState('') const [selectedGroupId, setSelectedGroupId] = useState('') const [documents, setDocuments] = useState([] as ipc.Document[]) const [documentGroups, setDocumentGroups] = useState([] as ipc.DocumentGroup[]) const [navigation, setNavigation] = useState([] as NavigationItem[]) useEffect(() => { setNavigation(getNavigationProps(documents, documentGroups)) }, [documents, documentGroups]) GetDocuments().then(response => { LogPrint(JSON.stringify(response, null, 2)) if (!documents.length) setDocuments(response.documents) if (!documentGroups.length) setDocumentGroups(response.documentGroups) }) const getParentGroupIdFromItemId = (itemId: string) => { let parentGroupId = '' navigation.forEach(n => { const foundItem = n.children.find(c => c.id === itemId) if (foundItem) parentGroupId = n.id }) return parentGroupId } const onItemClickHandler = (itemId: string) => { setSelectedItemId(itemId) setSelectedGroupId(getParentGroupIdFromItemId(itemId)) } const renderNavigationItems = () => ( ) return ( <>