mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
tweak(phone/utils): Refactored image validation to promise from cb
This commit is contained in:
1 parent
e330b89889
commit
18c1676c01
2 files changed
+16
-12
No files matched your search
@@ -4,8 +4,7 @@ import { v4 as uuidv4 } from 'uuid';
|
|||||||
|
|
||||||
import Nui from '../../../os/nui-events/utils/Nui';
|
import Nui from '../../../os/nui-events/utils/Nui';
|
||||||
import Modal from '../../../ui/components/Modal';
|
import Modal from '../../../ui/components/Modal';
|
||||||
import { IMAGE_DELIMITER } from '../utils/images';
|
import { IMAGE_DELIMITER, isImageValid } from '../utils/images';
|
||||||
import { withValidImage } from '../utils/images';
|
|
||||||
import { useModal } from '../hooks/useModal';
|
import { useModal } from '../hooks/useModal';
|
||||||
import EmojiSelect from './EmojiSelect';
|
import EmojiSelect from './EmojiSelect';
|
||||||
import ImageDisplay from './images/ImageDisplay';
|
import ImageDisplay from './images/ImageDisplay';
|
||||||
@@ -129,7 +128,9 @@ export const AddTweetModal = () => {
|
|||||||
// other users see this image on their TweetList it will be
|
// other users see this image on their TweetList it will be
|
||||||
// from the database and should already have passed through
|
// from the database and should already have passed through
|
||||||
// this logic
|
// this logic
|
||||||
withValidImage(cleanedLink, () => setImages([...images, image]));
|
isImageValid(cleanedLink)
|
||||||
|
.then(() => setImages([...images, image]))
|
||||||
|
.catch((e) => console.error(e));
|
||||||
|
|
||||||
setShowImagePrompt(false);
|
setShowImagePrompt(false);
|
||||||
setLink('');
|
setLink('');
|
||||||
|
|||||||
@@ -6,15 +6,18 @@
|
|||||||
export const IMAGE_DELIMITER = '||!||';
|
export const IMAGE_DELIMITER = '||!||';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate that the URL successfully loads in the browser
|
* Returns a Promise that validates that the URL successfully
|
||||||
|
* loads in the browser. If the URL doesn't load the promise is
|
||||||
|
* rejected.
|
||||||
* @param {string} link - a URL we are trying to validate
|
* @param {string} link - a URL we are trying to validate
|
||||||
* @param {func} onSuccess - callback if the link is valid
|
* @returns {Promise<string>} link - Will resolve the promise with the link string
|
||||||
|
* if valid. Will reject if failed.
|
||||||
*/
|
*/
|
||||||
export function withValidImage(link: string, onSuccess: any): void {
|
export function isImageValid(link: string): Promise<string> {
|
||||||
const image = new Image();
|
return new Promise((res, rej) => {
|
||||||
image.onload = onSuccess;
|
const image = new Image();
|
||||||
image.onerror = () => {
|
image.src = link;
|
||||||
console.warn(`Invalid image submitted: ${link}`);
|
image.onerror = () => rej(`Invalid image, ${link}`);
|
||||||
};
|
image.onload = () => res(link);
|
||||||
image.src = link;
|
});
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user