import React, { useState, useEffect } from 'react' import { useContent } from './useContent.hook' import { Modal, Text, Switch, TextInput, Button } from '@mantine/core' import { IconSearch, IconMessagePlus, IconLabel } from '@tabler/icons-react' import classes from './Content.module.css' import { Channel } from '../channel/Channel' import { Card } from '../card/Card' import { modals } from '@mantine/modals' export function Content({ textCard }: { textCard: { cardId: null|string }}) { const { state, actions } = useContent() const [add, setAdd] = useState(false) const [adding, setAdding] = useState(false) const [sealed, setSealed] = useState(false) const [subject, setSubject] = useState('') const [added, setAdded] = useState([] as string[]) const cards = state.sealSet && sealed ? state.sealable : state.connected const openTopic = async (cardId: string) => { setAdding(true); try { const id = await actions.openTopic(cardId); actions.setFocus(null, id); } catch (err) { console.log(err); showError(); } setAdding(false); } const addTopic = async () => { setAdding(true) try { const id = await actions.addTopic( sealed, subject, added.filter((id) => Boolean(cards.find((card) => card.cardId === id))) ) actions.setFocus(null, id); setAdd(false) setSealed(false) setAdded([]) setSubject('') } catch (err) { console.log(err) showError() } setAdding(false) } const showError = () => { modals.openConfirmModal({ title: state.strings.operationFailed, withCloseButton: true, overlayProps: { backgroundOpacity: 0.55, blur: 3, }, children: {state.strings.tryAgain}, cancelProps: { display: 'none' }, confirmProps: { display: 'none' }, }) } const contacts = cards.map((card, idx) => { const enable = ( id === card.cardId))} onChange={(ev) => { if (ev.currentTarget.checked) { setAdded([...added, card.cardId]) } else { setAdded(added.filter((id) => id !== card.cardId)) } }} /> ) return }) const channels = state.filtered.map((channel, idx) => { return ( actions.setFocus(channel.cardId, channel.channelId)} /> ) }) useEffect(() => { if (textCard.cardId) { openTopic(textCard.cardId); } }, [textCard]); return (
} placeholder={state.strings.topics} value={state.filter} onChange={(event) => actions.setFilter(event.currentTarget.value)} /> {state.layout === 'small' && ( )}
{channels.length === 0 &&
{state.strings.noTopics}
} {channels.length !== 0 &&
{channels}
} {state.layout === 'large' && (
)} setAdd(false)} overlayProps={{ backgroundOpacity: 0.65, blur: 3 }} centered>
} placeholder={state.strings.subjectOptional} value={subject} onChange={(event) => setSubject(event.currentTarget.value)} />
{state.strings.members}
{cards.length === 0 && (
{state.strings.noContacts}
)} {contacts}
{state.sealSet && setSealed(ev.currentTarget.checked)} />}
) }