import React, { useContext, useState, useEffect } from "react"; import SelectSearch from "react-select-search"; import { getZulipMessage, sendZulipMessage } from "../../../lib/zulip"; import { GetTranscript, GetTranscriptTopic } from "../../../api"; import "react-select-search/style.css"; import { DomainContext } from "../../../domainContext"; type ShareModal = { show: boolean; setShow: (show: boolean) => void; transcript: GetTranscript | null; topics: GetTranscriptTopic[] | null; }; interface Stream { id: number; name: string; topics: string[]; } interface SelectSearchOption { name: string; value: string; } const ShareModal = (props: ShareModal) => { const [stream, setStream] = useState(undefined); const [topic, setTopic] = useState(undefined); const [includeTopics, setIncludeTopics] = useState(false); const [isLoading, setIsLoading] = useState(true); const [streams, setStreams] = useState([]); const { zulip_streams } = useContext(DomainContext); useEffect(() => { fetch(zulip_streams + "/streams.json") .then((response) => { if (!response.ok) { throw new Error("Network response was not ok"); } return response.json(); }) .then((data) => { data = data.sort((a: Stream, b: Stream) => a.name.localeCompare(b.name), ); setStreams(data); setIsLoading(false); // data now contains the JavaScript object decoded from JSON }) .catch((error) => { console.error("There was a problem with your fetch operation:", error); }); }, []); const handleSendToZulip = () => { if (!props.transcript) return; const msg = getZulipMessage(props.transcript, props.topics, includeTopics); if (stream && topic) sendZulipMessage(stream, topic, msg); }; if (props.show && isLoading) { return
Loading...
; } let streamOptions: SelectSearchOption[] = []; if (streams) { streams.forEach((stream) => { const value = stream.name; streamOptions.push({ name: value, value: value }); }); } return (
{props.show && (

Send to Zulip

{/* Checkbox for 'Include Topics' */}
# { setTopic(undefined); setStream(val.toString()); }} placeholder="Pick a stream" />
{stream && ( <>
# s.name == stream) ?.topics.sort((a: string, b: string) => a.localeCompare(b), ) .map((t) => ({ name: t, value: t })) || [] } value={topic} onChange={(val) => setTopic(val.toString())} placeholder="Pick a topic" />
)}
)}
); }; export default ShareModal;