useColorscheme.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {useEffect} from 'react';
  2. import {NODE_ENV} from 'sentry/constants';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  5. import useMedia from './useMedia';
  6. function setFaviconTheme(theme: 'dark' | 'light'): void {
  7. // only on prod because we have a development favicon
  8. if (NODE_ENV !== 'production') {
  9. return;
  10. }
  11. const faviconNode = document.querySelector<HTMLLinkElement>(
  12. '[rel="icon"][type="image/png"]'
  13. );
  14. if (faviconNode === null) {
  15. return;
  16. }
  17. const path = faviconNode.href.split('/sentry/')[0];
  18. const iconName = theme === 'dark' ? 'favicon-dark' : 'favicon';
  19. faviconNode.href = `${path}/sentry/images/${iconName}.png`;
  20. }
  21. export function useColorscheme() {
  22. const {user} = useLegacyStore(ConfigStore);
  23. const configuredTheme = user?.options.theme ?? 'system';
  24. const preferDark = useMedia('(prefers-color-scheme: dark)');
  25. const preferredTheme = preferDark ? 'dark' : 'light';
  26. useEffect(() => {
  27. const theme = configuredTheme === 'system' ? preferredTheme : configuredTheme;
  28. setFaviconTheme(theme);
  29. ConfigStore.set('theme', theme);
  30. }, [configuredTheme, preferredTheme]);
  31. }