-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3834 from Emurgo/feat/YOEXT-1689/notifications-se…
…ttings feat(settings): add notifications setting
- Loading branch information
Showing
6 changed files
with
185 additions
and
4 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/yoroi-extension/app/UI/components/Switch/Switch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as React from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { Switch as MuiSwitch } from '@mui/material'; | ||
|
||
const height = 31; | ||
const width = 51; | ||
|
||
export const Switch: any = styled((props: any) => ( | ||
<MuiSwitch focusVisibleClassName=".Mui-focusVisible" disableRipple {...props} /> | ||
))(({ theme }) => ({ | ||
width, | ||
height, | ||
padding: 0, | ||
'& .MuiSwitch-switchBase': { | ||
padding: 0, | ||
margin: 3, | ||
transitionDuration: '300ms', | ||
'&.Mui-checked': { | ||
transform: 'translateX(20px)', | ||
color: '#fff', | ||
'& + .MuiSwitch-track': { | ||
backgroundColor: theme.palette.primary[500], | ||
opacity: 1, | ||
border: 0, | ||
}, | ||
'&.Mui-disabled + .MuiSwitch-track': { | ||
opacity: 0.5, | ||
}, | ||
}, | ||
'&.Mui-focusVisible .MuiSwitch-thumb': { | ||
border: '6px solid #fff', | ||
}, | ||
'&.Mui-disabled .MuiSwitch-thumb': { | ||
color: theme.palette.grey[100], | ||
}, | ||
'&.Mui-disabled + .MuiSwitch-track': { | ||
opacity: 0.7, | ||
}, | ||
}, | ||
'& .MuiSwitch-thumb': { | ||
boxSizing: 'border-box', | ||
width: '25px', | ||
height: '25px' | ||
}, | ||
'& .MuiSwitch-track': { | ||
borderRadius: height / 2, | ||
backgroundColor: theme.palette.grayscale[100], | ||
opacity: 1, | ||
transition: theme.transitions.create(['background-color'], { | ||
duration: 500, | ||
}), | ||
}, | ||
})); |
28 changes: 28 additions & 0 deletions
28
packages/yoroi-extension/app/UI/features/notifications/common/hooks/useStrings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import { defineMessages } from 'react-intl'; | ||
import { useIntl } from '../../../../context/IntlProvider'; | ||
|
||
export const messages = Object.freeze( | ||
defineMessages({ | ||
notifSettingsTitle: { | ||
id: 'notifications.settings.title', | ||
defaultMessage: '!!!In-app notifications', | ||
}, | ||
notifSettingsDesc: { | ||
id: 'notifications.settings.description', | ||
defaultMessage: | ||
'!!!Allow display of in-app notifications for key transactions', | ||
}, | ||
}) | ||
); | ||
|
||
export const useStrings = (intl = null) => { | ||
const { intl: contextIntl } = useIntl(); | ||
|
||
const i = intl || contextIntl; | ||
|
||
return React.useRef({ | ||
notifSettingsTitle: i.formatMessage(messages.notifSettingsTitle), | ||
notifSettingsDesc: i.formatMessage(messages.notifSettingsDesc), | ||
}).current; | ||
}; |
81 changes: 81 additions & 0 deletions
81
...on/app/UI/features/notifications/useCases/NotificationsSettings/NotificationsSettings.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as React from 'react'; | ||
import { Box, FormControlLabel, Typography } from '@mui/material'; | ||
import { useStrings } from '../../common/hooks/useStrings'; | ||
import { Switch } from '../../../../components/Switch/Switch'; | ||
import LocalStorageApi from '../../../../../api/localStorage'; | ||
import { ampli } from '../../../../../../ampli'; | ||
|
||
const NotificationsSettings = ({ intl }) => { | ||
const strings = useStrings(intl); | ||
const [notificationsEnabled, setNotificationsEnabled] = React.useState(true); | ||
const [selectedWalletId, setSelectedWalletId] = React.useState(""); | ||
|
||
const lsApi = new LocalStorageApi(); | ||
|
||
async function getNotificationsSetting(checkCurrentWallet: boolean = false) { | ||
const notifSettingsStr = await lsApi.getNotificationsSetting(); | ||
const notifSettings = JSON.parse(notifSettingsStr || "{}"); | ||
|
||
if (checkCurrentWallet) { | ||
const selectedWalletId = await lsApi.getSelectedWalletId(); | ||
setSelectedWalletId(selectedWalletId); | ||
|
||
return notifSettings[selectedWalletId] !== undefined ? notifSettings[selectedWalletId] : true; | ||
} | ||
|
||
return notifSettings; | ||
} | ||
|
||
async function setNotificationsSetting(enabled: boolean) { | ||
const notifSettings = await getNotificationsSetting(); | ||
lsApi.setNotificationsSetting(JSON.stringify({ ...notifSettings, [selectedWalletId]: enabled })); | ||
} | ||
|
||
// get initial state from localstorage | ||
React.useEffect(() => { | ||
async function initialNotifStatus() { | ||
const notifEnabled = await getNotificationsSetting(true); | ||
setNotificationsEnabled(notifEnabled); | ||
} | ||
|
||
initialNotifStatus(); | ||
}, []) | ||
|
||
// handle checkbox change event | ||
const handleNotificationsChange = async (event) => { | ||
const enabled = event.target.checked; | ||
setNotificationsEnabled(enabled); | ||
setNotificationsSetting(enabled); | ||
ampli.settingsInAppNotificationsStatusUpdated({ | ||
status: event.target.checked ? "enabled" : "disabled" | ||
}) | ||
} | ||
|
||
return ( | ||
<Box mb="40px"> | ||
<Box> | ||
<Typography variant='body1' fontWeight={500} color="ds.text_gray_medium">{strings.notifSettingsTitle}</Typography> | ||
</Box> | ||
<FormControlLabel | ||
label={strings.notifSettingsDesc} | ||
control={ | ||
<Box sx={{ alignSelf: 'flex-start' }}> | ||
<Switch | ||
checked={notificationsEnabled} | ||
onChange={handleNotificationsChange} | ||
/> | ||
</Box> | ||
} | ||
labelPlacement="top" | ||
sx={{ | ||
mt: '16px', | ||
marginLeft: '0px', | ||
color: 'ds.text_gray_medium', | ||
gap: '16px' | ||
}} | ||
/> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default NotificationsSettings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters