11import { ChangeEventHandler , useRef } from 'react' ;
22
3- import { Avatar , Badge , IconButton , styled } from '@mui/material' ;
3+ import {
4+ Avatar ,
5+ Badge ,
6+ CircularProgress ,
7+ IconButton ,
8+ styled ,
9+ } from '@mui/material' ;
10+
11+ import { useLocalContext } from '@graasp/apps-query-client' ;
412
513import { useMutation } from '@tanstack/react-query' ;
614import axios from 'axios' ;
715import { PenIcon } from 'lucide-react' ;
816
917import { CHATBOT_THUMBNAIL_APP_SETTING_NAME } from '@/config/appSetting' ;
10- import { API_HOST } from '@/config/env' ;
1118import { API_ROUTES , hooks } from '@/config/queryClient' ;
1219
1320const { buildUploadAppSettingFilesRoute } = API_ROUTES ;
@@ -33,13 +40,8 @@ const EditButton = styled(IconButton)(() => ({
3340 } ,
3441} ) ) ;
3542
36- const getItemId = ( ) => {
37- const sp = new URL ( location . href ) . searchParams ;
38- return sp . get ( 'itemId' ) ?? 'invalid' ;
39- } ;
40-
4143export const useUploadThumbnail = ( ) => {
42- const itemId = getItemId ( ) ;
44+ const { itemId, apiHost } = useLocalContext ( ) ;
4345 const { data : token } = hooks . useAuthToken ( itemId ) ;
4446 return useMutation ( {
4547 mutationFn : async ( args : { file : Blob } ) => {
@@ -53,7 +55,7 @@ export const useUploadThumbnail = () => {
5355 payload . append ( 'files' , args . file ) ;
5456
5557 await axios . post (
56- `${ API_HOST } /${ buildUploadAppSettingFilesRoute ( itemId ) } ` ,
58+ `${ apiHost } /${ buildUploadAppSettingFilesRoute ( itemId ) } ` ,
5759 payload ,
5860 {
5961 // formData: true,
@@ -76,7 +78,7 @@ export const useUploadThumbnail = () => {
7678
7779export function ChatbotAvatarEditor ( ) {
7880 const fileInput = useRef < HTMLInputElement > ( null ) ;
79- const { mutate : uploadThumbnail } = useUploadThumbnail ( ) ;
81+ const { mutate : uploadThumbnail , isPending } = useUploadThumbnail ( ) ;
8082 const { data : appSettings } = hooks . useAppSettings ( {
8183 name : CHATBOT_THUMBNAIL_APP_SETTING_NAME ,
8284 } ) ;
@@ -101,20 +103,24 @@ export function ChatbotAvatarEditor() {
101103 < Badge
102104 anchorOrigin = { { vertical : 'bottom' , horizontal : 'right' } }
103105 badgeContent = {
104- < EditButton
105- color = "info"
106- onClick = { ( ) => {
107- fileInput ?. current ?. click ( ) ;
108- } }
109- >
110- < PenIcon />
111- < VisuallyHiddenInput
112- ref = { fileInput }
113- onChange = { onChange }
114- type = "file"
115- accept = "image/png, image/jpeg, image/jpg"
116- />
117- </ EditButton >
106+ isPending ? (
107+ < CircularProgress />
108+ ) : (
109+ < EditButton
110+ color = "info"
111+ onClick = { ( ) => {
112+ fileInput ?. current ?. click ( ) ;
113+ } }
114+ >
115+ < PenIcon />
116+ < VisuallyHiddenInput
117+ ref = { fileInput }
118+ onChange = { onChange }
119+ type = "file"
120+ accept = "image/png, image/jpeg, image/jpg"
121+ />
122+ </ EditButton >
123+ )
118124 }
119125 overlap = "circular"
120126 >
0 commit comments