mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 11:54:46 +00:00
fix(mobile): show the folder icon when a project image fails to load (#37202)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { Folder } from 'lucide-react-native'
|
||||
import { createElement } from 'react'
|
||||
import { Image } from 'react-native'
|
||||
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { MobileRepoIcon } from './MobileRepoIcon'
|
||||
|
||||
vi.mock('react-native', () => ({
|
||||
Image: 'Image',
|
||||
StyleSheet: { create: <T,>(styles: T) => styles },
|
||||
Text: 'Text',
|
||||
View: 'View'
|
||||
}))
|
||||
// Each icon renders as an element named after it.
|
||||
vi.mock(
|
||||
'lucide-react-native',
|
||||
() => new Proxy({}, { has: () => true, get: (_, name) => String(name) })
|
||||
)
|
||||
vi.mock('../theme/use-mobile-color-scheme', () => ({ useMobileColorScheme: () => 'dark' }))
|
||||
|
||||
let renderer: ReactTestRenderer | null = null
|
||||
afterEach(() => {
|
||||
act(() => renderer?.unmount())
|
||||
renderer = null
|
||||
})
|
||||
|
||||
function imageIcon(src: string) {
|
||||
return createElement(MobileRepoIcon, { repoIcon: { type: 'image', src, label: 'owner/repo' } })
|
||||
}
|
||||
|
||||
describe('MobileRepoIcon', () => {
|
||||
it('shows the folder after an image fails, and tries a changed image again', () => {
|
||||
act(() => {
|
||||
renderer = create(imageIcon('https://github.com/owner.png?size=64'))
|
||||
})
|
||||
act(() => renderer!.root.findByType(Image).props.onError())
|
||||
expect(renderer!.root.findAllByType(Image)).toHaveLength(0)
|
||||
expect(renderer!.root.findAllByType(Folder)).toHaveLength(1)
|
||||
|
||||
act(() => renderer!.update(imageIcon('data:image/png;base64,AAAA')))
|
||||
expect(renderer!.root.findByType(Image).props.source).toEqual({
|
||||
uri: 'data:image/png;base64,AAAA'
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
SquareTerminal,
|
||||
Wrench
|
||||
} from 'lucide-react-native'
|
||||
import { useState } from 'react'
|
||||
import { Image, StyleSheet, Text, View } from 'react-native'
|
||||
import type { MobileRenderableRepoIcon } from '../host-screen/host-screen-reply-schema'
|
||||
import { colors, radii } from '../theme/mobile-theme'
|
||||
@@ -61,12 +62,16 @@ type Props = {
|
||||
// Render desktop-style custom image/emoji/lucide with Folder fallback, never bare project dot.
|
||||
export function MobileRepoIcon({ repoIcon, size = 14, color = colors.textSecondary }: Props) {
|
||||
useMobileColorScheme()
|
||||
if (repoIcon?.type === 'image') {
|
||||
// Keyed by src so a changed icon gets a fresh try; the page shell refuses every https load.
|
||||
const [failedSrc, setFailedSrc] = useState<string | null>(null)
|
||||
if (repoIcon?.type === 'image' && repoIcon.src !== failedSrc) {
|
||||
const { src } = repoIcon
|
||||
return (
|
||||
<Image
|
||||
source={{ uri: repoIcon.src }}
|
||||
source={{ uri: src }}
|
||||
style={{ width: size, height: size, borderRadius: radii.mark }}
|
||||
accessibilityLabel={repoIcon.label}
|
||||
onError={() => setFailedSrc(src)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user