Fix crash when toggling chat button plugins without restart

This commit is contained in:
Vendicated 2024-02-06 18:12:09 +01:00
parent de570a4800
commit 377def4a33
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
7 changed files with 13 additions and 16 deletions

View file

@ -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); );
}
} }
} }

View file

@ -65,7 +65,7 @@ function Indicator() {
} }
const ChatBarIcon: ChatBarButton = (_, isMainChat) => { const ChatBarIcon: ChatBarButton = ({ isMainChat }) => {
if (!isMainChat) return null; if (!isMainChat) return null;
return ( return (

View file

@ -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));

View file

@ -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 (

View file

@ -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) {

View file

@ -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;

View file

@ -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;