stream_data: Allow undefined stream_id in get_color.

Sometimes get_color is called with undefined
stream ids. This change makes more clear
what was already happening, and allows
for smoother conversion of other files
to typescript, including an upcoming
conversion of the drafts module, which
can call `get_color` with a draft with
undefined stream_id.

This keeps the color logic contained in
the `get_color` function, which seems
cleaner than the alternative of exporting
DEFAULT_COLOR to be used wherever sream_id
might be undefined.
This commit is contained in:
evykassirer
2024-02-14 22:20:57 -08:00
committed by Tim Abbott
parent 8784fd9944
commit 97c46a7375
3 changed files with 7 additions and 4 deletions

View File

@@ -1,11 +1,10 @@
import * as stream_data from "./stream_data";
import {DEFAULT_COLOR} from "./stream_data";
// In an attempt to decrease mixing, set stream bar
// color look like the stream being used.
// (In particular, if there's a color associated with it,
// have that color be reflected here too.)
export function decorate(stream_id: number | undefined, $element: JQuery): void {
const color = stream_id === undefined ? DEFAULT_COLOR : stream_data.get_color(stream_id);
const color = stream_data.get_color(stream_id);
$element.css("background-color", color);
}

View File

@@ -41,7 +41,7 @@ export type InviteStreamData = {
default_stream: boolean;
};
export const DEFAULT_COLOR = "#c2c2c2";
const DEFAULT_COLOR = "#c2c2c2";
// Expose get_subscriber_count for our automated puppeteer tests.
export const get_subscriber_count = peer_data.get_subscriber_count;
@@ -457,7 +457,10 @@ export function canonicalized_name(stream_name: string): string {
return stream_name.toString().toLowerCase();
}
export function get_color(stream_id: number): string {
export function get_color(stream_id: number | undefined): string {
if (stream_id === undefined) {
return DEFAULT_COLOR;
}
const sub = get_sub_by_id(stream_id);
if (sub === undefined) {
return DEFAULT_COLOR;

View File

@@ -140,6 +140,7 @@ test("basics", () => {
assert.equal(stream_data.get_color(social.stream_id), "red");
assert.equal(stream_data.get_color(undefined), "#c2c2c2");
assert.equal(stream_data.get_color(1234567), "#c2c2c2");
assert.equal(stream_data.get_name("denMARK"), "Denmark");
assert.equal(stream_data.get_name("unknown Stream"), "unknown Stream");