import React, { FormEventHandler, ReactNode, useMemo, useState } from 'react'; import { Badge, Box, Button, Chip, Icon, Icons, Input, Text } from 'folds'; import { UsageSwitcher, useUsageStr } from './UsageSwitcher'; import { mxcUrlToHttp } from '../../utils/matrix'; import * as css from './style.css'; import { ImageUsage, imageUsageEqual, PackImageReader } from '../../plugins/custom-emoji'; import { useMatrixClient } from '../../hooks/useMatrixClient'; import { SettingTile } from '../setting-tile'; import { useObjectURL } from '../../hooks/useObjectURL'; import { createUploadAtom, TUploadAtom } from '../../state/upload'; import { replaceSpaceWithDash } from '../../utils/common'; type ImageTileProps = { defaultShortcode: string; useAuthentication: boolean; packUsage: ImageUsage[]; image: PackImageReader; canEdit?: boolean; onEdit?: (defaultShortcode: string, image: PackImageReader) => void; deleted?: boolean; onDeleteToggle?: (defaultShortcode: string) => void; }; export function ImageTile({ defaultShortcode, image, packUsage, useAuthentication, canEdit, onEdit, onDeleteToggle, deleted, }: ImageTileProps) { const mx = useMatrixClient(); const getUsageStr = useUsageStr(); return ( } title={ deleted ? ( {image.shortcode} ) : ( image.shortcode ) } description={ {image.usage && getUsageStr(image.usage) !== getUsageStr(packUsage) && ( {getUsageStr(image.usage)} )} {image.body} } after={ canEdit ? ( onDeleteToggle?.(defaultShortcode)} > {deleted ? Undo : } {!deleted && ( onEdit?.(defaultShortcode, image)} > Edit )} ) : undefined } /> ); } type ImageTileUploadProps = { file: File; children: (uploadAtom: TUploadAtom) => ReactNode; }; export function ImageTileUpload({ file, children }: ImageTileUploadProps) { const url = useObjectURL(file); const uploadAtom = useMemo(() => createUploadAtom(file), [file]); return ( }> {children(uploadAtom)} ); } type ImageTileEditProps = { defaultShortcode: string; useAuthentication: boolean; packUsage: ImageUsage[]; image: PackImageReader; onCancel: (shortcode: string) => void; onSave: (shortcode: string, image: PackImageReader) => void; }; export function ImageTileEdit({ defaultShortcode, useAuthentication, packUsage, image, onCancel, onSave, }: ImageTileEditProps) { const mx = useMatrixClient(); const defaultUsage = image.usage ?? packUsage; const [unsavedUsage, setUnsavedUsages] = useState(defaultUsage); const handleSubmit: FormEventHandler = (evt) => { evt.preventDefault(); const target = evt.target as HTMLFormElement | undefined; const shortcodeInput = target?.shortcodeInput as HTMLInputElement | undefined; const bodyInput = target?.bodyInput as HTMLTextAreaElement | undefined; if (!shortcodeInput || !bodyInput) return; const shortcode = replaceSpaceWithDash(shortcodeInput.value.trim()); const body = bodyInput.value.trim() || undefined; const usage = unsavedUsage; if (!shortcode) return; if ( shortcode === image.shortcode && body === image.body && imageUsageEqual(usage, defaultUsage) ) { onCancel(defaultShortcode); return; } const imageReader = new PackImageReader(shortcode, image.url, { info: image.info, body, usage: imageUsageEqual(usage, packUsage) ? undefined : usage, }); onSave(defaultShortcode, imageReader); }; return ( } > Shortcode:} defaultValue={image.shortcode} name="shortcodeInput" variant="Secondary" size="300" radii="0" required autoFocus /> Body:} defaultValue={image.body} name="bodyInput" variant="Secondary" size="300" radii="0" /> ); }