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
28 changes: 28 additions & 0 deletions src/core/browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { openGenerationInBrowser } from './browser.js'

const launchMock = vi.fn<(target: string) => Promise<void>>()

afterEach(() => {
launchMock.mockReset()
})

describe('openGenerationInBrowser', () => {
it('opens the generation URL and reports success', async () => {
launchMock.mockResolvedValueOnce()
const result = await openGenerationInBrowser('abc123', launchMock)

expect(launchMock).toHaveBeenCalledWith('https://www.pixelmuse.studio/g/abc123')
expect(result).toEqual({ ok: true, message: null })
})

it('returns friendly error when browser launch fails', async () => {
launchMock.mockRejectedValueOnce(new Error('no xdg-open'))
const result = await openGenerationInBrowser('abc123', launchMock)

expect(launchMock).toHaveBeenCalledWith('https://www.pixelmuse.studio/g/abc123')
expect(result.ok).toBe(false)
expect(result.message).toContain('Failed to open browser')
expect(result.message).toContain('https://www.pixelmuse.studio/g/abc123')
})
})
27 changes: 27 additions & 0 deletions src/core/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import open from 'open'

const GENERATION_URL_BASE = 'https://www.pixelmuse.studio/g/'
const OPEN_BROWSER_ERROR = 'Failed to open browser. Open manually: '

export type OpenGenerationResult =
| { ok: true; message: null }
| { ok: false; message: string }

export type OpenUrl = (url: string) => Promise<void>

async function openUrl(url: string): Promise<void> {
await open(url)
}

export async function openGenerationInBrowser(
generationId: string,
launch: OpenUrl = openUrl,
): Promise<OpenGenerationResult> {
const url = `${GENERATION_URL_BASE}${encodeURIComponent(generationId)}`
try {
await launch(url)
return { ok: true, message: null }
} catch {
return { ok: false, message: `${OPEN_BROWSER_ERROR}${url}` }
}
}
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 Down Expand Up @@ -45,11 +45,18 @@ export default function GalleryDetail({ client, generationId, back }: Props) {
}
}, [generationId])

const handleOpenInBrowser = async (id: string) => {
const result = await openGenerationInBrowser(id)
if (!result.ok && result.message) {
setError(result.message)
}
}

useInput((input) => {
if (confirming) return
if (input === 'd') setConfirming(true)
if (input === 'o' && generation) {
open(`https://www.pixelmuse.studio/g/${generation.id}`)
void handleOpenInBrowser(generation.id)
}
})

Expand Down
Loading