import React, { useState, useEffect } from 'react'; import { useIntl, FormattedMessage, defineMessages } from 'react-intl'; import { patchMe } from 'soapbox/actions/me'; import { FE_NAME, SETTINGS_UPDATE, changeSetting } from 'soapbox/actions/settings'; import List, { ListItem } from 'soapbox/components/list'; import { CardHeader, CardTitle, Column, Button, Form, FormActions, FormGroup, Textarea, } from 'soapbox/components/ui'; import SettingToggle from 'soapbox/features/notifications/components/setting-toggle'; import { useAppSelector, useAppDispatch, useSettings } from 'soapbox/hooks'; import toast from 'soapbox/toast'; const isJSONValid = (text: any): boolean => { try { JSON.parse(text); return true; } catch { return false; } }; const messages = defineMessages({ heading: { id: 'column.settings_store', defaultMessage: 'Settings store' }, advanced: { id: 'developers.settings_store.advanced', defaultMessage: 'Advanced settings' }, hint: { id: 'developers.settings_store.hint', defaultMessage: 'It is possible to directly edit your user settings here. BE CAREFUL! Editing this section can break your account, and you will only be able to recover through the API.' }, }); const SettingsStore: React.FC = () => { const intl = useIntl(); const dispatch = useAppDispatch(); const settings = useSettings(); const settingsStore = useAppSelector(state => state.settings); const [rawJSON, setRawJSON] = useState(JSON.stringify(settingsStore, null, 2)); const [jsonValid, setJsonValid] = useState(true); const [isLoading, setLoading] = useState(false); const handleEditJSON: React.ChangeEventHandler = ({ target }) => { const rawJSON = target.value; setRawJSON(rawJSON); setJsonValid(isJSONValid(rawJSON)); }; const onToggleChange = (key: string[], checked: boolean) => { dispatch(changeSetting(key, checked, { showAlert: true })); }; const handleSubmit: React.FormEventHandler = e => { const settings = JSON.parse(rawJSON); setLoading(true); dispatch(patchMe({ pleroma_settings_store: { [FE_NAME]: settings, }, })).then(response => { dispatch({ type: SETTINGS_UPDATE, settings }); setLoading(false); }).catch(error => { toast.showAlertForError(error); setLoading(false); }); }; useEffect(() => { setRawJSON(JSON.stringify(settingsStore, null, 2)); setJsonValid(true); }, [settingsStore]); return (