import {InternalModal} from "tc-shared/ui/react-elements/internal-modal/Controller"; import * as React from "react"; import {Translatable} from "tc-shared/ui/react-elements/i18n"; import {Registry} from "tc-shared/events"; import { ChannelEditableProperty, ChannelEditEvents, ChannelPropertyPermission } from "tc-shared/ui/modal/channel-edit/Definitions"; import {useContext, useState} from "react"; import {BoxedInputField} from "tc-shared/ui/react-elements/InputField"; const cssStyle = require("./Renderer.scss"); const ModalTypeContext = React.createContext<"channel-edit" | "channel-create">("channel-edit"); const EventContext = React.createContext>(undefined); type ChannelPropertyState = { setPropertyValue: (value: ChannelEditableProperty[T]) => void } & ({ propertyState: "loading", propertyValue: undefined, } | { propertyState: "normal" | "applying", propertyValue: ChannelEditableProperty[T], }) const kPropertyLoading = "____loading_____"; function useProperty(property: T) : ChannelPropertyState { const events = useContext(EventContext); const [ value, setValue ] = useState(() => { events.fire("query_property", { property: property }); return kPropertyLoading; }); events.reactUse("notify_property", event => { if(event.property !== property) { return; } setValue(event.value as any); }, undefined, []); if(value === kPropertyLoading) { return { propertyState: "loading", propertyValue: undefined, setPropertyValue: _value => {} }; } else { return { propertyState: "normal", propertyValue: value, setPropertyValue: setValue as any }; } } function usePermission(permission: T, defaultValue: ChannelPropertyPermission[T]) : ChannelPropertyPermission[T] { const events = useContext(EventContext); const [ value, setValue ] = useState(() => { events.fire("query_property_permission", { permission: permission }); return defaultValue; }); events.reactUse("notify_property_permission", event => event.permission === permission && setValue(event.value as any)); return value; } const ChannelName = () => { const modalType = useContext(ModalTypeContext); const { propertyValue, propertyState, setPropertyValue } = useProperty("name"); const editable = usePermission("name", modalType === "channel-create"); const [ edited, setEdited ] = useState(false); return ( { setPropertyValue(value); setEdited(true); }} isInvalid={edited && (typeof propertyValue !== "string" || !propertyValue || propertyValue.length > 30)} /> ); } const GeneralContainer = () => { return (
); } export class ChannelEditModal extends InternalModal { private readonly events: Registry; private readonly isChannelCreate: boolean; constructor(events: Registry, isChannelCreate: boolean) { super(); this.events = events; this.isChannelCreate = isChannelCreate; } renderBody(): React.ReactElement { return ( ); } title(): string | React.ReactElement { return Create channel; } }