Fix crash when toggling chat button plugins without restart
This commit is contained in:
parent
de570a4800
commit
377def4a33
7 changed files with 13 additions and 16 deletions
|
@ -74,7 +74,7 @@ export interface ChatBarProps {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ChatBarButton = (props: ChatBarProps, isMainChat: boolean) => ReactNode;
|
export type ChatBarButton = (props: ChatBarProps & { isMainChat: boolean; }) => JSX.Element | null;
|
||||||
|
|
||||||
const buttonFactories = new Map<string, ChatBarButton>();
|
const buttonFactories = new Map<string, ChatBarButton>();
|
||||||
const logger = new Logger("ChatButtons");
|
const logger = new Logger("ChatButtons");
|
||||||
|
@ -82,13 +82,12 @@ const logger = new Logger("ChatButtons");
|
||||||
export function _injectButtons(buttons: ReactNode[], props: ChatBarProps) {
|
export function _injectButtons(buttons: ReactNode[], props: ChatBarProps) {
|
||||||
if (props.disabled) return;
|
if (props.disabled) return;
|
||||||
|
|
||||||
for (const [key, makeButton] of buttonFactories) {
|
for (const [key, Button] of buttonFactories) {
|
||||||
try {
|
buttons.push(
|
||||||
const res = makeButton(props, props.type.analyticsName === "normal");
|
<ErrorBoundary noop key={key} onError={e => logger.error(`Failed to render ${key}`, e.error)}>
|
||||||
if (res) buttons.push(res);
|
<Button {...props} isMainChat={props.type.analyticsName === "normal"} />
|
||||||
} catch (e) {
|
</ErrorBoundary>
|
||||||
logger.error(`Failed to render button ${key}`, e);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ function Indicator() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ChatBarIcon: ChatBarButton = (_, isMainChat) => {
|
const ChatBarIcon: ChatBarButton = ({ isMainChat }) => {
|
||||||
if (!isMainChat) return null;
|
if (!isMainChat) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -73,9 +73,7 @@ const getAttachments = async (channelId: string) =>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
const PreviewButton: ChatBarButton = (props, isMainChat) => {
|
const PreviewButton: ChatBarButton = ({ isMainChat, isEmpty, type: { attachments } }) => {
|
||||||
const { isEmpty, type: { attachments } } = props;
|
|
||||||
|
|
||||||
const channelId = SelectedChannelStore.getChannelId();
|
const channelId = SelectedChannelStore.getChannelId();
|
||||||
const draft = useStateFromStores([DraftStore], () => getDraft(channelId));
|
const draft = useStateFromStores([DraftStore], () => getDraft(channelId));
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ function PickerModal({ rootProps, close }: { rootProps: ModalProps, close(): voi
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ChatBarIcon: ChatBarButton = (_, isMainChat) => {
|
const ChatBarIcon: ChatBarButton = ({ isMainChat }) => {
|
||||||
if (!isMainChat) return null;
|
if (!isMainChat) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -41,7 +41,7 @@ const settings = definePluginSettings({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const SilentMessageToggle: ChatBarButton = (_, isMainChat) => {
|
const SilentMessageToggle: ChatBarButton = ({ isMainChat }) => {
|
||||||
const [enabled, setEnabled] = useState(lastState);
|
const [enabled, setEnabled] = useState(lastState);
|
||||||
|
|
||||||
function setEnabledValue(value: boolean) {
|
function setEnabledValue(value: boolean) {
|
||||||
|
|
|
@ -37,7 +37,7 @@ const settings = definePluginSettings({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const SilentTypingToggle: ChatBarButton = (_, isMainChat) => {
|
const SilentTypingToggle: ChatBarButton = ({ isMainChat }) => {
|
||||||
const { isEnabled, showIcon } = settings.use(["isEnabled", "showIcon"]);
|
const { isEnabled, showIcon } = settings.use(["isEnabled", "showIcon"]);
|
||||||
const toggle = () => settings.store.isEnabled = !settings.store.isEnabled;
|
const toggle = () => settings.store.isEnabled = !settings.store.isEnabled;
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ export function TranslateIcon({ height = 24, width = 24, className }: { height?:
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TranslateChatBarIcon: ChatBarButton = (props, isMainChat) => {
|
export const TranslateChatBarIcon: ChatBarButton = ({ isMainChat }) => {
|
||||||
const { autoTranslate } = settings.use(["autoTranslate"]);
|
const { autoTranslate } = settings.use(["autoTranslate"]);
|
||||||
|
|
||||||
if (!isMainChat) return null;
|
if (!isMainChat) return null;
|
||||||
|
|
Loading…
Reference in a new issue