Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Notification Sound Feature #185

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/notification.wav
Binary file not shown.
11 changes: 11 additions & 0 deletions src/components/DeviceSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
PlusIcon,
SunIcon,
TerminalIcon,
BellIcon,
BellOffIcon,
} from "lucide-react";

export const DeviceSelector = (): JSX.Element => {
Expand All @@ -20,6 +22,8 @@ export const DeviceSelector = (): JSX.Element => {
setSelectedDevice,
darkMode,
setDarkMode,
notifications,
setNotifications,
setCommandPaletteOpen,
setConnectDialogOpen,
} = useAppStore();
Expand Down Expand Up @@ -61,6 +65,13 @@ export const DeviceSelector = (): JSX.Element => {
</ul>
</div>
<div className="flex w-20 flex-col items-center space-y-5 bg-transparent px-5 pb-5">
<button
type="button"
className="transition-all hover:text-accent"
onClick={() => setNotifications(!notifications)}
>
{notifications ? <BellIcon /> : <BellOffIcon />}
</button>
<button
type="button"
className="transition-all hover:text-accent"
Expand Down
10 changes: 10 additions & 0 deletions src/core/stores/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface AppState {
rasterSources: RasterSource[];
commandPaletteOpen: boolean;
darkMode: boolean;
notifications: boolean;
accent: AccentColor;
connectDialogOpen: boolean;

Expand All @@ -38,6 +39,7 @@ interface AppState {
removeDevice: (deviceId: number) => void;
setCommandPaletteOpen: (open: boolean) => void;
setDarkMode: (enabled: boolean) => void;
setNotifications: (enabled: boolean) => void;
setAccent: (color: AccentColor) => void;
setConnectDialogOpen: (open: boolean) => void;
}
Expand All @@ -49,6 +51,7 @@ export const useAppStore = create<AppState>()((set) => ({
rasterSources: [],
commandPaletteOpen: false,
darkMode: window.matchMedia("(prefers-color-scheme: dark)").matches,
notifications: true,
accent: "orange",
connectDialogOpen: false,

Expand Down Expand Up @@ -99,6 +102,13 @@ export const useAppStore = create<AppState>()((set) => ({
}),
);
},
setNotifications: (enabled: boolean) => {
set(
produce<AppState>((draft) => {
draft.notifications = enabled;
}),
);
},
setAccent(color) {
set(
produce<AppState>((draft) => {
Expand Down
4 changes: 4 additions & 0 deletions src/core/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Device } from "@core/stores/deviceStore.js";
import { Protobuf, Types } from "@meshtastic/js";
import { playNotificationSound } from "./utils/notify";

export const subscribeAll = (
device: Device,
Expand Down Expand Up @@ -84,6 +85,9 @@ export const subscribeAll = (
...messagePacket,
state: messagePacket.from !== myNodeNum ? "ack" : "waiting",
});
if(messagePacket.from !== myNodeNum){
playNotificationSound();
}
});

connection.events.onPendingSettingsChange.subscribe((state) => {
Expand Down
17 changes: 17 additions & 0 deletions src/core/utils/notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useAppStore } from '@core/stores/appStore.js';

const notificationSound = new Audio("/notification.wav"); //change sound if needed

let isPlaying = false;

export const playNotificationSound = () => {
const { notifications } = useAppStore.getState();
if (notifications && !isPlaying) {
isPlaying = true;
notificationSound.play();

notificationSound.onended = () => {
isPlaying = false;
};
}
};
Loading