MCPcopy Create free account
hub / github.com/editablejs/editable / useImageLoadingStatus

Function useImageLoadingStatus

packages/ui/src/avatar.tsx:112–140  ·  view source on GitHub ↗
(src?: string)

Source from the content-addressed store, hash-verified

110/* -----------------------------------------------------------------------------------------------*/
111
112function useImageLoadingStatus(src?: string) {
113 const [loadingStatus, setLoadingStatus] = React.useState<ImageLoadingStatus>('idle')
114
115 React.useEffect(() => {
116 if (!src) {
117 setLoadingStatus('error')
118 return
119 }
120
121 let isMounted = true
122 const image = new window.Image()
123
124 const updateStatus = (status: ImageLoadingStatus) => () => {
125 if (!isMounted) return
126 setLoadingStatus(status)
127 }
128
129 setLoadingStatus('loading')
130 image.onload = updateStatus('loaded')
131 image.onerror = updateStatus('error')
132 image.src = src
133
134 return () => {
135 isMounted = false
136 }
137 }, [src])
138
139 return loadingStatus
140}
141
142export {
143 //

Callers 1

avatar.tsxFile · 0.85

Calls 1

updateStatusFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…