-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1150 from tloncorp/dd/image-upload-buttons
groups: s3 image upload buttons
- Loading branch information
Showing
6 changed files
with
239 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
import { findLast } from 'lodash'; | ||
import cn from 'classnames'; | ||
import { UseFormRegister, UseFormSetValue } from 'react-hook-form'; | ||
import { isValidUrl } from '@/logic/utils'; | ||
import LinkIcon from '@/components/icons/LinkIcon'; | ||
import useFileUpload from '@/logic/useFileUpload'; | ||
import { useFileStore } from '@/state/storage'; | ||
import LoadingSpinner from '@/components/LoadingSpinner/LoadingSpinner'; | ||
import { UploadErrorPopover } from '@/chat/ChatInput/ChatInput'; | ||
|
||
interface ImageURLUploadFieldProps { | ||
formRegister: UseFormRegister<any>; | ||
formWatchURL: string | null | undefined; | ||
formSetValue: UseFormSetValue<any>; | ||
formValue: string; | ||
} | ||
|
||
export default function ImageURLUploadField({ | ||
formRegister, | ||
formWatchURL, | ||
formSetValue, | ||
formValue, | ||
}: ImageURLUploadFieldProps) { | ||
const [isFocused, setIsFocused] = useState(false); | ||
const urlHasLength = formWatchURL?.length; | ||
|
||
const [uploadError, setUploadError] = useState<string | null>(null); | ||
const { loaded, hasCredentials, promptUpload } = useFileUpload(); | ||
const fileId = useRef(`chat-input-${Math.floor(Math.random() * 1000000)}`); | ||
const mostRecentFile = useFileStore((state) => | ||
findLast(state.files, ['for', fileId.current]) | ||
); | ||
|
||
useEffect(() => { | ||
if ( | ||
mostRecentFile && | ||
mostRecentFile.status === 'error' && | ||
mostRecentFile.errorMessage | ||
) { | ||
setUploadError(mostRecentFile.errorMessage); | ||
} | ||
|
||
if (mostRecentFile && mostRecentFile.status === 'success') { | ||
formSetValue(formValue, mostRecentFile.url, { | ||
shouldDirty: true, | ||
shouldTouch: true, | ||
}); | ||
} | ||
}, [mostRecentFile, formSetValue, formValue]); | ||
|
||
return ( | ||
<div className="input relative flex w-full items-center justify-end"> | ||
<input | ||
className={cn('input-inner grow p-0')} | ||
onFocus={() => setIsFocused(true)} | ||
{...formRegister(formValue, { | ||
onBlur: () => setIsFocused(false), | ||
validate: (value: string) => | ||
value && value.length ? isValidUrl(value) : true, | ||
})} | ||
/> | ||
{!isFocused && !urlHasLength ? ( | ||
<div className="pointer-events-none absolute left-[0.5625rem] flex cursor-pointer items-center"> | ||
<LinkIcon className="mr-1 inline h-4 w-4 fill-gray-100" /> | ||
<span className="pointer-events-none">Paste an image URL</span> | ||
</div> | ||
) : null} | ||
{loaded && hasCredentials ? ( | ||
<button | ||
title={'Upload an image'} | ||
className="small-button whitespace-nowrap" | ||
aria-label="Add attachment" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
promptUpload(fileId.current); | ||
}} | ||
> | ||
{mostRecentFile && mostRecentFile.status === 'loading' ? ( | ||
<LoadingSpinner secondary="black" className="h-4 w-4" /> | ||
) : ( | ||
'Upload Image' | ||
)} | ||
</button> | ||
) : null} | ||
{uploadError ? ( | ||
<div className="absolute mr-2"> | ||
<UploadErrorPopover | ||
errorMessage={uploadError} | ||
setUploadError={setUploadError} | ||
/> | ||
</div> | ||
) : null} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
11f16fd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
homestead-storybook – ./ui
homestead-storybook-tlon.vercel.app
homestead-storybook.vercel.app
homestead-storybook-git-master-tlon.vercel.app
11f16fd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
chatstead – ./ui
chatstead-git-master-tlon.vercel.app
chatstead.vercel.app
chatstead-tlon.vercel.app
11f16fd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
homestead – ./ui
homestead-git-master-tlon.vercel.app
homestead-tlon.vercel.app
tlon-homestead.vercel.app