mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(download): keep iOS home screen app saves in-app via the share sheet
This commit is contained in:
1 parent
b21c80c9e8
commit
2c00d244d9
9 files changed
+150
-46
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { fetchWrapper, fetchWrapperBlob } from './fetchWrapper'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { saveFile } from '@/lib/download'
|
||||
import type { FileInfo, ChunkedFileInfo, PatchOperation } from '@/types/files'
|
||||
|
||||
interface FileApiUrlOptions {
|
||||
@@ -59,15 +60,8 @@ export async function downloadDirectoryAsZip(path: string, options?: DownloadOpt
|
||||
})
|
||||
|
||||
const blob = await fetchWrapperBlob(url)
|
||||
const urlObj = window.URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = urlObj
|
||||
const dirName = path.split('/').pop() || 'download'
|
||||
a.download = `${dirName}.zip`
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
window.URL.revokeObjectURL(urlObj)
|
||||
await saveFile(blob, `${dirName}.zip`)
|
||||
}
|
||||
|
||||
export async function applyFilePatches(path: string, patches: PatchOperation[]): Promise<{ success: boolean; totalLines: number }> {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { Repo } from './types'
|
||||
import { FetchError, fetchWrapper, fetchWrapperVoid, fetchWrapperBlob } from './fetchWrapper'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { saveFile } from '@/lib/download'
|
||||
import type { DiscoverReposResponse, AssistantModeStatus, AssistantModeInitRequest } from '@opencode-manager/shared/types'
|
||||
|
||||
export interface CreateRepoOptions {
|
||||
@@ -196,14 +197,7 @@ export async function downloadRepo(id: number, repoName: string, options?: Downl
|
||||
const url = `${API_BASE_URL}/api/repos/${id}/download${params.toString() ? '?' + params.toString() : ''}`
|
||||
|
||||
const blob = await fetchWrapperBlob(url)
|
||||
const urlObj = window.URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = urlObj
|
||||
a.download = `${repoName}.zip`
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
window.URL.revokeObjectURL(urlObj)
|
||||
await saveFile(blob, `${repoName}.zip`)
|
||||
}
|
||||
|
||||
export async function updateRepoOrder(order: number[]): Promise<void> {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Button } from '@/components/ui/button'
|
||||
import { Download, X, Edit3, Save, X as XIcon, WrapText, Eye, Code } from 'lucide-react'
|
||||
import type { FileInfo } from '@/types/files'
|
||||
import { getFileApiUrl } from '@/api/files'
|
||||
import { saveFileFromUrl } from '@/lib/download'
|
||||
import { VirtualizedTextView, type VirtualizedTextViewHandle } from '@/components/ui/virtualized-text-view'
|
||||
import { MarkdownRenderer } from './MarkdownRenderer'
|
||||
|
||||
@@ -94,12 +95,7 @@ export const FilePreview = memo(function FilePreview({ file, hideHeader = false,
|
||||
}
|
||||
|
||||
const handleDownload = () => {
|
||||
const link = document.createElement('a')
|
||||
link.href = getFileApiUrl(file.path, { params: { download: true } })
|
||||
link.download = file.name
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
void saveFileFromUrl(getFileApiUrl(file.path, { params: { download: true } }), file.name)
|
||||
}
|
||||
|
||||
const decodeBase64 = (base64: string): string => {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { DeleteDialog } from '@/components/ui/delete-dialog'
|
||||
import { getFileApiUrl } from '@/api/files'
|
||||
import { saveFileFromUrl } from '@/lib/download'
|
||||
import {
|
||||
File,
|
||||
Folder,
|
||||
@@ -95,13 +96,7 @@ function TreeNode({ file, level, onFileSelect, onDirectoryClick, selectedFile, o
|
||||
const handleDownload = () => {
|
||||
if (file.isDirectory) return
|
||||
|
||||
const downloadUrl = getFileApiUrl(file.path, { params: { download: true } })
|
||||
const link = document.createElement('a')
|
||||
link.href = downloadUrl
|
||||
link.download = file.name
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
void saveFileFromUrl(getFileApiUrl(file.path, { params: { download: true } }), file.name)
|
||||
}
|
||||
|
||||
const getFileIcon = () => {
|
||||
|
||||
@@ -24,6 +24,7 @@ import { useServerHealth } from '@/hooks/useServerHealth'
|
||||
import { useOpenCodeServerActions } from '@/hooks/useOpenCodeServerActions'
|
||||
import { parseJsonc, hasJsoncComments } from '@/lib/jsonc'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import { saveFile } from '@/lib/download'
|
||||
import { invalidateConfigCaches } from '@/lib/queryInvalidation'
|
||||
import { getOpenCodeApiErrorMessage } from '@/lib/opencode-errors'
|
||||
import { FetchError } from '@/api/fetchWrapper'
|
||||
@@ -308,12 +309,7 @@ export function OpenCodeConfigManager() {
|
||||
const content = config.rawContent || JSON.stringify(config.content, null, 2)
|
||||
const extension = config.rawContent && hasJsoncComments(config.rawContent) ? 'jsonc' : 'json'
|
||||
const blob = new Blob([content], { type: 'application/json' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `${config.name}.${extension}`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
void saveFile(blob, `${config.name}.${extension}`)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { saveFile } from './download'
|
||||
import { showToast } from './toast'
|
||||
|
||||
vi.mock('./toast', () => ({
|
||||
showToast: {
|
||||
error: vi.fn(),
|
||||
info: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
function setIosHomeScreenApp(): void {
|
||||
Object.defineProperty(navigator, 'userAgent', { value: 'iPhone', configurable: true })
|
||||
Object.defineProperty(navigator, 'standalone', { value: true, configurable: true })
|
||||
}
|
||||
|
||||
describe('saveFile', () => {
|
||||
let clickSpy: ReturnType<typeof vi.spyOn>
|
||||
|
||||
beforeEach(() => {
|
||||
clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
||||
vi.stubGlobal('URL', Object.assign(URL, {
|
||||
createObjectURL: vi.fn(() => 'blob:mock'),
|
||||
revokeObjectURL: vi.fn(),
|
||||
}))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
Reflect.deleteProperty(navigator, 'standalone')
|
||||
Reflect.deleteProperty(navigator, 'canShare')
|
||||
Reflect.deleteProperty(navigator, 'share')
|
||||
})
|
||||
|
||||
it('downloads via an anchor outside iOS home screen apps', async () => {
|
||||
await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
|
||||
expect(clickSpy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('uses the share sheet instead of navigating in an iOS home screen app', async () => {
|
||||
setIosHomeScreenApp()
|
||||
const share = vi.fn().mockResolvedValue(undefined)
|
||||
Object.defineProperty(navigator, 'canShare', { value: () => true, configurable: true })
|
||||
Object.defineProperty(navigator, 'share', { value: share, configurable: true })
|
||||
|
||||
await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
|
||||
expect(clickSpy).not.toHaveBeenCalled()
|
||||
const shared = share.mock.calls[0][0].files[0] as File
|
||||
expect(shared.name).toBe('notes.md')
|
||||
})
|
||||
|
||||
it('offers a retry when the share loses user activation', async () => {
|
||||
setIosHomeScreenApp()
|
||||
const share = vi.fn()
|
||||
.mockRejectedValueOnce(new DOMException('no activation', 'NotAllowedError'))
|
||||
.mockResolvedValueOnce(undefined)
|
||||
Object.defineProperty(navigator, 'canShare', { value: () => true, configurable: true })
|
||||
Object.defineProperty(navigator, 'share', { value: share, configurable: true })
|
||||
|
||||
await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
|
||||
const options = vi.mocked(showToast.info).mock.calls[0][1]
|
||||
options?.action?.onClick()
|
||||
await vi.waitFor(() => expect(share).toHaveBeenCalledTimes(2))
|
||||
expect(clickSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,66 @@
|
||||
import { fetchWrapperBlob } from '@/api/fetchWrapper'
|
||||
import { showToast } from './toast'
|
||||
|
||||
function isIosHomeScreenApp(): boolean {
|
||||
const isIos =
|
||||
/iPad|iPhone|iPod/.test(navigator.userAgent) ||
|
||||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
|
||||
if (!isIos) return false
|
||||
|
||||
const isStandalone =
|
||||
(navigator as Navigator & { standalone?: boolean }).standalone === true ||
|
||||
window.matchMedia('(display-mode: standalone)').matches
|
||||
return isStandalone
|
||||
}
|
||||
|
||||
function saveViaAnchor(href: string, filename: string): void {
|
||||
const link = document.createElement('a')
|
||||
link.href = href
|
||||
link.download = filename
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
}
|
||||
|
||||
async function saveViaShareSheet(blob: Blob, filename: string): Promise<void> {
|
||||
const file = new File([blob], filename, { type: blob.type || 'application/octet-stream' })
|
||||
|
||||
if (!navigator.canShare?.({ files: [file] })) {
|
||||
showToast.error(`${filename} cannot be saved from this device`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await navigator.share({ files: [file] })
|
||||
} catch (error) {
|
||||
const name = error instanceof DOMException ? error.name : ''
|
||||
if (name === 'AbortError') return
|
||||
if (name === 'NotAllowedError') {
|
||||
showToast.info(`Tap Save to finish saving ${filename}`, {
|
||||
action: { label: 'Save', onClick: () => void saveViaShareSheet(blob, filename) },
|
||||
})
|
||||
return
|
||||
}
|
||||
showToast.error(`Failed to save ${filename}`)
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveFile(blob: Blob, filename: string): Promise<void> {
|
||||
if (isIosHomeScreenApp()) {
|
||||
await saveViaShareSheet(blob, filename)
|
||||
return
|
||||
}
|
||||
|
||||
const url = URL.createObjectURL(blob)
|
||||
saveViaAnchor(url, filename)
|
||||
setTimeout(() => URL.revokeObjectURL(url), 0)
|
||||
}
|
||||
|
||||
export async function saveFileFromUrl(url: string, filename: string): Promise<void> {
|
||||
if (isIosHomeScreenApp()) {
|
||||
await saveFile(await fetchWrapperBlob(url), filename)
|
||||
return
|
||||
}
|
||||
|
||||
saveViaAnchor(url, filename)
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { MessageWithParts, Session, Part } from '@/api/types'
|
||||
import { saveFile } from './download'
|
||||
|
||||
function formatDate(timestamp: number): string {
|
||||
return new Date(timestamp).toLocaleString()
|
||||
@@ -115,16 +116,8 @@ function generateSessionMarkdown(
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
export function downloadMarkdown(content: string, filename: string): void {
|
||||
const blob = new Blob([content], { type: 'text/markdown' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.download = filename
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
URL.revokeObjectURL(url)
|
||||
export async function downloadMarkdown(content: string, filename: string): Promise<void> {
|
||||
await saveFile(new Blob([content], { type: 'text/markdown' }), filename)
|
||||
}
|
||||
|
||||
export function exportSession(
|
||||
|
||||
@@ -420,7 +420,7 @@ export function SessionDetail() {
|
||||
}
|
||||
|
||||
const { filename, content } = exportSession(data, session)
|
||||
downloadMarkdown(content, filename)
|
||||
void downloadMarkdown(content, filename)
|
||||
showToast.success(`Exported to ${filename}`)
|
||||
}, [getMessagesWithParts, session]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user