mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(cloud): Run menu suggestions re-read package.json on menu open, hover and window focus (LC2-P3)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { defineFixture } from '../../component-fixtures/component-fixture'
|
||||
import { suggestedRunCommands } from '@/hooks/use-cloud-suggested-run-commands'
|
||||
import type { HostedTerminalQuickCommand } from '@/hooks/use-terminal-quick-command-hosts'
|
||||
import { TabBarQuickCommandItem } from './TabBarQuickCommandItem'
|
||||
|
||||
const HOST = { hostId: 'runtime:env' as const, label: 'Orca Cloud' }
|
||||
const noop = (): void => {}
|
||||
|
||||
const saved: HostedTerminalQuickCommand = {
|
||||
command: {
|
||||
id: 'saved-dev',
|
||||
label: 'dev server',
|
||||
command: 'pnpm dev --port 3000',
|
||||
appendEnter: true
|
||||
},
|
||||
hostId: HOST.hostId,
|
||||
hostLabel: HOST.label,
|
||||
key: 'runtime:env\0saved-dev'
|
||||
}
|
||||
|
||||
// The Run menu's rows in a cloud workspace: a saved command, then package.json suggestions
|
||||
// (Copy only, "From package.json"). Suggestions are re-read when the menu opens (LC2-P3).
|
||||
export const cloudRunMenuRows = defineFixture({
|
||||
width: 420,
|
||||
render: () => (
|
||||
<div role="listbox" className="flex flex-col rounded-lg border border-border bg-popover py-1">
|
||||
{[
|
||||
saved,
|
||||
...suggestedRunCommands(
|
||||
JSON.stringify({
|
||||
packageManager: 'pnpm@9.0.0',
|
||||
scripts: { test: 'vitest', typecheck: 'tsc', lint: 'eslint .' }
|
||||
}),
|
||||
HOST
|
||||
)
|
||||
].map((entry, index) => (
|
||||
<TabBarQuickCommandItem
|
||||
key={entry.key}
|
||||
entry={entry}
|
||||
showHostLabel={false}
|
||||
selected={index === 1}
|
||||
onRun={noop}
|
||||
onEdit={noop}
|
||||
onDelete={noop}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useCallback, useMemo, useState } from 'react'
|
||||
import { Play } from 'lucide-react'
|
||||
import { useAppStore } from '@/store'
|
||||
import { getTerminalQuickCommandHostPromptMaxLength } from '@/lib/terminal-quick-command-host-prompt-limit'
|
||||
@@ -64,7 +64,14 @@ export function TabBarQuickCommandsButton({
|
||||
cloudEnvironmentId && hosts.length === 1 && hosts[0].hostId === executionHostId
|
||||
? { hostId: hosts[0].hostId, label: hosts[0].label, environmentId: cloudEnvironmentId }
|
||||
: null
|
||||
const suggestedCommands = useCloudSuggestedRunCommands(worktreeId, cloudHost)
|
||||
const { commands: suggestedCommands, refresh: refreshSuggestions } = useCloudSuggestedRunCommands(
|
||||
worktreeId,
|
||||
cloudHost
|
||||
)
|
||||
const handleMenuOpen = useCallback((): void => {
|
||||
refreshRemoteHost()
|
||||
refreshSuggestions()
|
||||
}, [refreshRemoteHost, refreshSuggestions])
|
||||
// Floating terminals have a synthetic workspace ID without a repo; never treat that sentinel as a run target.
|
||||
const repoId = useMemo(() => {
|
||||
if (worktreeId === FLOATING_TERMINAL_WORKTREE_ID) {
|
||||
@@ -199,6 +206,8 @@ export function TabBarQuickCommandsButton({
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => addRepoCommand(defaultHostId)}
|
||||
// A package.json added since the last read turns this into the suggestions menu.
|
||||
onPointerEnter={refreshSuggestions}
|
||||
className="my-auto flex h-7 shrink-0 items-center gap-1 rounded-md px-1.5 text-muted-foreground hover:bg-accent/50 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
|
||||
aria-label={t(
|
||||
'auto.components.tab.bar.TabBarQuickCommandsButton.8f1e971966',
|
||||
@@ -241,7 +250,7 @@ export function TabBarQuickCommandsButton({
|
||||
addHosts={hosts}
|
||||
hostLoadFailed={remoteHostLoadFailed}
|
||||
hostOwnershipPending={remoteHostPending}
|
||||
onMenuOpen={refreshRemoteHost}
|
||||
onMenuOpen={handleMenuOpen}
|
||||
onAddCommand={addRepoCommand}
|
||||
onEditCommand={(entry) =>
|
||||
setEditor({ mode: 'edit', command: entry.command, hostId: entry.hostId })
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { act, createElement } from 'react'
|
||||
import { createRoot, type Root } from 'react-dom/client'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
read: vi.fn<() => Promise<{ content: string; isBinary: boolean }>>()
|
||||
}))
|
||||
vi.mock('@/store', () => ({
|
||||
useAppStore: (select: (state: unknown) => unknown) => select({})
|
||||
}))
|
||||
vi.mock('@/store/github/refresh/worktree-refresh', () => ({
|
||||
findWorktreeById: () => ({ path: '/vercel/orca/workspaces/app/ws' })
|
||||
}))
|
||||
vi.mock('@/runtime/files/reads/runtime-file-read-client', () => ({
|
||||
readRuntimeFileContent: mocks.read
|
||||
}))
|
||||
|
||||
const { useCloudSuggestedRunCommands } = await import('./use-cloud-suggested-run-commands')
|
||||
|
||||
const HOST = { hostId: 'runtime:env' as const, label: 'Orca Cloud', environmentId: 'env' }
|
||||
let latest: ReturnType<typeof useCloudSuggestedRunCommands> = { commands: [], refresh: () => {} }
|
||||
function Probe(): null {
|
||||
latest = useCloudSuggestedRunCommands('ws', HOST)
|
||||
return null
|
||||
}
|
||||
|
||||
const flush = (): Promise<void> => act(async () => new Promise((r) => setTimeout(r, 0)))
|
||||
const labels = (): string[] => latest.commands.map((entry) => entry.command.label)
|
||||
|
||||
describe('Run menu suggestions refresh (LC2-P3)', () => {
|
||||
let root: Root
|
||||
beforeEach(() => {
|
||||
mocks.read.mockReset()
|
||||
root = createRoot(document.createElement('div'))
|
||||
})
|
||||
afterEach(() => {
|
||||
act(() => root.unmount())
|
||||
})
|
||||
|
||||
it('a package.json created after mount shows up on the next refresh (menu open)', async () => {
|
||||
mocks.read.mockRejectedValueOnce(new Error('ENOENT: no such file'))
|
||||
await act(async () => root.render(createElement(Probe)))
|
||||
await flush()
|
||||
expect(labels()).toEqual([])
|
||||
|
||||
mocks.read.mockResolvedValue({
|
||||
content: JSON.stringify({ scripts: { test: 'vitest', lint: 'eslint .' } }),
|
||||
isBinary: false
|
||||
})
|
||||
await act(async () => latest.refresh())
|
||||
await flush()
|
||||
expect(labels()).toEqual(['test', 'lint'])
|
||||
})
|
||||
|
||||
it('re-reads when the window regains focus, and keeps the same list when nothing changed', async () => {
|
||||
mocks.read.mockResolvedValue({
|
||||
content: JSON.stringify({ scripts: { test: 'vitest' } }),
|
||||
isBinary: false
|
||||
})
|
||||
await act(async () => root.render(createElement(Probe)))
|
||||
await flush()
|
||||
const first = latest.commands
|
||||
expect(labels()).toEqual(['test'])
|
||||
|
||||
await act(async () => window.dispatchEvent(new Event('focus')))
|
||||
await flush()
|
||||
expect(mocks.read).toHaveBeenCalledTimes(2)
|
||||
expect(latest.commands).toBe(first)
|
||||
})
|
||||
})
|
||||
@@ -2,7 +2,7 @@
|
||||
// lint, test and friends from package.json, run with the package manager the project declares.
|
||||
// Suggestions run like saved quick commands but are not saved; nothing is offered when the file is
|
||||
// missing or unreadable.
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { useAppStore } from '@/store'
|
||||
import { findWorktreeById } from '@/store/github/refresh/worktree-refresh'
|
||||
import { readRuntimeFileContent } from '@/runtime/files/reads/runtime-file-read-client'
|
||||
@@ -57,19 +57,41 @@ export function suggestedRunCommands(
|
||||
})
|
||||
}
|
||||
|
||||
/** Suggestions for one cloud workspace; [] until read, and for anything that isn't cloud. */
|
||||
const EMPTY: HostedTerminalQuickCommand[] = []
|
||||
|
||||
/** Same suggestions (same command lines): keep the old array so the menu doesn't re-render. */
|
||||
function sameSuggestions(
|
||||
a: readonly HostedTerminalQuickCommand[],
|
||||
b: readonly HostedTerminalQuickCommand[]
|
||||
): boolean {
|
||||
const line = (entry: HostedTerminalQuickCommand): string =>
|
||||
`${entry.key}\0${'command' in entry.command ? entry.command.command : ''}`
|
||||
return a.length === b.length && a.every((entry, index) => line(entry) === line(b[index]))
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggestions for one cloud workspace; [] until read, and for anything that isn't cloud.
|
||||
* package.json can change any time (a new script, a file created mid-session), so it is read
|
||||
* again by `refresh` (the Run menu calls it on open and on hover) and when the window regains
|
||||
* focus, not only when the tab bar mounts (LC2-P3).
|
||||
*/
|
||||
export function useCloudSuggestedRunCommands(
|
||||
worktreeId: string,
|
||||
cloudHost: { hostId: ExecutionHostId; label: string; environmentId: string } | null
|
||||
): HostedTerminalQuickCommand[] {
|
||||
const [commands, setCommands] = useState<HostedTerminalQuickCommand[]>([])
|
||||
): { commands: HostedTerminalQuickCommand[]; refresh: () => void } {
|
||||
const [commands, setCommandsRaw] = useState<HostedTerminalQuickCommand[]>(EMPTY)
|
||||
const [generation, setGeneration] = useState(0)
|
||||
const refresh = useCallback(() => setGeneration((value) => value + 1), [])
|
||||
const setCommands = useCallback((next: HostedTerminalQuickCommand[]) => {
|
||||
setCommandsRaw((previous) => (sameSuggestions(previous, next) ? previous : next))
|
||||
}, [])
|
||||
const worktreePath = useAppStore((state) => findWorktreeById(state, worktreeId)?.path ?? null)
|
||||
const hostId = cloudHost?.hostId ?? null
|
||||
const label = cloudHost?.label ?? ''
|
||||
const environmentId = cloudHost?.environmentId ?? null
|
||||
useEffect(() => {
|
||||
if (!hostId || !environmentId || !worktreePath) {
|
||||
setCommands([])
|
||||
setCommands(EMPTY)
|
||||
return
|
||||
}
|
||||
let cancelled = false
|
||||
@@ -100,6 +122,13 @@ export function useCloudSuggestedRunCommands(
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [environmentId, hostId, label, worktreeId, worktreePath])
|
||||
return commands
|
||||
}, [environmentId, generation, hostId, label, setCommands, worktreeId, worktreePath])
|
||||
useEffect(() => {
|
||||
if (!hostId) {
|
||||
return
|
||||
}
|
||||
window.addEventListener('focus', refresh)
|
||||
return () => window.removeEventListener('focus', refresh)
|
||||
}, [hostId, refresh])
|
||||
return { commands, refresh }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user