@shopify/shop-minis-react 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/index3.js +2 -5
- package/dist/_virtual/index3.js.map +1 -1
- package/dist/_virtual/index4.js +5 -2
- package/dist/_virtual/index4.js.map +1 -1
- package/dist/hooks/util/useImagePicker.js +13 -6
- package/dist/hooks/util/useImagePicker.js.map +1 -1
- package/dist/internal/utils/resizeImage.js +61 -0
- package/dist/internal/utils/resizeImage.js.map +1 -0
- package/dist/providers/ImagePickerProvider.js +123 -102
- package/dist/providers/ImagePickerProvider.js.map +1 -1
- package/dist/shop-minis-react/node_modules/.pnpm/querystringify@2.2.0/node_modules/querystringify/index.js +1 -1
- package/dist/shop-minis-react/node_modules/.pnpm/video.js@8.23.3/node_modules/video.js/dist/video.es.js +1 -1
- package/package.json +1 -1
- package/src/hooks/util/useImagePicker.test.tsx +193 -0
- package/src/hooks/util/useImagePicker.ts +24 -5
- package/src/internal/utils/resizeImage.test.ts +314 -0
- package/src/internal/utils/resizeImage.ts +108 -0
- package/src/providers/ImagePickerProvider.test.tsx +32 -1
- package/src/providers/ImagePickerProvider.tsx +108 -65
|
@@ -9,12 +9,29 @@ import React, {
|
|
|
9
9
|
|
|
10
10
|
import {useRequestPermissions} from '../hooks/util/useRequestPermissions'
|
|
11
11
|
import {useReportInteraction} from '../internal/useReportInteraction'
|
|
12
|
+
import {resizeImage} from '../internal/utils/resizeImage'
|
|
12
13
|
|
|
14
|
+
export type ImageQuality = 'low' | 'medium' | 'high' | 'original'
|
|
13
15
|
export type CameraFacing = 'front' | 'back'
|
|
16
|
+
export interface CustomImageQuality {
|
|
17
|
+
size?: number
|
|
18
|
+
compression?: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface OpenCameraParams {
|
|
22
|
+
cameraFacing?: CameraFacing
|
|
23
|
+
quality?: ImageQuality
|
|
24
|
+
customQuality?: CustomImageQuality
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface OpenGalleryParams {
|
|
28
|
+
quality?: ImageQuality
|
|
29
|
+
customQuality?: CustomImageQuality
|
|
30
|
+
}
|
|
14
31
|
|
|
15
32
|
interface ImagePickerContextValue {
|
|
16
|
-
openCamera: (
|
|
17
|
-
openGallery: () => Promise<File>
|
|
33
|
+
openCamera: (params?: OpenCameraParams) => Promise<File>
|
|
34
|
+
openGallery: (params?: OpenGalleryParams) => Promise<File>
|
|
18
35
|
}
|
|
19
36
|
|
|
20
37
|
const ImagePickerContext = createContext<ImagePickerContextValue | null>(null)
|
|
@@ -44,7 +61,8 @@ export function ImagePickerProvider({children}: ImagePickerProviderProps) {
|
|
|
44
61
|
handler: () => void
|
|
45
62
|
} | null>(null)
|
|
46
63
|
const activeOperationRef = useRef<'gallery' | 'camera' | null>(null)
|
|
47
|
-
|
|
64
|
+
const qualityRef = useRef<ImageQuality>('medium')
|
|
65
|
+
const customQualityRef = useRef<CustomImageQuality>(undefined)
|
|
48
66
|
const {requestPermission} = useRequestPermissions()
|
|
49
67
|
const {reportInteraction} = useReportInteraction()
|
|
50
68
|
|
|
@@ -77,26 +95,41 @@ export function ImagePickerProvider({children}: ImagePickerProviderProps) {
|
|
|
77
95
|
resolveRef.current = null
|
|
78
96
|
rejectRef.current = null
|
|
79
97
|
activeOperationRef.current = null
|
|
98
|
+
qualityRef.current = 'medium'
|
|
99
|
+
customQualityRef.current = undefined
|
|
80
100
|
}
|
|
81
101
|
}, [reportInteraction])
|
|
82
102
|
|
|
83
103
|
const handleFileChange = useCallback(
|
|
84
|
-
(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
85
|
-
const
|
|
104
|
+
async (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
105
|
+
const {target} = event
|
|
106
|
+
const file = target.files?.[0]
|
|
86
107
|
|
|
87
108
|
if (file && resolveRef.current) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
} else if (activeOperationRef.current === 'camera') {
|
|
94
|
-
reportInteraction({
|
|
95
|
-
interactionType: 'camera_success',
|
|
109
|
+
try {
|
|
110
|
+
const resizedFile = await resizeImage({
|
|
111
|
+
file,
|
|
112
|
+
quality: qualityRef.current,
|
|
113
|
+
customQuality: customQualityRef.current,
|
|
96
114
|
})
|
|
97
|
-
}
|
|
98
115
|
|
|
99
|
-
|
|
116
|
+
if (activeOperationRef.current === 'gallery') {
|
|
117
|
+
reportInteraction({
|
|
118
|
+
interactionType: 'image_picker_success',
|
|
119
|
+
})
|
|
120
|
+
} else if (activeOperationRef.current === 'camera') {
|
|
121
|
+
reportInteraction({
|
|
122
|
+
interactionType: 'camera_success',
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
resolveRef.current(resizedFile)
|
|
127
|
+
} catch (error) {
|
|
128
|
+
console.warn('Image resize failed, using original:', error)
|
|
129
|
+
if (resolveRef.current) {
|
|
130
|
+
resolveRef.current(file)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
100
133
|
|
|
101
134
|
resolveRef.current = null
|
|
102
135
|
rejectRef.current = null
|
|
@@ -105,80 +138,90 @@ export function ImagePickerProvider({children}: ImagePickerProviderProps) {
|
|
|
105
138
|
cleanupCancelHandler()
|
|
106
139
|
}
|
|
107
140
|
|
|
108
|
-
|
|
141
|
+
target.value = ''
|
|
109
142
|
},
|
|
110
143
|
[cleanupCancelHandler, reportInteraction]
|
|
111
144
|
)
|
|
112
145
|
|
|
113
|
-
const openGallery = useCallback(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
resolveRef.current = resolve
|
|
119
|
-
rejectRef.current = reject
|
|
120
|
-
activeOperationRef.current = 'gallery'
|
|
121
|
-
|
|
122
|
-
const input = galleryInputRef.current
|
|
146
|
+
const openGallery = useCallback(
|
|
147
|
+
({quality = 'medium', customQuality}: OpenGalleryParams = {}) => {
|
|
148
|
+
return new Promise<File>((resolve, reject) => {
|
|
149
|
+
rejectPendingPromise()
|
|
150
|
+
cleanupCancelHandler()
|
|
123
151
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
reject(error)
|
|
131
|
-
resolveRef.current = null
|
|
132
|
-
rejectRef.current = null
|
|
133
|
-
activeOperationRef.current = null
|
|
134
|
-
return
|
|
135
|
-
}
|
|
152
|
+
qualityRef.current = quality
|
|
153
|
+
customQualityRef.current = customQuality
|
|
154
|
+
resolveRef.current = resolve
|
|
155
|
+
rejectRef.current = reject
|
|
156
|
+
activeOperationRef.current = 'gallery'
|
|
157
|
+
const input = galleryInputRef.current
|
|
136
158
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const error = new Error('User cancelled file selection')
|
|
159
|
+
if (!input) {
|
|
160
|
+
const error = new Error('Gallery input not found')
|
|
140
161
|
reportInteraction({
|
|
141
162
|
interactionType: 'image_picker_error',
|
|
142
163
|
interactionValue: error.message,
|
|
143
164
|
})
|
|
144
|
-
|
|
165
|
+
reject(error)
|
|
145
166
|
resolveRef.current = null
|
|
146
167
|
rejectRef.current = null
|
|
147
168
|
activeOperationRef.current = null
|
|
169
|
+
return
|
|
148
170
|
}
|
|
149
|
-
cleanupCancelHandler()
|
|
150
|
-
}
|
|
151
171
|
|
|
152
|
-
|
|
153
|
-
|
|
172
|
+
const handleCancel = () => {
|
|
173
|
+
if (rejectRef.current) {
|
|
174
|
+
const error = new Error('User cancelled file selection')
|
|
175
|
+
reportInteraction({
|
|
176
|
+
interactionType: 'image_picker_error',
|
|
177
|
+
interactionValue: error.message,
|
|
178
|
+
})
|
|
179
|
+
rejectRef.current(error)
|
|
180
|
+
resolveRef.current = null
|
|
181
|
+
rejectRef.current = null
|
|
182
|
+
activeOperationRef.current = null
|
|
183
|
+
}
|
|
184
|
+
cleanupCancelHandler()
|
|
185
|
+
}
|
|
154
186
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
})
|
|
187
|
+
input.addEventListener('cancel', handleCancel)
|
|
188
|
+
activeCancelHandlerRef.current = {input, handler: handleCancel}
|
|
158
189
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
// This will show both Camera and Gallery
|
|
162
|
-
input.click()
|
|
163
|
-
})
|
|
164
|
-
.catch(() => {
|
|
165
|
-
// Show only Gallery
|
|
166
|
-
input.click()
|
|
190
|
+
reportInteraction({
|
|
191
|
+
interactionType: 'image_picker_open',
|
|
167
192
|
})
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
193
|
+
|
|
194
|
+
requestPermission({permission: 'CAMERA'})
|
|
195
|
+
.then(() => {
|
|
196
|
+
// This will show both Camera and Gallery
|
|
197
|
+
input.click()
|
|
198
|
+
})
|
|
199
|
+
.catch(() => {
|
|
200
|
+
// Show only Gallery
|
|
201
|
+
input.click()
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
},
|
|
205
|
+
[
|
|
206
|
+
rejectPendingPromise,
|
|
207
|
+
cleanupCancelHandler,
|
|
208
|
+
requestPermission,
|
|
209
|
+
reportInteraction,
|
|
210
|
+
]
|
|
211
|
+
)
|
|
175
212
|
|
|
176
213
|
const openCamera = useCallback(
|
|
177
|
-
(
|
|
214
|
+
({
|
|
215
|
+
cameraFacing = 'back',
|
|
216
|
+
quality = 'medium',
|
|
217
|
+
customQuality,
|
|
218
|
+
}: OpenCameraParams = {}) => {
|
|
178
219
|
return new Promise<File>((resolve, reject) => {
|
|
179
220
|
rejectPendingPromise()
|
|
180
221
|
cleanupCancelHandler()
|
|
181
222
|
|
|
223
|
+
qualityRef.current = quality
|
|
224
|
+
customQualityRef.current = customQuality
|
|
182
225
|
resolveRef.current = resolve
|
|
183
226
|
rejectRef.current = reject
|
|
184
227
|
activeOperationRef.current = 'camera'
|