2022-05-19 03:28:51 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-05-23 22:00:01 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2023-10-11 19:06:43 +00:00
|
|
|
import type { Meta } from '@storybook/react';
|
|
|
|
import type { Props } from './CallingAudioIndicator';
|
2023-02-28 20:01:52 +00:00
|
|
|
import {
|
|
|
|
CallingAudioIndicator,
|
|
|
|
SPEAKING_LINGER_MS,
|
|
|
|
} from './CallingAudioIndicator';
|
2022-05-23 22:00:01 +00:00
|
|
|
import { AUDIO_LEVEL_INTERVAL_MS } from '../calling/constants';
|
2023-02-28 20:01:52 +00:00
|
|
|
import { useValueAtFixedRate } from '../hooks/useValueAtFixedRate';
|
2022-05-19 03:28:51 +00:00
|
|
|
|
2022-06-07 00:48:02 +00:00
|
|
|
export default {
|
|
|
|
title: 'Components/CallingAudioIndicator',
|
2023-10-11 19:06:43 +00:00
|
|
|
component: CallingAudioIndicator,
|
|
|
|
argTypes: {
|
|
|
|
hasAudio: {
|
|
|
|
control: { type: 'boolean' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: {
|
|
|
|
hasAudio: true,
|
|
|
|
},
|
|
|
|
} satisfies Meta<Props>;
|
2022-05-19 03:28:51 +00:00
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
export function Extreme(args: Props): JSX.Element {
|
2022-05-23 22:00:01 +00:00
|
|
|
const [audioLevel, setAudioLevel] = useState(1);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setAudioLevel(1 - audioLevel);
|
|
|
|
}, 2 * AUDIO_LEVEL_INTERVAL_MS);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
};
|
|
|
|
}, [audioLevel, setAudioLevel]);
|
|
|
|
|
2023-02-28 20:01:52 +00:00
|
|
|
const isSpeaking = useValueAtFixedRate(audioLevel > 0, SPEAKING_LINGER_MS);
|
|
|
|
|
2022-05-23 22:00:01 +00:00
|
|
|
return (
|
|
|
|
<CallingAudioIndicator
|
2023-10-11 19:06:43 +00:00
|
|
|
hasAudio={args.hasAudio}
|
2022-05-23 22:00:01 +00:00
|
|
|
audioLevel={audioLevel}
|
2023-02-28 20:01:52 +00:00
|
|
|
shouldShowSpeaking={isSpeaking}
|
2022-05-23 22:00:01 +00:00
|
|
|
/>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|
2022-05-23 22:00:01 +00:00
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
export function Random(args: Props): JSX.Element {
|
2022-05-23 22:00:01 +00:00
|
|
|
const [audioLevel, setAudioLevel] = useState(1);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setAudioLevel(Math.random());
|
|
|
|
}, AUDIO_LEVEL_INTERVAL_MS);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
};
|
|
|
|
}, [audioLevel, setAudioLevel]);
|
|
|
|
|
2023-02-28 20:01:52 +00:00
|
|
|
const isSpeaking = useValueAtFixedRate(audioLevel > 0, SPEAKING_LINGER_MS);
|
|
|
|
|
2022-05-23 22:00:01 +00:00
|
|
|
return (
|
|
|
|
<CallingAudioIndicator
|
2023-10-11 19:06:43 +00:00
|
|
|
hasAudio={args.hasAudio}
|
2022-05-23 22:00:01 +00:00
|
|
|
audioLevel={audioLevel}
|
2023-02-28 20:01:52 +00:00
|
|
|
shouldShowSpeaking={isSpeaking}
|
2022-05-23 22:00:01 +00:00
|
|
|
/>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|