fix: resolve colors in convertIconToImageURL for qr code

pull/3083/head
William Grant 1 year ago
parent b459bf9570
commit 5e3742e281

@ -4,14 +4,48 @@ import useMount from 'react-use/lib/useMount';
import { SessionIcon, SessionIconProps, SessionIconType } from '../components/icon'; import { SessionIcon, SessionIconProps, SessionIconType } from '../components/icon';
import { sleepFor } from '../session/utils/Promise'; import { sleepFor } from '../session/utils/Promise';
import { useIsDarkTheme } from '../state/selectors/theme'; import { useIsDarkTheme } from '../state/selectors/theme';
import { COLORS } from '../themes/constants/colors'; import { ThemeKeys, getThemeValue } from '../themes/globals';
import { getThemeValue } from '../themes/globals';
import { ThemeColorVariables } from '../themes/variableColors'; const chooseIconColors = (
isThemed: boolean,
isDarkTheme: boolean,
darkColor: ThemeKeys,
lightColor: ThemeKeys,
fallbackColor: ThemeKeys
) => {
return getThemeValue(isThemed ? (isDarkTheme ? darkColor : lightColor) : fallbackColor);
};
const convertIconToImageURL = async ( const convertIconToImageURL = async (
props: Pick<SessionIconProps, 'iconType' | 'iconSize' | 'iconColor' | 'backgroundColor'> props: { isThemed: boolean; isDarkTheme: boolean } & Pick<
) => { SessionIconProps,
const { iconType, iconSize, iconColor = COLORS.BLACK, backgroundColor = COLORS.WHITE } = props; 'iconType' | 'iconSize' | 'iconColor' | 'backgroundColor'
>
): Promise<{ dataUrl: string; bgColor: string; fgColor: string }> => {
const { isThemed, isDarkTheme, iconType, iconSize, iconColor, backgroundColor } = props;
let bgColor = backgroundColor;
let fgColor = iconColor;
if (!bgColor) {
bgColor = chooseIconColors(
isThemed,
isDarkTheme,
'--text-primary-color',
'--background-primary-color',
'--white-color'
);
}
if (!fgColor) {
fgColor = chooseIconColors(
isThemed,
isDarkTheme,
'--background-primary-color',
'--text-primary-color',
'--black-color'
);
}
const root = document.querySelector('#root'); const root = document.querySelector('#root');
const divElement = document.createElement('div'); const divElement = document.createElement('div');
@ -35,21 +69,21 @@ const convertIconToImageURL = async (
reactRoot?.unmount(); reactRoot?.unmount();
root?.removeChild(divElement); root?.removeChild(divElement);
if (svgString) { return {
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgString)}`; bgColor,
} fgColor,
dataUrl: svgString ? `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgString)}` : '',
return null; };
}; };
export const useIconToImageURL = ({ export const useIconToImageURL = ({
iconType, iconType,
iconSize, iconSize,
theming = true, isThemed = true,
}: { }: {
iconType: SessionIconType; iconType: SessionIconType;
iconSize: number; iconSize: number;
theming?: boolean; isThemed?: boolean;
}) => { }) => {
const isDarkTheme = useIsDarkTheme(); const isDarkTheme = useIsDarkTheme();
const [dataURL, setDataURL] = useState(''); const [dataURL, setDataURL] = useState('');
@ -59,58 +93,51 @@ export const useIconToImageURL = ({
const [backgroundColor, setBackgroundColor] = useState(''); const [backgroundColor, setBackgroundColor] = useState('');
const [iconColor, setIconColor] = useState(''); const [iconColor, setIconColor] = useState('');
const chooseColor = useCallback(
(darkColor: keyof ThemeColorVariables, lightColor: keyof ThemeColorVariables) => {
return theming ? getThemeValue(isDarkTheme ? darkColor : lightColor) : '';
},
[isDarkTheme, theming]
);
const loadURL = useCallback(async () => { const loadURL = useCallback(async () => {
setLoading(true); setLoading(true);
setDataURL(''); setDataURL('');
try {
const bgColor = chooseColor('--text-primary-color', '--background-primary-color');
const fgColor = chooseColor('--background-primary-color', '--text-primary-color');
setBackgroundColor(bgColor); try {
setIconColor(fgColor); const {
dataUrl: newURL,
const newURL = await convertIconToImageURL({ bgColor,
fgColor,
} = await convertIconToImageURL({
isThemed,
isDarkTheme,
iconType, iconType,
iconSize, iconSize,
iconColor: fgColor, iconColor,
backgroundColor: bgColor, backgroundColor,
}); });
if (!newURL) { if (!newURL) {
throw new Error('[useIconToImageURL] Failed to convert icon to URL'); throw new Error('[useIconToImageURL] Failed to convert icon to URL');
} }
setBackgroundColor(bgColor);
setIconColor(fgColor);
setDataURL(newURL); setDataURL(newURL);
setInDarkTheme(!!theming && isDarkTheme);
if (!mounted) { if (!mounted) {
setMounted(true); setMounted(true);
} }
setLoading(false); setLoading(false);
setInDarkTheme(!!isThemed && isDarkTheme);
} catch (error) { } catch (error) {
window.log.error('[useIconToImageURL] Error fetching icon data url', error); window.log.error('[useIconToImageURL] Error fetching icon data url', error);
} }
}, [chooseColor, iconSize, iconType, isDarkTheme, mounted, theming]); }, [backgroundColor, iconColor, iconSize, iconType, isDarkTheme, isThemed, mounted]);
useMount(() => { useMount(() => {
void loadURL(); void loadURL();
}); });
useEffect(() => { useEffect(() => {
if (mounted && theming && isDarkTheme !== inDarkTheme) { if (!loading && mounted && isThemed && isDarkTheme !== inDarkTheme) {
void loadURL(); void loadURL();
} }
}, [inDarkTheme, isDarkTheme, loadURL, mounted, theming]); }, [inDarkTheme, isDarkTheme, isThemed, loadURL, loading, mounted]);
const returnProps = { dataURL, iconSize, iconColor, backgroundColor, loading };
window.log.debug(`WIP: [useIconToImageURL] returnProps: ${JSON.stringify(returnProps)}`);
return returnProps; return { dataURL, iconSize, iconColor, backgroundColor, loading };
}; };

@ -8,7 +8,7 @@ function setDuration(duration: number | string) {
} }
// These variables are independent of the current theme // These variables are independent of the current theme
export type ThemeGlobals = { type ThemeGlobals = {
/* Fonts */ /* Fonts */
'--font-default': string; '--font-default': string;
'--font-accent': string; '--font-accent': string;
@ -119,6 +119,21 @@ export type ThemeGlobals = {
'--right-panel-attachment-height': string; '--right-panel-attachment-height': string;
}; };
type Theme = ThemeGlobals | ThemeColorVariables;
export type ThemeKeys = keyof ThemeGlobals | keyof ThemeColorVariables;
export function getThemeValue(key: ThemeKeys) {
return getComputedStyle(document.documentElement).getPropertyValue(key);
}
export function setThemeValues(variables: Theme) {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(variables)) {
document.documentElement.style.setProperty(key, value);
}
}
// These are only set once in the global style (at root). // These are only set once in the global style (at root).
export const THEME_GLOBALS: ThemeGlobals = { export const THEME_GLOBALS: ThemeGlobals = {
'--font-default': 'Roboto', '--font-default': 'Roboto',
@ -212,16 +227,3 @@ export const THEME_GLOBALS: ThemeGlobals = {
'--right-panel-attachment-height': '--right-panel-attachment-height':
'calc(var(--right-panel-height) - 2 * var(--margins-2xl) -7px)', 'calc(var(--right-panel-height) - 2 * var(--margins-2xl) -7px)',
}; };
type ThemeKeys = ThemeGlobals & ThemeColorVariables;
export function getThemeValue(key: keyof ThemeKeys) {
return getComputedStyle(document.documentElement).getPropertyValue(key);
}
export function setThemeValues(variables: ThemeGlobals | ThemeColorVariables) {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(variables)) {
document.documentElement.style.setProperty(key, value);
}
}

Loading…
Cancel
Save