Skip to content

Commit 9ae5791

Browse files
vauwebhrissan
authored andcommitted
StatsHouse UI: update Server Announcement Message MarkDown unicode binary base64
1 parent 5ca7c30 commit 9ae5791

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

statshouse-ui/src/api/api.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
55
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
66

7-
import { hasOwn, toFlatPairs, toString } from '../common/helpers';
7+
import { base64ToUtf8, hasOwn, toFlatPairs, toString } from '../common/helpers';
88
import { API_FETCH_OPT_METHODS, ApiFetchOptMethods } from './enum';
99
import { setAnnouncementMessage } from '@/store2/announcementStore';
1010

@@ -95,8 +95,10 @@ export async function apiFetch<R = unknown, G = ApiFetchParam, P = ApiFetchParam
9595
body,
9696
});
9797
result.status = resp.status;
98-
// two spaces as gfm ' \n'
99-
setAnnouncementMessage(resp.headers.get('X-Statshouse-Announcment')?.replaceAll(' ', ' \n') ?? '');
98+
const announcement = resp.headers.get('X-Statshouse-Announcement');
99+
if (announcement) {
100+
setAnnouncementMessage(base64ToUtf8(announcement));
101+
}
100102

101103
if (resp.headers.get('Content-Type') !== 'application/json') {
102104
const text = await resp.text();

statshouse-ui/src/common/helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,12 @@ export const bwd = (v: number) => {
431431
export function labelAsString(label?: string | HTMLElement): string | undefined {
432432
return label instanceof HTMLElement ? label.innerText : label;
433433
}
434+
435+
export function base64ToUtf8(base64: string): string {
436+
const binString = atob(base64);
437+
return new TextDecoder().decode(Uint8Array.from(binString, (m) => m.codePointAt(0) ?? 0));
438+
}
439+
440+
export function utf8ToBase64(str: string): string {
441+
return btoa(Array.from(new TextEncoder().encode(str), (byte) => String.fromCodePoint(byte)).join(''));
442+
}

0 commit comments

Comments
 (0)