1 import { decodeUtf8, encodeUtf8 } from '@proton/crypto/lib/utils';
2 import { getCookie, setCookie } from '@proton/shared/lib/helpers/cookies';
3 import { decodeBase64URL, encodeBase64URL } from '@proton/shared/lib/helpers/encoding';
4 import { getSecondLevelDomain } from '@proton/shared/lib/helpers/url';
5 import type { OrganizationWithSettings } from '@proton/shared/lib/interfaces';
7 export const COOKIE_NAME = 'OrgTheme';
9 export const syncToCookie = (cookieValue: string | undefined) => {
10 // Note: We might set `undefined` which will clear the cookie
12 cookieName: COOKIE_NAME,
14 cookieDomain: getSecondLevelDomain(window.location.hostname),
16 expirationDate: 'max',
20 export interface OrgThemeCookie {
26 export const serializeOrgTheme = (organization: OrganizationWithSettings | undefined, localID: number) => {
27 const settings = organization?.Settings;
28 if (!settings || !settings.LogoID) {
31 const value: OrgThemeCookie = {
32 LogoID: settings.LogoID,
33 Name: (organization?.Name || '').slice(0, 50),
36 return encodeBase64URL(encodeUtf8(JSON.stringify(value)));
39 export const deserializeOrgTheme = (serializedValue: string): OrgThemeCookie | undefined => {
41 const deserializedValue = decodeUtf8(decodeBase64URL(serializedValue));
42 const parsedValue = JSON.parse(deserializedValue);
43 const value: OrgThemeCookie = {
44 LogoID: String(parsedValue.LogoID),
45 Name: String(parsedValue.Name),
46 LocalID: Number(parsedValue.LocalID),
52 export const getOrganizationThemeFromCookie = () => {
53 const cookie = getCookie(COOKIE_NAME);
57 return deserializeOrgTheme(cookie);