Skip to content

Commit

Permalink
feat: suppress periodic audio in inspector
Browse files Browse the repository at this point in the history
Avoids being jumpscared by a periodic audio readout that also can't be
silenced, short of closing the tab or navigating away. To replace the
missing feedback on whether periodic audio is configured, also adds a
line to the inspector audio controls to report the readout parameters
that would have been used.
  • Loading branch information
digitalcora committed Nov 1, 2024
1 parent cfcb6ec commit 0b2e951
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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

0 comments on commit 0b2e951

Please sign in to comment.