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

feat: suppress periodic audio in inspector #2281

Merged
merged 1 commit into from
Nov 1, 2024
Merged
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
20 changes: 20 additions & 0 deletions assets/src/components/admin/inspector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { useHistory, useLocation } from "react-router-dom";

import AdminForm from "./admin_form";

import { type AudioConfig } from "Components/v2/screen_container";

import { doSubmit, type Config, type Screen } from "Util/admin";
import {
type Message,
Expand Down Expand Up @@ -383,6 +385,7 @@ const DataControls: ComponentType<{
};

const AudioControls: ComponentType<{ screen: ScreenWithId }> = ({ screen }) => {
const [config, setConfig] = useState<AudioConfig | null | undefined>();
// To bypass browser caching, we add a meaningless query param to the URL of
// the <audio> element, based on the timestamp the Play button was clicked.
const [playingAt, setPlayingAt] = useState<Date | null>(null);
Expand All @@ -394,6 +397,10 @@ const AudioControls: ComponentType<{ screen: ScreenWithId }> = ({ screen }) => {
[dialogRef, ssml],
);

useReceiveMessage((message) => {
if (message.type == "audio_config") setConfig(message.config);
});

const audioPath = AUDIO_SCREEN_TYPES.has(screen.config.app_id)
? `/v2/audio/${screen.id}`
: null;
Expand Down Expand Up @@ -443,6 +450,19 @@ const AudioControls: ComponentType<{ screen: ScreenWithId }> = ({ screen }) => {
src={`${audioPath}/readout.mp3?at=${playingAt.getTime()}`}
/>
)}

{config !== undefined && (
<div>
{config ? (
<span>
🔈 Plays every <b>{config.readoutIntervalMinutes}</b> minutes
(offset <b>{config.intervalOffsetSeconds}</b> seconds)
</span>
) : (
"🔇 No periodic audio readout"
)}
</div>
)}
</>
) : (
<div>Not supported on this screen</div>
Expand Down
8 changes: 8 additions & 0 deletions assets/src/hooks/v2/use_audio_readout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AudioConfig } from "Components/v2/screen_container";
import useDriftlessInterval from "Hooks/use_driftless_interval";
import { fetchDatasetValue } from "Util/dataset";
import { isFramed, sendToInspector } from "Util/inspector";

const readoutPath = (id: string) => `/v2/audio/${id}/readout.mp3`;
const volumePath = (id: string) => `/v2/audio/${id}/volume`;
Expand Down Expand Up @@ -32,6 +33,13 @@ interface UseAudioReadoutArgs {
}

const useAudioReadout = ({ id, config }: UseAudioReadoutArgs): void => {
if (isFramed()) {
// Don't read out periodic audio in the inspector, since there's otherwise
// no way to turn it off. Just report the config that would have been used.
sendToInspector({ type: "audio_config", config: config });
return;
}

if (config == null || config.readoutIntervalMinutes === 0) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions assets/src/util/inspector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect } from "react";
import { type AudioConfig } from "Components/v2/screen_container";

/**
* Defines a protocol the "Inspector" admin UI can use to communicate with
Expand All @@ -11,6 +12,7 @@ import { useEffect } from "react";
*/

export type Message =
| { type: "audio_config"; config: AudioConfig | null }
| { type: "data_refreshed"; timestamp: number }
| { type: "refresh_data" }
| { type: "set_data_variant"; variant: string | null }
Expand Down Expand Up @@ -44,6 +46,7 @@ const receiveHook = (handler: MessageHandler) => {
};

export {
isFramed,
sendMessage,
sendToInspector,
useReceiveMessage,
Expand Down
Loading