|
| 1 | +import React, { createContext, useContext } from "react"; |
| 2 | +import { useSearchParams } from "react-router-dom"; |
| 3 | + |
| 4 | +import { useTheme } from "@/components/theme-provider"; |
| 5 | + |
| 6 | +interface EmbedColorsContextType { |
| 7 | + backgroundColor: string; |
| 8 | + textForegroundColor: string; |
| 9 | + primaryColor: string; |
| 10 | + fromMeBubbleColor: string; |
| 11 | + fromMeForegroundColor: string; |
| 12 | + fromOtherBubbleColor: string; |
| 13 | + fromOtherForegroundColor: string; |
| 14 | + fromMeQuotedBubbleColor: string; |
| 15 | + fromOtherQuotedBubbleColor: string; |
| 16 | + inputBackgroundColor: string; |
| 17 | + inputTextForegroundColor: string; |
| 18 | + inputIconsMainColor: string; |
| 19 | +} |
| 20 | + |
| 21 | +const EmbedColorsContext = createContext<EmbedColorsContextType>({ |
| 22 | + backgroundColor: "", |
| 23 | + textForegroundColor: "", |
| 24 | + primaryColor: "", |
| 25 | + fromMeBubbleColor: "", |
| 26 | + fromMeForegroundColor: "", |
| 27 | + fromOtherBubbleColor: "", |
| 28 | + fromOtherForegroundColor: "", |
| 29 | + fromMeQuotedBubbleColor: "", |
| 30 | + fromOtherQuotedBubbleColor: "", |
| 31 | + inputBackgroundColor: "", |
| 32 | + inputTextForegroundColor: "", |
| 33 | + inputIconsMainColor: "", |
| 34 | +}); |
| 35 | + |
| 36 | +export function EmbedColorsProvider({ |
| 37 | + children, |
| 38 | +}: { |
| 39 | + children: React.ReactNode; |
| 40 | +}) { |
| 41 | + const [searchParams] = useSearchParams(); |
| 42 | + const { theme } = useTheme(); |
| 43 | + const backgroundColor = searchParams.get("backgroundColor"); |
| 44 | + const textForegroundColor = searchParams.get("textForegroundColor"); |
| 45 | + const primaryColor = searchParams.get("primaryColor"); |
| 46 | + const fromMeBubbleColor = searchParams.get("fromMeBubbleColor"); |
| 47 | + const fromMeForegroundColor = searchParams.get("fromMeForegroundColor"); |
| 48 | + const fromOtherBubbleColor = searchParams.get("fromOtherBubbleColor"); |
| 49 | + const fromOtherForegroundColor = searchParams.get("fromOtherForegroundColor"); |
| 50 | + const fromMeQuotedBubbleColor = searchParams.get("fromMeQuotedBubbleColor"); |
| 51 | + const fromOtherQuotedBubbleColor = searchParams.get( |
| 52 | + "fromOtherQuotedBubbleColor", |
| 53 | + ); |
| 54 | + const inputBackgroundColor = searchParams.get("inputBackgroundColor"); |
| 55 | + const inputTextForegroundColor = searchParams.get("inputTextForegroundColor"); |
| 56 | + const inputIconsMainColor = searchParams.get("inputIconsMainColor"); |
| 57 | + const getDefaultBackgroundColor = () => { |
| 58 | + if (theme === "dark") { |
| 59 | + return "#0f0f0f"; |
| 60 | + } |
| 61 | + return "#faf9fa"; |
| 62 | + }; |
| 63 | + |
| 64 | + const getDefaultTextForegroundColor = () => { |
| 65 | + if (theme === "dark") { |
| 66 | + return "#faf9fa"; |
| 67 | + } |
| 68 | + return "#020202"; |
| 69 | + }; |
| 70 | + |
| 71 | + const getDefaultPrimaryColor = () => { |
| 72 | + if (theme === "dark") { |
| 73 | + return "#0b332a"; |
| 74 | + } |
| 75 | + return "#e0f0f0"; |
| 76 | + }; |
| 77 | + |
| 78 | + const getDefaultFromMeBubbleColor = () => { |
| 79 | + if (theme === "dark") { |
| 80 | + return "#0b332a"; |
| 81 | + } |
| 82 | + return "#c8fff2"; |
| 83 | + }; |
| 84 | + |
| 85 | + const getDefaultFromMeForegroundColor = () => { |
| 86 | + if (theme === "dark") { |
| 87 | + return "#ffffff"; |
| 88 | + } |
| 89 | + return "#020202"; |
| 90 | + }; |
| 91 | + |
| 92 | + const getDefaultFromOtherBubbleColor = () => { |
| 93 | + if (theme === "dark") { |
| 94 | + return "#1d2724"; |
| 95 | + } |
| 96 | + return "#e0f0f0"; |
| 97 | + }; |
| 98 | + |
| 99 | + const getDefaultFromOtherForegroundColor = () => { |
| 100 | + if (theme === "dark") { |
| 101 | + return "#ffffff"; |
| 102 | + } |
| 103 | + return "#020202"; |
| 104 | + }; |
| 105 | + |
| 106 | + const getDefaultInputBackgroundColor = () => { |
| 107 | + if (theme === "dark") { |
| 108 | + return "#161616"; |
| 109 | + } |
| 110 | + return "#e0f0f0"; |
| 111 | + }; |
| 112 | + |
| 113 | + const getDefaultInputTextForegroundColor = () => { |
| 114 | + if (theme === "dark") { |
| 115 | + return "#faf9fa"; |
| 116 | + } |
| 117 | + return "#020202"; |
| 118 | + }; |
| 119 | + |
| 120 | + const getDefaultFromMeQuotedBubbleColor = () => { |
| 121 | + if (theme === "dark") { |
| 122 | + return "#1f463d"; |
| 123 | + } |
| 124 | + return "#aff7e6"; |
| 125 | + }; |
| 126 | + |
| 127 | + const getDefaultFromOtherQuotedBubbleColor = () => { |
| 128 | + if (theme === "dark") { |
| 129 | + return "#0f1413"; |
| 130 | + } |
| 131 | + return "#d2e2e2"; |
| 132 | + }; |
| 133 | + |
| 134 | + const getDefaultInputIconsMainColor = () => { |
| 135 | + if (theme === "dark") { |
| 136 | + return "#0e6451"; |
| 137 | + } |
| 138 | + return "#0b332a"; |
| 139 | + }; |
| 140 | + |
| 141 | + return ( |
| 142 | + <EmbedColorsContext.Provider |
| 143 | + value={{ |
| 144 | + backgroundColor: backgroundColor || getDefaultBackgroundColor(), |
| 145 | + textForegroundColor: |
| 146 | + textForegroundColor || getDefaultTextForegroundColor(), |
| 147 | + primaryColor: primaryColor || getDefaultPrimaryColor(), |
| 148 | + fromMeBubbleColor: fromMeBubbleColor || getDefaultFromMeBubbleColor(), |
| 149 | + fromMeForegroundColor: |
| 150 | + fromMeForegroundColor || getDefaultFromMeForegroundColor(), |
| 151 | + fromOtherBubbleColor: |
| 152 | + fromOtherBubbleColor || getDefaultFromOtherBubbleColor(), |
| 153 | + fromOtherForegroundColor: |
| 154 | + fromOtherForegroundColor || getDefaultFromOtherForegroundColor(), |
| 155 | + fromMeQuotedBubbleColor: |
| 156 | + fromMeQuotedBubbleColor || getDefaultFromMeQuotedBubbleColor(), |
| 157 | + fromOtherQuotedBubbleColor: |
| 158 | + fromOtherQuotedBubbleColor || getDefaultFromOtherQuotedBubbleColor(), |
| 159 | + inputBackgroundColor: |
| 160 | + inputBackgroundColor || getDefaultInputBackgroundColor(), |
| 161 | + inputTextForegroundColor: |
| 162 | + inputTextForegroundColor || getDefaultInputTextForegroundColor(), |
| 163 | + inputIconsMainColor: |
| 164 | + inputIconsMainColor || getDefaultInputIconsMainColor(), |
| 165 | + }} |
| 166 | + > |
| 167 | + {children} |
| 168 | + </EmbedColorsContext.Provider> |
| 169 | + ); |
| 170 | +} |
| 171 | + |
| 172 | +export const useEmbedColors = () => useContext(EmbedColorsContext); |
0 commit comments