Skip to content

Commit

Permalink
feat: play sound on successful swaps (#536)
Browse files Browse the repository at this point in the history
* feat: play sound on successful swaps

closes #338

Update src/context/Global.tsx

Co-authored-by: michael1011 <[email protected]>

* fix: small es language fix

---------

Co-authored-by: michael1011 <[email protected]>
Co-authored-by: Kilian <[email protected]>
  • Loading branch information
3 people authored May 22, 2024
1 parent a4169fe commit caa861a
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 4 deletions.
Binary file added public/notification.mp3
Binary file not shown.
34 changes: 34 additions & 0 deletions src/components/AudioNotificationSetting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useGlobalContext } from "../context/Global";

const AudioNotificationSetting = () => {
const {
audioNotification,
setAudioNotification,
t,
playNotificationSound,
} = useGlobalContext();

const toggleAudioNotification = (evt: MouseEvent) => {
if (!audioNotification()) playNotificationSound();
setAudioNotification(!audioNotification());
evt.stopPropagation();
};

return (
<>
<div
class="audio toggle"
title={t("enable_audio_tooltip")}
onClick={toggleAudioNotification}>
<span class={audioNotification() ? "active" : ""}>
{t("on")}
</span>
<span class={!audioNotification() ? "active" : ""}>
{t("off")}
</span>
</div>
</>
);
};

export default AudioNotificationSetting;
7 changes: 7 additions & 0 deletions src/components/SettingsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IoClose } from "solid-icons/io";

import { useGlobalContext } from "../context/Global";
import "../style/settings.scss";
import AudioNotificationSetting from "./AudioNotificationSetting";
import Denomination from "./Denomination";
import Logs from "./Logs";
import Separator from "./Separator";
Expand Down Expand Up @@ -34,6 +35,12 @@ const SettingsMenu = () => {
<div class="spacer"></div>
<Separator />
</span>
<span class="setting">
<label>{t("enable_audio_notifications")}: </label>
<Tooltip label="enable_audio_notifications_tooltip" />
<div class="spacer"></div>
<AudioNotificationSetting />
</span>
<span class="setting">
<label>{t("logs")}: </label>
<Tooltip label="logs_tooltip" />
Expand Down
2 changes: 1 addition & 1 deletion src/components/SwapChecker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const SwapChecker = () => {
if (claimedSwap.id === swap().id) {
setSwap(claimedSwap);
}
notify("success", t("claim_success", { id: res.id }));
notify("success", t("claim_success", { id: res.id }), true);
} catch (e) {
const msg = t("claim_fail", { id: currentSwap.id });
log.warn(msg, e);
Expand Down
24 changes: 22 additions & 2 deletions src/context/Global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ export type GlobalContextType = {
setSeparator: Setter<string>;
settingsMenu: Accessor<boolean>;
setSettingsMenu: Setter<boolean>;
audioNotification: Accessor<boolean>;
setAudioNotification: Setter<boolean>;
// functions
t: (key: string, values?: Record<string, any>) => string;
notify: (type: string, message: string) => void;
notify: (type: string, message: string, audio?: boolean) => void;
playNotificationSound: () => void;
fetchPairs: (asset?: string) => void;

getLogs: () => Promise<Record<string, string[]>>;
Expand Down Expand Up @@ -153,6 +156,13 @@ const GlobalProvider = (props: { children: any }) => {

const [settingsMenu, setSettingsMenu] = createSignal<boolean>(false);

const [audioNotification, setAudioNotification] = makePersisted(
createSignal<boolean>(false),
{
name: "audioNotification",
},
);

const localeSeparator = (0.1).toLocaleString().charAt(1);
const [separator, setSeparator] = makePersisted(
createSignal(localeSeparator),
Expand All @@ -161,9 +171,16 @@ const GlobalProvider = (props: { children: any }) => {
},
);

const notify = (type: string, message: string) => {
const notify = (type: string, message: string, audio: boolean = false) => {
setNotificationType(type);
setNotification(message);
if (audio && audioNotification()) playNotificationSound();
};

const playNotificationSound = () => {
const audio = new Audio("/notification.mp3");
audio.volume = 0.3;
audio.play();
};

const fetchPairs = (asset: string = BTC) => {
Expand Down Expand Up @@ -326,9 +343,12 @@ const GlobalProvider = (props: { children: any }) => {
setSeparator,
settingsMenu,
setSettingsMenu,
audioNotification,
setAudioNotification,
// functions
t,
notify,
playNotificationSound,
fetchPairs,
getLogs,
clearLogs,
Expand Down
22 changes: 21 additions & 1 deletion src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ const dict = {
claim_fail: "Failed to claim swap: {{ id }}",
logs: "Logs",
logs_tooltip: "Logs of the web app, useful for debugging.",
enable_audio_notifications: "Enable Audio Notifications",
enable_audio_notifications_tooltip:
"Enable or disable audio notifications",
on: "on",
off: "off",
},
de: {
language: "Deutsch",
Expand Down Expand Up @@ -379,6 +384,11 @@ const dict = {
claim_fail: "Swap {{ id }} konnte nicht geclaimed werden!",
logs: "Logs",
logs_tooltip: "Logs der Web App, nützlich für Debugging.",
enable_audio_notifications: "Audio Benachrichtigungen aktivieren",
enable_audio_notifications_tooltip:
"Aktiviere oder deaktiviere Audio-Benachrichtigungen",
on: "an",
off: "aus",
},
es: {
language: "Español",
Expand Down Expand Up @@ -572,7 +582,13 @@ const dict = {
claim_success: "¡Intercambio {{ id }} reclamado con éxito!",
claim_fail: "¡Error en reclamar el intercambio {{ id }}!",
logs: "Logs",
logs_tooltip: "Registros de la aplicación web, para depuración.",
logs_tooltip:
"Registros de la aplicación web como herramienta de depuración.",
enable_audio_notifications: "Activar notificaciones de audio",
enable_audio_notifications_tooltip:
"Activar o desactivar notificaciones de audio",
on: "on",
off: "off",
},
zh: {
language: "中文",
Expand Down Expand Up @@ -747,6 +763,10 @@ const dict = {
claim_fail: "交换{{ id }}索赔失败!",
logs: "日志",
logs_tooltip: "网络应用程序的日志,用于调试。",
enable_audio_notifications: "启用音频通知",
enable_audio_notifications_tooltip: "启用或禁用音频通知",
on: "开",
off: "关",
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/style/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ textarea {
height: 28px;
border-radius: 50%;
user-select: none;
display: flex;
align-items: center;
justify-content: center;
}
> :first-child {
margin-right: 5px;
Expand Down
3 changes: 3 additions & 0 deletions src/style/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
display: flex;
align-items: center;
}
.toggle {
margin-left: 15px;
}
}
.setting {
display: flex;
Expand Down

0 comments on commit caa861a

Please sign in to comment.