Skip to content

Commit

Permalink
Merge pull request #7422 from gitbutlerapp/e-branch-88
Browse files Browse the repository at this point in the history
web: Play sound on chat notifications
  • Loading branch information
estib-vega authored Feb 26, 2025
2 parents afa1f31 + ca59a8e commit 50c6455
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
18 changes: 15 additions & 3 deletions apps/web/src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import '$home/styles/styles.css';
import { env } from '$env/dynamic/public';
const CHAT_NOTFICATION_SOUND = '/sounds/pop.mp3';
interface Props {
children: Snippet;
}
Expand All @@ -50,16 +52,26 @@
setContext(NewUserService, newUserService);
const branchService = new BranchService(httpClient, appState.appDispatch);
setContext(BranchService, branchService);
const patchSerice = new PatchService(httpClient, appState.appDispatch);
setContext(PatchService, patchSerice);
const patchService = new PatchService(httpClient, appState.appDispatch);
setContext(PatchService, patchService);
const patchEventsService = new PatchEventsService(
httpClient,
appState,
appState.appDispatch,
authService.tokenReadable,
patchSerice,
patchService,
env.PUBLIC_APP_HOST
);
const user = $derived(userService.user);
$effect(() => {
if ($user) {
patchEventsService.setUserId($user.id);
patchEventsService.setChatSoundUrl(CHAT_NOTFICATION_SOUND);
}
});
setContext(PatchEventsService, patchEventsService);
const chatChannelService = new ChatChannelsService(httpClient, appState.appDispatch);
setContext(ChatChannelsService, chatChannelService);
Expand Down
Binary file added apps/web/static/sounds/pop.mp3
Binary file not shown.
Binary file added apps/web/static/sounds/vintage.wav
Binary file not shown.
23 changes: 23 additions & 0 deletions packages/shared/src/lib/branches/patchEventsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '$lib/branches/types';
import { InterestStore, type Interest } from '$lib/interest/interestStore';
import { errorToLoadable, isFound } from '$lib/network/loadable';
import { playSound } from '$lib/sounds';
import { asyncToSyncSignals, writableDerived } from '$lib/storeUtils';
import { createConsumer } from '@rails/actioncable';
import { type Readable } from 'svelte/store';
Expand All @@ -30,6 +31,8 @@ function getActionCableEndpoint(token: string | undefined, baseUrl: string): str
}

export class PatchEventsService {
private userId: number | undefined;
private chatSoundUrl: string | undefined;
private readonly patchEventsInterestStore = new InterestStore<{
changeId: string;
projectId: string;
Expand All @@ -44,6 +47,14 @@ export class PatchEventsService {
private readonly websocketBase: string
) {}

setUserId(userId: number) {
this.userId = userId;
}

setChatSoundUrl(chatSoundUrl: string) {
this.chatSoundUrl = chatSoundUrl;
}

patchEventsInterest(projectId: string, changeId: string): Interest {
// Using writableDerived over derived because derived's start stop
// notifier doesn't behave as expected, and doesn't runs top even
Expand Down Expand Up @@ -84,6 +95,14 @@ export class PatchEventsService {
.createInterest();
}

private shouldPlayChatSound(patchEvent: PatchEvent): boolean {
if (patchEvent.user?.id === undefined || this.userId === undefined) {
return false;
}

return patchEvent.user.id !== this.userId && patchEvent.eventType === 'chat';
}

private handlePatchEventData(projectId: string, changeId: string, data: ApiPatchEvent) {
const key = createPatchEventChannelKey(projectId, changeId);
const eventChannel = patchEventsSelectors.selectById(this.appState.patchEvents, key);
Expand All @@ -104,6 +123,10 @@ export class PatchEventsService {
if (data.event_type === 'patch_version' || data.event_type === 'issue_status') {
this.patchService.refreshPatchWithSections(changeId);
}

if (this.shouldPlayChatSound(patchEvent) && this.chatSoundUrl) {
playSound(this.chatSoundUrl);
}
}

private async fetchInitialPatchEvents(projectId: string, changeId: string) {
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/lib/sounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function playSound(soundUrl: string) {
const audio = new Audio(soundUrl);
audio.play();
}

0 comments on commit 50c6455

Please sign in to comment.