signal-desktop/ts/components/CallingAudioIndicator.stories.tsx

65 lines
1.5 KiB
TypeScript
Raw Normal View History

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';
import { boolean } from '@storybook/addon-knobs';
2022-05-19 03:28:51 +00:00
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',
};
2022-05-19 03:28:51 +00:00
2022-11-18 00:45:19 +00:00
export function Extreme(): 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
hasAudio={boolean('hasAudio', true)}
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
2022-11-18 00:45:19 +00:00
export function Random(): 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
hasAudio={boolean('hasAudio', true)}
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
}