bigbuffet-rw/app/soapbox/actions/settings.ts

265 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-08-11 16:23:42 -07:00
import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet } from 'immutable';
import { defineMessage } from 'react-intl';
2022-01-10 14:01:24 -08:00
import { createSelector } from 'reselect';
2022-04-15 18:00:03 -07:00
import { v4 as uuid } from 'uuid';
2022-01-10 14:01:24 -08:00
import { patchMe } from 'soapbox/actions/me';
2021-11-08 18:21:41 -08:00
import messages from 'soapbox/locales/messages';
2022-12-20 07:47:46 -08:00
import toast from 'soapbox/toast';
2021-03-25 10:25:45 -07:00
import { isLoggedIn } from 'soapbox/utils/auth';
import type { AppDispatch, RootState } from 'soapbox/store';
2020-03-27 13:59:38 -07:00
2023-06-28 15:53:17 -07:00
const SETTING_CHANGE = 'SETTING_CHANGE' as const;
const SETTING_SAVE = 'SETTING_SAVE' as const;
const SETTINGS_UPDATE = 'SETTINGS_UPDATE' as const;
const FE_NAME = 'soapbox_fe';
/** Options when changing/saving settings. */
type SettingOpts = {
/** Whether to display an alert when settings are saved. */
showAlert?: boolean
}
const saveSuccessMessage = defineMessage({ id: 'settings.save.success', defaultMessage: 'Your preferences have been saved!' });
2022-03-21 11:09:01 -07:00
const defaultSettings = ImmutableMap({
2022-05-02 13:55:52 -07:00
onboarded: false,
2020-04-28 10:59:15 -07:00
skinTone: 1,
reduceMotion: false,
underlineLinks: false,
2020-05-28 13:43:17 -07:00
autoPlayGif: true,
2020-05-28 18:36:39 -07:00
displayMedia: 'default',
2020-04-28 10:59:15 -07:00
expandSpoilers: false,
unfollowModal: false,
boostModal: false,
deleteModal: true,
missingDescriptionModal: false,
2020-04-28 10:59:15 -07:00
defaultPrivacy: 'public',
defaultContentType: 'text/plain',
2022-05-04 06:35:45 -07:00
themeMode: 'system',
2021-11-08 18:21:41 -08:00
locale: navigator.language || 'en',
showExplanationBox: true,
explanationBox: true,
autoloadTimelines: true,
autoloadMore: true,
2020-04-28 10:59:15 -07:00
systemFont: false,
demetricator: false,
isDeveloper: false,
2020-08-25 12:58:35 -07:00
chats: ImmutableMap({
panes: ImmutableList(),
mainWindow: 'minimized',
sound: true,
2020-08-25 12:58:35 -07:00
}),
2020-04-28 10:59:15 -07:00
home: ImmutableMap({
shows: ImmutableMap({
reblog: true,
reply: true,
direct: false,
2020-04-28 10:59:15 -07:00
}),
regex: ImmutableMap({
body: '',
}),
}),
notifications: ImmutableMap({
alerts: ImmutableMap({
follow: true,
follow_request: false,
2020-04-28 10:59:15 -07:00
favourite: true,
reblog: true,
mention: true,
poll: true,
move: true,
'pleroma:emoji_reaction': true,
2020-04-28 10:59:15 -07:00
}),
quickFilter: ImmutableMap({
active: 'all',
show: true,
advanced: false,
}),
shows: ImmutableMap({
follow: true,
2022-04-16 17:03:33 -07:00
follow_request: true,
2020-04-28 10:59:15 -07:00
favourite: true,
reblog: true,
mention: true,
poll: true,
move: true,
'pleroma:emoji_reaction': true,
2020-04-28 10:59:15 -07:00
}),
sounds: ImmutableMap({
2020-05-18 17:43:58 -07:00
follow: false,
follow_request: false,
2020-05-18 17:43:58 -07:00
favourite: false,
reblog: false,
mention: false,
poll: false,
move: false,
'pleroma:emoji_reaction': false,
2020-04-28 10:59:15 -07:00
}),
birthdays: ImmutableMap({
show: true,
}),
2020-04-28 10:59:15 -07:00
}),
community: ImmutableMap({
2020-08-07 17:28:30 -07:00
shows: ImmutableMap({
reblog: false,
2020-08-07 17:28:30 -07:00
reply: true,
2021-12-12 20:34:10 -08:00
direct: false,
2020-08-07 17:28:30 -07:00
}),
2020-04-28 10:59:15 -07:00
other: ImmutableMap({
onlyMedia: false,
}),
regex: ImmutableMap({
body: '',
}),
}),
public: ImmutableMap({
2020-08-07 17:28:30 -07:00
shows: ImmutableMap({
reblog: true,
reply: true,
2021-12-12 20:34:10 -08:00
direct: false,
2020-08-07 17:28:30 -07:00
}),
2020-04-28 10:59:15 -07:00
other: ImmutableMap({
onlyMedia: false,
}),
regex: ImmutableMap({
body: '',
}),
}),
direct: ImmutableMap({
regex: ImmutableMap({
body: '',
}),
}),
account_timeline: ImmutableMap({
shows: ImmutableMap({
reblog: true,
pinned: true,
2021-12-12 20:34:10 -08:00
direct: false,
}),
}),
groups: ImmutableMap({}),
2020-04-28 10:59:15 -07:00
trends: ImmutableMap({
show: true,
}),
2020-09-27 17:43:42 -07:00
columns: ImmutableList([
ImmutableMap({ id: 'COMPOSE', uuid: uuid(), params: {} }),
ImmutableMap({ id: 'HOME', uuid: uuid(), params: {} }),
ImmutableMap({ id: 'NOTIFICATIONS', uuid: uuid(), params: {} }),
]),
2021-08-11 16:23:42 -07:00
remote_timeline: ImmutableMap({
pinnedHosts: ImmutableOrderedSet(),
}),
2020-04-28 10:59:15 -07:00
});
const getSettings = createSelector([
(state: RootState) => state.soapbox.get('defaultSettings'),
(state: RootState) => state.settings,
], (soapboxSettings, settings) => {
2020-04-28 10:59:15 -07:00
return defaultSettings
.mergeDeep(soapboxSettings)
.mergeDeep(settings);
});
2020-04-28 10:59:15 -07:00
2023-06-28 15:53:17 -07:00
interface SettingChangeAction {
type: typeof SETTING_CHANGE
path: string[]
value: any
}
const changeSettingImmediate = (path: string[], value: any, opts?: SettingOpts) =>
(dispatch: AppDispatch) => {
2023-06-28 15:53:17 -07:00
const action: SettingChangeAction = {
type: SETTING_CHANGE,
path,
value,
2023-06-28 15:53:17 -07:00
};
2023-06-28 15:53:17 -07:00
dispatch(action);
dispatch(saveSettingsImmediate(opts));
};
const changeSetting = (path: string[], value: any, opts?: SettingOpts) =>
(dispatch: AppDispatch) => {
2023-06-28 15:53:17 -07:00
const action: SettingChangeAction = {
2020-03-27 13:59:38 -07:00
type: SETTING_CHANGE,
path,
value,
2023-06-28 15:53:17 -07:00
};
2020-03-27 13:59:38 -07:00
2023-06-28 15:53:17 -07:00
dispatch(action);
return dispatch(saveSettings(opts));
2020-03-27 13:59:38 -07:00
};
const saveSettingsImmediate = (opts?: SettingOpts) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return;
const state = getState();
if (getSettings(state).getIn(['saved'])) return;
const data = state.settings.delete('saved').toJS();
dispatch(patchMe({
pleroma_settings_store: {
[FE_NAME]: data,
},
})).then(() => {
dispatch({ type: SETTING_SAVE });
2022-03-21 11:09:01 -07:00
if (opts?.showAlert) {
toast.success(saveSuccessMessage);
2022-06-22 10:17:35 -07:00
}
}).catch(error => {
2022-12-20 09:45:46 -08:00
toast.showAlertForError(error);
});
};
const saveSettings = (opts?: SettingOpts) =>
(dispatch: AppDispatch) => dispatch(saveSettingsImmediate(opts));
const getLocale = (state: RootState, fallback = 'en') => {
const localeWithVariant = (getSettings(state).get('locale') as string).replace('_', '-');
const locale = localeWithVariant.split('-')[0];
return Object.keys(messages).includes(localeWithVariant) ? localeWithVariant : Object.keys(messages).includes(locale) ? locale : fallback;
};
2023-06-28 15:53:17 -07:00
type SettingsAction =
| SettingChangeAction
| { type: typeof SETTING_SAVE }
export {
SETTING_CHANGE,
SETTING_SAVE,
SETTINGS_UPDATE,
FE_NAME,
defaultSettings,
getSettings,
changeSettingImmediate,
changeSetting,
saveSettingsImmediate,
saveSettings,
getLocale,
2023-06-28 15:53:17 -07:00
type SettingsAction,
};