diff --git a/src/js/components/Menu.tsx b/src/js/components/Menu.tsx index d66a4781..8ab15cb2 100644 --- a/src/js/components/Menu.tsx +++ b/src/js/components/Menu.tsx @@ -15,6 +15,8 @@ import { } from '@heroicons/react/24/solid'; import { Link } from 'preact-router'; +import CreateNoteForm from '@/components/create/CreateNoteForm'; + import Icons from '../Icons'; import localState from '../LocalState'; import Key from '../nostr/Key'; @@ -24,7 +26,6 @@ import Show from './helpers/Show'; import Modal from './modal/Modal'; import Avatar from './user/Avatar'; import Name from './user/Name'; -import PublicMessageForm from './PublicMessageForm'; const MENU_ITEMS = [ { url: '/', text: 'home', icon: HomeIcon, activeIcon: HomeIconFull }, @@ -80,7 +81,7 @@ export default function Menu() { const renderNewPostModal = () => ( setShowNewPostModal(false)}> - setShowNewPostModal(false)} placeholder={t('whats_on_your_mind')} autofocus={true} diff --git a/src/js/components/PublicMessageForm.tsx b/src/js/components/PublicMessageForm.tsx deleted file mode 100644 index cb520f56..00000000 --- a/src/js/components/PublicMessageForm.tsx +++ /dev/null @@ -1,436 +0,0 @@ -import { html } from 'htm/preact'; -import $ from 'jquery'; -import { Event } from 'nostr-tools'; -import { createRef } from 'preact'; - -import { uploadFile } from '@/utils/uploadFile'; - -import Component from '../BaseComponent'; -import Helpers from '../Helpers'; -import Icons from '../Icons'; -import localState from '../LocalState'; -import Events from '../nostr/Events'; -import Key from '../nostr/Key'; -import { translate as t } from '../translations/Translation.mjs'; - -import SafeImg from './SafeImg'; -import SearchBox from './SearchBox'; -import Torrent from './Torrent'; - -const mentionRegex = /\B@[\u00BF-\u1FFF\u2C00-\uD7FF\w]*$/; - -interface IProps { - replyingTo?: string; - forceAutofocusMobile?: boolean; - autofocus?: boolean; - onSubmit?: (msg: any) => void; - waitForFocus?: boolean; - class?: string; - index?: string; - placeholder?: string; -} - -interface IState { - attachments?: any[]; - torrentId?: string; - mentioning?: boolean; - focused?: boolean; -} - -class PublicMessageForm extends Component { - newMsgRef = createRef(); - - componentDidMount() { - const textEl = $(this.newMsgRef.current); - if ( - (!Helpers.isMobile || this.props.forceAutofocusMobile == true) && - this.props.autofocus !== false - ) { - textEl.focus(); - } - if (!this.props.replyingTo) { - localState - .get('channels') - .get('public') - .get('msgDraft') - .once((t) => !textEl.val() && textEl.val(t)); - } else { - const currentHistoryState = window.history.state; - if (currentHistoryState && currentHistoryState['replyTo' + this.props.replyingTo]) { - textEl.val(currentHistoryState['replyTo' + this.props.replyingTo]); - } - } - } - - onMsgFormSubmit(event) { - event.preventDefault(); - this.submit(); - } - - async submit() { - if (!this.props.replyingTo) { - localState.get('channels').get('public').get('msgDraft').put(null); - } - const textEl = $(this.newMsgRef.current); - const text = textEl.val(); - if (!text.length) { - return; - } - const msg: any = { text }; - if (this.props.replyingTo) { - msg.replyingTo = this.props.replyingTo; - } - if (this.state.attachments) { - msg.attachments = this.state.attachments; - } - await this.sendNostr(msg); - this.props.onSubmit && this.props.onSubmit(msg); - this.setState({ attachments: undefined, torrentId: undefined }); - textEl.val(''); - textEl.height(''); - this.saveDraftToHistory(); - } - - setTextareaHeight(textarea) { - textarea.style.height = ''; - textarea.style.height = `${textarea.scrollHeight}px`; - } - - onMsgTextPaste(event) { - const clipboardData = event.clipboardData || window.clipboardData; - - // Handling magnet links - const pasted = clipboardData.getData('text'); - const magnetRegex = /(magnet:\?xt=urn:btih:.*)/gi; - const match = magnetRegex.exec(pasted); - console.log('magnet match', match); - if (match) { - this.setState({ torrentId: match[0] }); - } - - if (clipboardData.items) { - const items = clipboardData.items; - for (let i = 0; i < items.length; i++) { - if (items[i].type.startsWith('image/')) { - const blob = items[i].getAsFile(); - uploadFile( - blob, - (url) => { - const textEl = $(this.newMsgRef.current); - const currentVal = textEl.val(); - if (currentVal) { - textEl.val(currentVal + '\n\n' + url); - } else { - textEl.val(url); - } - }, - (errorMsg) => { - console.error(errorMsg); - }, - ); - } - } - } - } - - onKeyUp(e) { - if ([37, 38, 39, 40].indexOf(e.keyCode) != -1) { - this.checkMention(e); - } - } - - onKeyDown(e) { - if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') { - this.submit(); - } - } - - saveDraftToHistory() { - const text = $(this.newMsgRef.current).val(); - const currentHistoryState = window.history.state; - const newHistoryState = { - ...currentHistoryState, - }; - newHistoryState['replyTo' + this.props.replyingTo] = text; - window.history.replaceState(newHistoryState, ''); - } - - onMsgTextInput(event) { - this.setTextareaHeight(event.target); - if (!this.props.replyingTo) { - localState.get('channels').get('public').get('msgDraft').put($(event.target).val()); - } - this.checkMention(event); - this.saveDraftToHistory(); - } - - attachFileClicked(event) { - event.stopPropagation(); - event.preventDefault(); - $(this.base).find('.attachment-input').click(); - } - - attachmentsChanged(event) { - // TODO use Upload btn - const files = event.target.files || event.dataTransfer.files; - if (files) { - for (let i = 0; i < files.length; i++) { - const formData = new FormData(); - formData.append('fileToUpload', files[i]); - - const a = this.state.attachments || []; - a[i] = a[i] || { - type: files[i].type, - }; - - Helpers.getBase64(files[i]).then((base64) => { - a[i].data = base64; - this.setState({ attachments: a }); - }); - - fetch('https://nostr.build/api/upload/iris.php', { - method: 'POST', - body: formData, - }) - .then(async (response) => { - const url = await response.json(); - console.log('upload response', url); - if (url) { - a[i].url = url; - this.setState({ attachments: a }); - const textEl = $(this.newMsgRef.current); - const currentVal = textEl.val(); - if (currentVal) { - textEl.val(currentVal + '\n\n' + url); - } else { - textEl.val(url); - } - } - }) - .catch((error) => { - console.error('upload error', error); - a[i].error = 'upload failed'; - this.setState({ attachments: a }); - }); - } - $(event.target).val(null); - $(this.newMsgRef.current).focus(); - } - } - - onSelectMention(item) { - const textarea = $(this.base).find('textarea').get(0); - const pos = textarea.selectionStart; - const join = [ - textarea.value.slice(0, pos).replace(mentionRegex, 'nostr:'), - item.key, - textarea.value.slice(pos), - ].join(''); - textarea.value = `${join} `; - textarea.focus(); - textarea.selectionStart = textarea.selectionEnd = pos + item.key.length; - } - - render() { - const textareaPlaceholder = - this.props.placeholder || - (this.props.index === 'media' ? 'type_a_message_or_paste_a_magnet_link' : 'type_a_message'); - return html`
this.onMsgFormSubmit(e)} - > - this.attachmentsChanged(e)} - /> -