mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 14:03:30 +00:00 
			
		
		
		
	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:
		@@ -1,11 +1,10 @@
 | 
				
			|||||||
import * as stream_data from "./stream_data";
 | 
					import * as stream_data from "./stream_data";
 | 
				
			||||||
import {DEFAULT_COLOR} from "./stream_data";
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// In an attempt to decrease mixing, set stream bar
 | 
					// In an attempt to decrease mixing, set stream bar
 | 
				
			||||||
// color look like the stream being used.
 | 
					// color look like the stream being used.
 | 
				
			||||||
// (In particular, if there's a color associated with it,
 | 
					// (In particular, if there's a color associated with it,
 | 
				
			||||||
//  have that color be reflected here too.)
 | 
					//  have that color be reflected here too.)
 | 
				
			||||||
export function decorate(stream_id: number | undefined, $element: JQuery): void {
 | 
					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);
 | 
					    $element.css("background-color", color);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -41,7 +41,7 @@ export type InviteStreamData = {
 | 
				
			|||||||
    default_stream: boolean;
 | 
					    default_stream: boolean;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const DEFAULT_COLOR = "#c2c2c2";
 | 
					const DEFAULT_COLOR = "#c2c2c2";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Expose get_subscriber_count for our automated puppeteer tests.
 | 
					// Expose get_subscriber_count for our automated puppeteer tests.
 | 
				
			||||||
export const get_subscriber_count = peer_data.get_subscriber_count;
 | 
					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();
 | 
					    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);
 | 
					    const sub = get_sub_by_id(stream_id);
 | 
				
			||||||
    if (sub === undefined) {
 | 
					    if (sub === undefined) {
 | 
				
			||||||
        return DEFAULT_COLOR;
 | 
					        return DEFAULT_COLOR;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -140,6 +140,7 @@ test("basics", () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    assert.equal(stream_data.get_color(social.stream_id), "red");
 | 
					    assert.equal(stream_data.get_color(social.stream_id), "red");
 | 
				
			||||||
    assert.equal(stream_data.get_color(undefined), "#c2c2c2");
 | 
					    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("denMARK"), "Denmark");
 | 
				
			||||||
    assert.equal(stream_data.get_name("unknown Stream"), "unknown Stream");
 | 
					    assert.equal(stream_data.get_name("unknown Stream"), "unknown Stream");
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user