2022-07-26 08:27:57 +00:00
|
|
|
import { useTheme as useNextTheme } from "next-themes";
|
2023-02-17 11:18:54 +00:00
|
|
|
import { useEffect } from "react";
|
2021-07-09 22:59:21 +00:00
|
|
|
|
2022-05-27 15:37:02 +00:00
|
|
|
import { useEmbedTheme } from "@calcom/embed-core/embed-iframe";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Maybe } from "@calcom/trpc/server";
|
2021-10-14 19:22:01 +00:00
|
|
|
|
2023-02-17 11:18:54 +00:00
|
|
|
import useMountedState from "./useMountedState";
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
// makes sure the ui doesn't flash
|
2021-09-27 16:09:19 +00:00
|
|
|
export default function useTheme(theme?: Maybe<string>) {
|
2023-02-17 11:18:54 +00:00
|
|
|
let currentTheme: Maybe<string> = theme || "system";
|
|
|
|
|
|
|
|
const { resolvedTheme, setTheme, forcedTheme, theme: activeTheme } = useNextTheme();
|
2022-04-08 05:33:24 +00:00
|
|
|
const embedTheme = useEmbedTheme();
|
2023-02-17 11:18:54 +00:00
|
|
|
const isMounted = useMountedState();
|
2022-03-31 08:45:47 +00:00
|
|
|
// Embed UI configuration takes more precedence over App Configuration
|
2023-02-17 11:18:54 +00:00
|
|
|
currentTheme = embedTheme || theme;
|
2022-02-16 15:53:26 +00:00
|
|
|
|
2022-07-26 08:27:57 +00:00
|
|
|
useEffect(() => {
|
2023-02-17 11:18:54 +00:00
|
|
|
if (currentTheme !== activeTheme && typeof currentTheme === "string") {
|
|
|
|
setTheme(currentTheme);
|
2022-07-26 08:27:57 +00:00
|
|
|
}
|
2023-02-17 11:18:54 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- we do not want activeTheme to re-render this effect
|
|
|
|
}, [currentTheme, setTheme]);
|
2021-07-09 22:59:21 +00:00
|
|
|
|
|
|
|
return {
|
2022-07-28 10:50:25 +00:00
|
|
|
resolvedTheme,
|
2022-07-26 08:27:57 +00:00
|
|
|
setTheme,
|
2022-07-28 10:50:25 +00:00
|
|
|
forcedTheme,
|
2023-02-17 11:18:54 +00:00
|
|
|
activeTheme,
|
|
|
|
isReady: isMounted(),
|
2021-07-11 19:35:56 +00:00
|
|
|
};
|
|
|
|
}
|