From 82122ffe4526044410a89683ad879035ba41a87f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 23 Jun 2022 17:53:04 -0500 Subject: [PATCH] Snackbar: refactor to use opts object --- app/soapbox/actions/snackbar.ts | 20 +++++++++++--------- app/soapbox/main.tsx | 13 +++++++++++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/soapbox/actions/snackbar.ts b/app/soapbox/actions/snackbar.ts index 485537ea80..071c61c4fc 100644 --- a/app/soapbox/actions/snackbar.ts +++ b/app/soapbox/actions/snackbar.ts @@ -15,29 +15,31 @@ export type SnackbarAction = { severity: SnackbarActionSeverity, }; -export const show = ( - severity: SnackbarActionSeverity, - message: SnackbarMessage, +type SnackbarOpts = { actionLabel?: SnackbarMessage, actionLink?: string, action?: () => void, +}; + +export const show = ( + severity: SnackbarActionSeverity, + message: SnackbarMessage, + opts?: SnackbarOpts, ): SnackbarAction => ({ type: ALERT_SHOW, message, - actionLabel, - actionLink, severity, - action, + ...opts, }); export const info = (message: SnackbarMessage, actionLabel?: SnackbarMessage, actionLink?: string) => - show('info', message, actionLabel, actionLink); + show('info', message, { actionLabel, actionLink }); export const success = (message: SnackbarMessage, actionLabel?: SnackbarMessage, actionLink?: string) => - show('success', message, actionLabel, actionLink); + show('success', message, { actionLabel, actionLink }); export const error = (message: SnackbarMessage, actionLabel?: SnackbarMessage, actionLink?: string) => - show('error', message, actionLabel, actionLink); + show('error', message, { actionLabel, actionLink }); export default { info, diff --git a/app/soapbox/main.tsx b/app/soapbox/main.tsx index 862bf771f9..2ae1212f05 100644 --- a/app/soapbox/main.tsx +++ b/app/soapbox/main.tsx @@ -4,6 +4,7 @@ import './precheck'; import * as OfflinePluginRuntime from '@lcdp/offline-plugin/runtime'; import React from 'react'; import ReactDOM from 'react-dom'; +import { defineMessages } from 'react-intl'; import snackbar from 'soapbox/actions/snackbar'; import * as BuildConfig from 'soapbox/build_config'; @@ -15,6 +16,11 @@ import * as monitoring from './monitoring'; import * as perf from './performance'; import ready from './ready'; +const messages = defineMessages({ + update: { id: 'sw.update', defaultMessage: 'Update' }, + updateText: { id: 'sw.update_text', defaultMessage: 'An update is available.' }, +}); + function main() { perf.start('main()'); @@ -36,8 +42,11 @@ function main() { // https://github.com/NekR/offline-plugin/pull/201#issuecomment-285133572 OfflinePluginRuntime.install({ onUpdateReady: function() { - store.dispatch(snackbar.show('info', 'An update is available.', 'Update', undefined, () => { - OfflinePluginRuntime.applyUpdate(); + store.dispatch(snackbar.show('info', messages.updateText, { + actionLabel: messages.update, + action: () => { + OfflinePluginRuntime.applyUpdate(); + }, })); }, onUpdated: function() {