Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.5 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { IconFolderMove, IconFolderOpen, IconStar, IconStarFilled } from '@posthog/icons'

import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'
import { projectTreeDataLogic } from '~/layout/panel-layout/ProjectTree/projectTreeDataLogic'
import { PROJECT_TREE_KEY, projectTreeLogic } from '~/layout/panel-layout/ProjectTree/projectTreeLogic'
import { joinPath, splitPath } from '~/layout/panel-layout/ProjectTree/utils'
import { SceneMenuBarItem } from '~/layout/scenes/components/SceneMenuBar'

import { moveToLogic } from '../FileSystem/MoveTo/moveToLogic'
import { sceneFileLogic } from './sceneFileLogic'

type SceneMenuBarFileItemsProps = {
    /** Used as a prefix on data-attr for testing */
    dataAttrKey: string
}

/**
 * File-system actions for the current scene's tree entry, intended to be placed inside
 * a <SceneMenuBarMenu label="File">. Returns null when `sceneFileLogic.hasFileItems` is false, so parents gate on it.
 */
export function SceneMenuBarFileItems({ dataAttrKey }: SceneMenuBarFileItemsProps): JSX.Element | null {
    const { fileItemsEntry: projectTreeRefEntry } = useValues(sceneFileLogic)
    const { assureVisibility } = useActions(projectTreeLogic({ key: PROJECT_TREE_KEY }))
    const { showLayoutPanel, setActivePanelIdentifier } = useActions(panelLayoutLogic)
    const { addShortcutItem, deleteShortcut } = useActions(projectTreeDataLogic)
    const { shortcutNonFolderPaths, shortcutData } = useValues(projectTreeDataLogic)
    const { openMoveToModal } = useActions(moveToLogic)
    if (!projectTreeRefEntry) {
        return null
    }

    const itemShortcutPath = joinPath([splitPath(projectTreeRefEntry.path).pop() ?? 'Unnamed'])
    const isAlreadyStarred = projectTreeRefEntry.type !== 'folder' && shortcutNonFolderPaths.has(itemShortcutPath)

    return (
        <>
            <SceneMenuBarItem
                opensFloatingUi
                onClick={() => {
                    assureVisibility({ type: 'folder', ref: projectTreeRefEntry.path })
                    showLayoutPanel(true)
                    setActivePanelIdentifier('Project')
                }}
                data-attr={`${dataAttrKey}-menubar-open-in-project-tree`}
            >
                <IconFolderOpen />
                Open in project tree
            </SceneMenuBarItem>
            <SceneMenuBarItem
                opensFloatingUi
                onClick={() => openMoveToModal([projectTreeRefEntry])}
                data-attr={`${dataAttrKey}-menubar-move-to`}
            >
                <IconFolderMove />
                Move to another folder
            </SceneMenuBarItem>
            <SceneMenuBarItem
                onClick={() => {
                    if (isAlreadyStarred) {
                        const shortcut = shortcutData.find((s) => s.path === itemShortcutPath && s.type !== 'folder')
                        if (shortcut?.id) {
                            deleteShortcut(shortcut.id)
                        }
                    } else {
                        addShortcutItem(projectTreeRefEntry)
                    }
                }}
                data-attr={
                    isAlreadyStarred
                        ? `${dataAttrKey}-menubar-remove-from-starred`
                        : `${dataAttrKey}-menubar-add-to-starred`
                }
            >
                {isAlreadyStarred ? <IconStarFilled className="text-warning" /> : <IconStar />}
                {isAlreadyStarred ? 'Remove from starred' : 'Add to starred'}
            </SceneMenuBarItem>
        </>
    )
}