Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/core/browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect, vi } from 'vitest'
import { openGenerationInBrowser } from './browser.js'

describe('openGenerationInBrowser', () => {
it('opens the generation URL and returns null on success', async () => {
const openFn = vi.fn().mockResolvedValue(undefined)
const result = await openGenerationInBrowser('gen_123', openFn)

expect(openFn).toHaveBeenCalledOnce()
expect(openFn).toHaveBeenCalledWith('https://www.pixelmuse.studio/g/gen_123')
expect(result).toBeNull()
})

it('returns the error message when open fails', async () => {
const openFn = vi.fn().mockRejectedValue(new Error('No browser available'))
const result = await openGenerationInBrowser('gen_123', openFn)

expect(result).toBe('No browser available')
})
})
20 changes: 20 additions & 0 deletions src/core/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import open from 'open'

type OpenFunction = (target: string) => Promise<unknown> | unknown

export async function openGenerationInBrowser(
generationId: string,
openFn: OpenFunction = open,
): Promise<string | null> {
const generationUrl = `https://www.pixelmuse.studio/g/${generationId}`

try {
await Promise.resolve(openFn(generationUrl))
return null
} catch (error: unknown) {
if (error instanceof Error && error.message) {
return error.message
}
return 'Unknown error'
}
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export { timeAgo } from './utils.js'
export { initiateDeviceAuth, pollForToken } from './device-auth.js'
export { detectEditors, configureMcp, type EditorInfo } from './mcp-config.js'
export { buildMacClipboardArgs } from './clipboard.js'
export { openGenerationInBrowser } from './browser.js'
11 changes: 9 additions & 2 deletions src/screens/GalleryDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useState, useEffect } from 'react'
import { Box, Text, useInput } from 'ink'
import { ConfirmInput, Spinner } from '@inkjs/ui'
import open from 'open'
import type { PixelmuseClient } from '../core/client.js'
import type { Generation } from '../core/types.js'
import { imageToBuffer, autoSave } from '../core/image.js'
import { openGenerationInBrowser } from '../core/browser.js'
import ImagePreview from '../components/ImagePreview.js'

interface Props {
Expand All @@ -19,6 +19,7 @@ export default function GalleryDetail({ client, generationId, back }: Props) {
const [loading, setLoading] = useState(true)
const [confirming, setConfirming] = useState(false)
const [error, setError] = useState<string | null>(null)
const [actionError, setActionError] = useState<string | null>(null)

useEffect(() => {
let cancelled = false
Expand Down Expand Up @@ -49,7 +50,12 @@ export default function GalleryDetail({ client, generationId, back }: Props) {
if (confirming) return
if (input === 'd') setConfirming(true)
if (input === 'o' && generation) {
open(`https://www.pixelmuse.studio/g/${generation.id}`)
setActionError(null)
void openGenerationInBrowser(generation.id).then((openError) => {
if (openError) {
setActionError(`Could not open browser: ${openError}`)
}
})
}
})

Expand Down Expand Up @@ -113,6 +119,7 @@ export default function GalleryDetail({ client, generationId, back }: Props) {
) : (
<Text color="gray">[o] open in browser | [d] delete | esc back</Text>
)}
{actionError && <Text color="yellow">{actionError}</Text>}
</Box>
)
}
Loading