@@ -246,9 +243,8 @@ export function MarkdownInput ({ label, topLevel, groupClassName, onChange, setH
{
- setUploadedImages(prev => [...prev, { file, url }])
- }}
+ className='d-flex align-items-center me-1'
+ onSuccess={img => setUnsubmittedImages(prev => [...prev, img])}
>
@@ -274,8 +270,26 @@ export function MarkdownInput ({ label, topLevel, groupClassName, onChange, setH
onBlur={() => setTimeout(resetSuggestions, 100)}
/>)}
- {uploadedImages.map((props, i) => {
- return removeUploadedImage(i)} />
+ {unsubmittedImages.map((img, i) => {
+ return (
+ {
+ try {
+ await deleteImage({ variables: { id: img.id } })
+ toaster.success('image deleted')
+ } catch (err) {
+ console.error(err)
+ toaster.danger('failed to delete image')
+ }
+ }}
+ />
+ )
})}
{tab !== 'write' &&
diff --git a/components/image.js b/components/image.js
index 89bd63b6..22796ef5 100644
--- a/components/image.js
+++ b/components/image.js
@@ -1,5 +1,5 @@
import styles from './text.module.css'
-import { Fragment, useState, useEffect, useMemo, useCallback, useRef } from 'react'
+import { Fragment, useState, useEffect, useMemo, useCallback, useRef, createContext, useContext } from 'react'
import { IMGPROXY_URL_REGEXP } from '../lib/url'
import { useShowModal } from './modal'
import { useMe } from './me'
@@ -7,7 +7,67 @@ import { Dropdown } from 'react-bootstrap'
import { UPLOAD_TYPES_ALLOW } from '../lib/constants'
import { useToast } from './toast'
import gql from 'graphql-tag'
-import { useMutation } from '@apollo/client'
+import { useMutation, useQuery } from '@apollo/client'
+
+const ImageContext = createContext({ unsubmitted: [] })
+
+export function ImageProvider ({ me, children }) {
+ const { data, loading } = useQuery(
+ gql`
+ query images($submitted: Boolean) {
+ me {
+ images(submitted: $submitted) {
+ id
+ createdAt
+ updatedAt
+ type
+ size
+ width
+ height
+ itemId
+ }
+ }
+ }`, {
+ variables: { submitted: false }
+ }
+ )
+ const [deleteImage] = useMutation(gql`
+ mutation deleteImage($id: ID!) {
+ deleteImage(id: $id)
+ }`, {
+ onCompleted: (_, options) => {
+ const id = options.variables.id
+ setUnsubmittedImages(prev => prev.filter(img => img.id !== id))
+ }
+ })
+ const [unsubmittedImages, setUnsubmittedImages] = useState([])
+
+ const imageIdToUrl = id => `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/${id}`
+
+ useEffect(() => {
+ const images = data?.me?.images
+ if (images) {
+ setUnsubmittedImages(images.map(img => ({ ...img, url: imageIdToUrl(img.id) })))
+ }
+ }, [loading])
+
+ const contextValue = {
+ unsubmittedImages,
+ setUnsubmittedImages,
+ deleteImage
+ }
+
+ return (
+