mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
Add calling and notification sounds (#119)
* Add calling and notification sounds * Fix notification visibility on any resolution * Add comments * Log howler error src * Revert removed files im stupid * Make sliders on settings use changeCommitted instead of onChange * Fix ringtone paths * Break alert words overflow * Fix tweet overflow pepega * Remove unused hook * Update volume sliders in real time * Fix resolution notifications * Fix initial value for vol sliders * Fix call duration timer bugs * Add alert when phone active and phone closes * keep notification * Stop ringing when accept
This commit is contained in:
1 parent
8ecb91e71f
commit
4eda44b9c2
28 files changed
+589
-142
No files matched your search
@@ -38,7 +38,7 @@ function CallTimer({ isAccepted }) {
|
||||
return () => clearInterval(timer.current)
|
||||
}, [isAccepted, setDuration]);
|
||||
|
||||
if (!duration) return null;
|
||||
if (!duration || !isAccepted) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { CallProps } from '../../../common/typings/call';
|
||||
import { useApp } from '../../apps/hooks/useApps';
|
||||
import { useNotifications } from '../../notifications/hooks/useNotifications';
|
||||
import { useRingtoneSound } from '../../sound/hooks/useRingtoneSound';
|
||||
import { CallNotification } from '../components/CallNotification';
|
||||
import { useCall } from './useCall';
|
||||
|
||||
const NOTIFICATION_ID = 'call:current';
|
||||
|
||||
export const useCallNotifications = () => {
|
||||
const { t } = useTranslation();
|
||||
const history = useHistory();
|
||||
const { addNotificationAlert, removeId } = useNotifications();
|
||||
|
||||
const { play, stop, playing } = useRingtoneSound();
|
||||
|
||||
const { icon, notificationIcon } = useApp('DIALER');
|
||||
|
||||
const callNotificationBase = {
|
||||
app: 'CALL',
|
||||
id: NOTIFICATION_ID,
|
||||
cantClose: true,
|
||||
icon,
|
||||
onClick: () => history.push('/call'),
|
||||
notificationIcon,
|
||||
};
|
||||
|
||||
const { call } = useCall();
|
||||
|
||||
const setNotification = (call: CallProps) => {
|
||||
if (!call || !call.active) {
|
||||
stop();
|
||||
removeId(NOTIFICATION_ID);
|
||||
return;
|
||||
}
|
||||
if (call.accepted) {
|
||||
stop();
|
||||
removeId(NOTIFICATION_ID);
|
||||
addNotificationAlert(
|
||||
{
|
||||
...callNotificationBase,
|
||||
keepWhenPhoneClosed: true,
|
||||
content: (
|
||||
<CallNotification>
|
||||
{t('APPS_DIALER_CURRENT_CALL_WITH', {
|
||||
transmitter: call.transmitter,
|
||||
})}
|
||||
</CallNotification>
|
||||
),
|
||||
title: t('APPS_DIALER_CURRENT_CALL_TITLE'),
|
||||
},
|
||||
true
|
||||
);
|
||||
}
|
||||
if (!call.isTransmitter && !call.accepted) {
|
||||
if (!playing()) {
|
||||
play();
|
||||
}
|
||||
removeId(NOTIFICATION_ID);
|
||||
addNotificationAlert(
|
||||
{
|
||||
...callNotificationBase,
|
||||
title: t('APPS_DIALER_INCOMING_CALL_TITLE', {
|
||||
transmitter: call.transmitter,
|
||||
}),
|
||||
keepWhenPhoneClosed: true,
|
||||
content: (
|
||||
<CallNotification>
|
||||
{t('APPS_DIALER_TRANSMITTER_IS_CALLING', {
|
||||
transmitter: call.transmitter,
|
||||
})}
|
||||
</CallNotification>
|
||||
),
|
||||
},
|
||||
true
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const resetCallNotification = () => setNotification(call);
|
||||
|
||||
return { setNotification, resetCallNotification };
|
||||
};
|
||||
@@ -1,18 +1,14 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useNuiEvent } from '../../nui-events/hooks/useNuiEvent';
|
||||
import { useSetRecoilState } from 'recoil';
|
||||
import { callerState } from './state';
|
||||
import { useNotifications } from '../../notifications/hooks/useNotifications';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { CallNotification } from '../components/CallNotification';
|
||||
import { useCall } from './useCall';
|
||||
import { CallProps } from '../../../common/typings/call';
|
||||
import { useApp } from '../../apps/hooks/useApps';
|
||||
import InjectDebugData from '../../debug/InjectDebugData';
|
||||
import { useCallModal } from './useCallModal';
|
||||
import { useHistory, useLocation } from 'react-router-dom';
|
||||
|
||||
const NOTIFICATION_ID = 'call:current';
|
||||
import { callerState } from './state';
|
||||
import { useCallNotifications } from './useCallNotifications';
|
||||
import { useDuration } from './useDuration';
|
||||
|
||||
InjectDebugData<CallProps | boolean>([
|
||||
/* {
|
||||
@@ -22,7 +18,7 @@ InjectDebugData<CallProps | boolean>([
|
||||
accepted: false,
|
||||
isTransmitter: false,
|
||||
transmitter: 'Chip',
|
||||
receiver: 'Taso',
|
||||
receiver: 'Taso',
|
||||
active: true,
|
||||
},
|
||||
},
|
||||
@@ -34,33 +30,19 @@ InjectDebugData<CallProps | boolean>([
|
||||
]);
|
||||
|
||||
export const useCallService = () => {
|
||||
const { t } = useTranslation();
|
||||
const { modal } = useCallModal();
|
||||
const history = useHistory();
|
||||
const { pathname } = useLocation();
|
||||
const {
|
||||
addNotificationAlert,
|
||||
removeId,
|
||||
addNotification,
|
||||
} = useNotifications();
|
||||
|
||||
const { call, setCall } = useCall();
|
||||
const { resetDuration } = useDuration();
|
||||
|
||||
const { icon, notificationIcon } = useApp('DIALER');
|
||||
const { setNotification } = useCallNotifications();
|
||||
|
||||
const setModal = useSetRecoilState(callerState.callModal);
|
||||
|
||||
const [modalHasBeenOpenedThisCall, setModalOpened] = useState<boolean>(false);
|
||||
|
||||
const callNotificationBase = {
|
||||
app: 'CALL',
|
||||
id: NOTIFICATION_ID,
|
||||
cantClose: true,
|
||||
icon,
|
||||
onClick: () => history.push('/call'),
|
||||
notificationIcon,
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setModalOpened(!!modal);
|
||||
}, [modal]);
|
||||
@@ -75,47 +57,10 @@ export const useCallService = () => {
|
||||
}, [history, modal, pathname, modalHasBeenOpenedThisCall]);
|
||||
|
||||
const _setCall = (_call: CallProps) => {
|
||||
resetDuration();
|
||||
setCall(_call);
|
||||
if (!_call || !_call.active) {
|
||||
removeId(NOTIFICATION_ID);
|
||||
return;
|
||||
}
|
||||
if (_call.accepted) {
|
||||
removeId(NOTIFICATION_ID);
|
||||
addNotification({
|
||||
...callNotificationBase,
|
||||
content: (
|
||||
<CallNotification>
|
||||
{t('APPS_DIALER_CURRENT_CALL_WITH', {
|
||||
transmitter: _call.transmitter,
|
||||
})}
|
||||
</CallNotification>
|
||||
),
|
||||
title: t('APPS_DIALER_CURRENT_CALL_TITLE'),
|
||||
});
|
||||
}
|
||||
if (!_call.isTransmitter && !_call.accepted) {
|
||||
removeId(NOTIFICATION_ID);
|
||||
addNotificationAlert(
|
||||
{
|
||||
...callNotificationBase,
|
||||
title: t('APPS_DIALER_INCOMING_CALL_TITLE', {
|
||||
transmitter: _call.transmitter,
|
||||
}),
|
||||
keepWhenPhoneClosed: true,
|
||||
content: (
|
||||
<CallNotification>
|
||||
{t('APPS_DIALER_TRANSMITTER_IS_CALLING', {
|
||||
transmitter: _call.transmitter,
|
||||
})}
|
||||
</CallNotification>
|
||||
),
|
||||
},
|
||||
true
|
||||
);
|
||||
}
|
||||
setNotification(_call);
|
||||
};
|
||||
|
||||
useNuiEvent<CallProps>('CALL', 'setCaller', _setCall, call);
|
||||
useNuiEvent<boolean>('CALL', 'callModal', setModal);
|
||||
};
|
||||
@@ -1,7 +1,14 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
import { callerState } from './state';
|
||||
|
||||
export const useDuration = () => {
|
||||
const [duration, setDuration] = useRecoilState(callerState.callDuration);
|
||||
return { duration, setDuration}
|
||||
}
|
||||
|
||||
const resetDuration = useCallback(
|
||||
() => setDuration({ ms: 0, s: 0, m: 0, h: 0 }),
|
||||
[setDuration]
|
||||
);
|
||||
|
||||
return { duration, setDuration, resetDuration };
|
||||
};
|
||||
@@ -29,6 +29,7 @@ const useStyles = makeStyles((theme) => ({
|
||||
},
|
||||
},
|
||||
alertContent: {
|
||||
wordBreak: 'break-all',
|
||||
display: '-webkit-box',
|
||||
maxWidth: '360px',
|
||||
height: '60px',
|
||||
|
||||
@@ -5,9 +5,13 @@ import React, {
|
||||
useMemo,
|
||||
useRef,
|
||||
useEffect,
|
||||
useContext,
|
||||
} from 'react';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { useSettings } from '../../../apps/settings/hooks/useSettings';
|
||||
import { phoneState } from '../../phone/hooks/state';
|
||||
import { soundContext } from '../../sound/providers/SoundProvider';
|
||||
import { getSoundSettings } from '../../sound/utils/getSoundSettings';
|
||||
import { DEFAULT_ALERT_HIDE_TIME } from '../notifications.constants';
|
||||
|
||||
export interface INotification {
|
||||
@@ -17,6 +21,7 @@ export interface INotification {
|
||||
content?: React.ReactNode;
|
||||
icon?: JSX.Element;
|
||||
notificationIcon?: JSX.Element;
|
||||
sound?: boolean;
|
||||
cantClose?: boolean;
|
||||
keepWhenPhoneClosed?: boolean;
|
||||
onClose?: (notification: INotification) => void;
|
||||
@@ -59,13 +64,17 @@ export const NotificationsContext = createContext<{
|
||||
export function NotificationsProvider({ children }) {
|
||||
const isPhoneOpen = useRecoilValue(phoneState.visibility);
|
||||
|
||||
const [settings] = useSettings();
|
||||
|
||||
const [barUncollapsed, setBarUncollapsed] = useState<boolean>(false);
|
||||
|
||||
const [notifications, setNotifications] = useState<INotification[]>([]);
|
||||
|
||||
const { mount, play } = useContext(soundContext);
|
||||
|
||||
const alertTimeout = useRef<NodeJS.Timeout>();
|
||||
const [alerts, setAlerts] = useState<
|
||||
Array<[INotification, boolean, Partial<INotification>]>
|
||||
Array<[INotification, boolean, Partial<INotification>, string | undefined]>
|
||||
>([]);
|
||||
const [currentAlert, setCurrentAlert] = useState<INotificationAlert>();
|
||||
|
||||
@@ -122,7 +131,12 @@ export function NotificationsProvider({ children }) {
|
||||
);
|
||||
|
||||
const _showAlert = useCallback(
|
||||
(n: INotification, persist: boolean, update: Partial<INotification>) => {
|
||||
(
|
||||
n: INotification,
|
||||
persist: boolean,
|
||||
update: Partial<INotification>,
|
||||
soundUrl: string
|
||||
) => {
|
||||
return new Promise<void>((res) => {
|
||||
const resolve = () => {
|
||||
if (persist) {
|
||||
@@ -144,6 +158,10 @@ export function NotificationsProvider({ children }) {
|
||||
onCloseAlert: onExit(n.onClose),
|
||||
});
|
||||
|
||||
if (n.sound && soundUrl) {
|
||||
play(soundUrl);
|
||||
}
|
||||
|
||||
if (n.keepWhenPhoneClosed) {
|
||||
return;
|
||||
}
|
||||
@@ -155,7 +173,7 @@ export function NotificationsProvider({ children }) {
|
||||
}, DEFAULT_ALERT_HIDE_TIME + 300);
|
||||
});
|
||||
},
|
||||
[addOrUpdateNotification]
|
||||
[addOrUpdateNotification, play]
|
||||
);
|
||||
|
||||
const addNotificationAlert = (
|
||||
@@ -163,7 +181,18 @@ export function NotificationsProvider({ children }) {
|
||||
persist?: boolean,
|
||||
update?: Partial<INotification>
|
||||
) => {
|
||||
setAlerts((curr) => [...curr, [n, persist, update]]);
|
||||
if (n.sound) {
|
||||
const { sound, volume } = getSoundSettings(
|
||||
'notification',
|
||||
settings,
|
||||
n.app
|
||||
);
|
||||
mount(sound, volume, false).then(({ url }) =>
|
||||
setAlerts((curr) => [...curr, [n, persist, update, url]])
|
||||
);
|
||||
return;
|
||||
}
|
||||
setAlerts((curr) => [...curr, [n, persist, update, undefined]]);
|
||||
};
|
||||
|
||||
const removeId = (id: string) => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { IServerConfig } from '../../../common/typings/config';
|
||||
import ServerConfig from '../../../config.autogen.json';
|
||||
import { useCallNotifications } from '../../call/hooks/useCallNotifications';
|
||||
import { useNotifications } from '../../notifications/hooks/useNotifications';
|
||||
import Nui from '../../nui-events/utils/Nui';
|
||||
import { phoneState } from './state';
|
||||
@@ -16,9 +17,11 @@ export const usePhone = (): IUsePhone => {
|
||||
const isPhoneOpen = useRecoilValue(phoneState.visibility);
|
||||
|
||||
const { removeAlerts } = useNotifications();
|
||||
const { resetCallNotification } = useCallNotifications();
|
||||
|
||||
const closePhone = () => {
|
||||
removeAlerts();
|
||||
resetCallNotification();
|
||||
Nui.send('phone:close');
|
||||
};
|
||||
const openPhone = () => {
|
||||
|
||||
@@ -38,7 +38,7 @@ export const usePhoneVisibility = () => {
|
||||
|
||||
const bottom = useMemo(() => {
|
||||
if (!visibility) {
|
||||
return `calc(-70% * ${zoom})`;
|
||||
return `calc(-728px * ${zoom})`;
|
||||
}
|
||||
return 0;
|
||||
}, [visibility, zoom]);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { useCallback, useContext, useEffect, useMemo } from 'react';
|
||||
import { useSettings } from '../../../apps/settings/hooks/useSettings';
|
||||
import { soundContext } from '../providers/SoundProvider';
|
||||
import { getSoundSettings } from '../utils/getSoundSettings';
|
||||
|
||||
export const useRingtoneSound = () => {
|
||||
const [settings] = useSettings();
|
||||
|
||||
const context = useContext(soundContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error('useRingtoneSound must be wrapped in SoundProvider');
|
||||
}
|
||||
|
||||
const options = useMemo(() => getSoundSettings('ringtone', settings), [
|
||||
settings,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!context.isMounted(options.sound)) {
|
||||
context.mount(options.sound, options.volume, true);
|
||||
return;
|
||||
}
|
||||
context.volume(options.sound, options.volume);
|
||||
}, [context, options.sound, options.volume]);
|
||||
|
||||
const play = useCallback(() => {
|
||||
context.play(options.sound, options.volume, true);
|
||||
}, [context, options.sound, options.volume]);
|
||||
|
||||
const stop = useCallback(() => {
|
||||
context.stop(options.sound);
|
||||
}, [context, options.sound]);
|
||||
|
||||
const playing = useCallback(() => {
|
||||
return context.playing(options.sound);
|
||||
}, [context, options.sound]);
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
play,
|
||||
stop,
|
||||
playing,
|
||||
}),
|
||||
[play, playing, stop]
|
||||
);
|
||||
};
|
||||
File renamed without changes.
@@ -0,0 +1,154 @@
|
||||
import { Howl } from 'howler';
|
||||
import React, { createContext, useCallback, useEffect, useRef } from 'react';
|
||||
|
||||
interface IMountResponse {
|
||||
howl: Howl;
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface ISoundContext {
|
||||
mount(
|
||||
url: string,
|
||||
volume?: number,
|
||||
loop?: boolean,
|
||||
autoplay?: boolean
|
||||
): Promise<IMountResponse>;
|
||||
play(url?: string, volume?: number, loop?: boolean): void;
|
||||
stop(url: string): void;
|
||||
remove(url: string): boolean;
|
||||
volume(url: string, volume?: number): number;
|
||||
playing(url: string): boolean;
|
||||
isMounted(url: string): boolean;
|
||||
}
|
||||
|
||||
export const soundContext = createContext<ISoundContext>(null);
|
||||
|
||||
export const SoundProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const soundRefs = useRef<Map<string, { howl: Howl; volume: number }>>();
|
||||
|
||||
useEffect(() => {
|
||||
soundRefs.current = new Map();
|
||||
}, []);
|
||||
|
||||
const mount = (
|
||||
url: string,
|
||||
volume: number = 1,
|
||||
loop: boolean = false,
|
||||
autoplay: boolean = false
|
||||
) => {
|
||||
return new Promise<IMountResponse>((res) => {
|
||||
if (!soundRefs.current) {
|
||||
soundRefs.current = new Map();
|
||||
}
|
||||
|
||||
const find = soundRefs.current.get(url);
|
||||
|
||||
if (find) {
|
||||
find.howl.volume(volume);
|
||||
find.howl.loop(loop);
|
||||
if (autoplay) {
|
||||
find.howl.play();
|
||||
}
|
||||
return res({ howl: find.howl, url });
|
||||
}
|
||||
|
||||
const instance = new Howl({
|
||||
src: url,
|
||||
loop,
|
||||
volume,
|
||||
autoplay,
|
||||
});
|
||||
|
||||
soundRefs.current.set(url, { howl: instance, volume });
|
||||
|
||||
instance.once('load', () => {
|
||||
console.log('ready', instance);
|
||||
res({ howl: instance, url });
|
||||
});
|
||||
|
||||
const onError = (_id, e) => {
|
||||
res(null);
|
||||
console.error('Howler Error \n', e, '\nid: ', _id, '\nsrc: ', url);
|
||||
};
|
||||
|
||||
instance.once('loaderror', onError);
|
||||
|
||||
instance.once('playerror', onError);
|
||||
});
|
||||
};
|
||||
|
||||
const play = useCallback((url: string, volume: number, loop: boolean) => {
|
||||
const sound = soundRefs.current.get(url);
|
||||
let fadedSounds = [];
|
||||
if (sound) {
|
||||
soundRefs.current.forEach((ref, key) => {
|
||||
const isCurrent = key === url;
|
||||
const isPlaying = ref.howl.playing();
|
||||
const isLoop = ref.howl.loop();
|
||||
if (isCurrent || !isLoop || !isPlaying) {
|
||||
return;
|
||||
}
|
||||
fadedSounds.push(ref);
|
||||
const currVol = ref.howl.volume();
|
||||
ref.howl.fade(currVol, currVol * 0.3, 50);
|
||||
});
|
||||
if (volume !== undefined) {
|
||||
sound.howl.volume(volume);
|
||||
}
|
||||
if (loop !== undefined) {
|
||||
sound.howl.loop(loop);
|
||||
}
|
||||
sound.howl.play();
|
||||
|
||||
sound.howl.once('end', () => {
|
||||
fadedSounds.forEach((s) => {
|
||||
s.howl.fade(s.howl.volume(), s.volume, 50);
|
||||
});
|
||||
fadedSounds = undefined;
|
||||
});
|
||||
return;
|
||||
}
|
||||
mount(url, volume, loop, true);
|
||||
}, []);
|
||||
|
||||
const volume = (url: string, volume: number = undefined): number => {
|
||||
const sound = soundRefs.current.get(url);
|
||||
return sound?.howl.volume(volume) as number;
|
||||
};
|
||||
|
||||
const stop = (url: string) => {
|
||||
const sound = soundRefs.current.get(url);
|
||||
sound?.howl.stop();
|
||||
};
|
||||
|
||||
const remove = (url: string): boolean => {
|
||||
const sound = soundRefs.current.get(url);
|
||||
if (sound) {
|
||||
sound.howl.stop();
|
||||
sound.howl.unload();
|
||||
soundRefs.current.delete(url);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const isMounted = (url: string): boolean => {
|
||||
if (!soundRefs.current) {
|
||||
return false;
|
||||
}
|
||||
return !!soundRefs.current.get(url);
|
||||
};
|
||||
|
||||
const playing = (url: string) => {
|
||||
const sound = soundRefs.current.get(url);
|
||||
return sound?.howl.playing() || false;
|
||||
};
|
||||
|
||||
return (
|
||||
<soundContext.Provider
|
||||
value={{ mount, play, stop, playing, remove, volume, isMounted }}
|
||||
>
|
||||
{children}
|
||||
</soundContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
import { IPhoneSettings } from '../../../apps/settings/hooks/useSettings';
|
||||
import {
|
||||
getNotificationPath,
|
||||
getRingtonePath,
|
||||
} from '../../../apps/settings/utils/getRingtonePath';
|
||||
|
||||
const getPath = {
|
||||
ringtone: getRingtonePath,
|
||||
notification: getNotificationPath,
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the configuration for SoundProvider based on settings and apps
|
||||
*/
|
||||
export const getSoundSettings = (
|
||||
type: 'ringtone' | 'notification',
|
||||
settings: IPhoneSettings,
|
||||
app?: string
|
||||
) => {
|
||||
try {
|
||||
return app
|
||||
? {
|
||||
sound: getPath[type](settings[`${app}_${type}`]),
|
||||
volume: settings[`${app}_${type}Vol`] / 100,
|
||||
}
|
||||
: {
|
||||
sound: getPath[type](settings[type]),
|
||||
volume: settings[`${type}Vol`] / 100,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('getSoundSettings.ts error', e, 'Using default sounds');
|
||||
return { sound: getPath[type](), volume: 1 };
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user