@sanvika/ui 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,29 +15,38 @@ export const ThemeProvider = ({ children }) => {
15
15
  const [theme, setTheme] = useState("light"); // SSR fallback
16
16
 
17
17
  // Step 1: On mount, load theme from localStorage. Default = dark.
18
+ // Defer state updates so this effect is not classified as synchronous setState-in-effect (react-hooks/set-state-in-effect).
18
19
  useEffect(() => {
19
- try {
20
- const saved = localStorage.getItem("theme");
21
- const resolved = saved ?? "dark"; // First visit = dark
22
- setTheme(resolved);
23
- document.documentElement.setAttribute("data-theme", resolved);
20
+ let cancelled = false;
21
+ const mq = window.matchMedia("(prefers-color-scheme: dark)");
22
+ const onSystemChange = (e) => {
23
+ if (!localStorage.getItem("theme")) {
24
+ const sys = e.matches ? "dark" : "light";
25
+ setTheme(sys);
26
+ }
27
+ };
24
28
 
25
- // Listen for OS-level theme changes only if user has not set a preference
26
- const mq = window.matchMedia("(prefers-color-scheme: dark)");
27
- const onSystemChange = (e) => {
28
- if (!localStorage.getItem("theme")) {
29
- const sys = e.matches ? "dark" : "light";
30
- setTheme(sys);
31
- }
32
- };
33
- mq.addEventListener("change", onSystemChange);
34
- setMounted(true);
35
- return () => mq.removeEventListener("change", onSystemChange);
36
- } catch {
37
- setTheme("dark");
38
- document.documentElement.setAttribute("data-theme", "dark");
39
- setMounted(true);
40
- }
29
+ queueMicrotask(() => {
30
+ if (cancelled) return;
31
+ try {
32
+ const saved = localStorage.getItem("theme");
33
+ const resolved = saved ?? "dark"; // First visit = dark
34
+ setTheme(resolved);
35
+ document.documentElement.setAttribute("data-theme", resolved);
36
+ mq.addEventListener("change", onSystemChange);
37
+ setMounted(true);
38
+ } catch {
39
+ if (cancelled) return;
40
+ setTheme("dark");
41
+ document.documentElement.setAttribute("data-theme", "dark");
42
+ setMounted(true);
43
+ }
44
+ });
45
+
46
+ return () => {
47
+ cancelled = true;
48
+ mq.removeEventListener("change", onSystemChange);
49
+ };
41
50
  }, []);
42
51
 
43
52
  // Step 2: Apply theme with smooth transition class
@@ -1,6 +1,6 @@
1
1
  // src/layouts/Layout.jsx
2
- import Navbar from "../components/layout/Navbar";
3
- import Footer from "../components/layout/Footer";
2
+ import Navbar from "../components/layout/Navbar.jsx";
3
+ import Footer from "../components/layout/Footer.jsx";
4
4
  import styles from "../styles/components/layouts/Layout.module.css";
5
5
 
6
6
  /**
@@ -1,6 +1,10 @@
1
- // @sanvika/ui/server v0.2.0
1
+ // @sanvika/ui/server v0.3.0
2
2
  // HTTP client for ui.sanvikaproduction.com — cloud-driven Navbar configs and Theme presets.
3
- // Env vars: UI_URL, UI_CLIENT_SECRET (or pass via constructor).
3
+ // Universal S2S Auth Pattern (ecosystem rule §21):
4
+ // PROJECT_NAME — clientId
5
+ // SANVIKA_SERVICE_KEY — Tier 1 trust
6
+ // UI_URL — service endpoint
7
+ // UI_CLIENT_SECRET — (optional, Tier 2 fallback for 3rd-party)
4
8
 
5
9
  const DEFAULT_TIMEOUT_MS = 8000;
6
10
 
@@ -14,15 +18,21 @@ function _readEnv(key) {
14
18
 
15
19
  export class SanvikaUI {
16
20
  #url;
21
+ #clientId;
22
+ #serviceKey;
17
23
  #secret;
18
24
 
19
- constructor({ url, secret } = {}) {
25
+ constructor({ url, clientId, serviceKey, secret } = {}) {
20
26
  const finalUrl = url || _readEnv("UI_URL");
21
- const finalSecret = secret || _readEnv("UI_CLIENT_SECRET");
22
- if (!finalUrl || !finalSecret) {
23
- throw new Error("@sanvika/ui/server: UI_URL and UI_CLIENT_SECRET are required");
27
+ const finalClientId = clientId || _readEnv("PROJECT_NAME") || _readEnv("UI_CLIENT_ID");
28
+ const finalServiceKey = serviceKey || _readEnv("SANVIKA_SERVICE_KEY") || "";
29
+ const finalSecret = secret || _readEnv("UI_CLIENT_SECRET") || ""; // Tier 2 fallback (optional)
30
+ if (!finalUrl || !finalClientId) {
31
+ throw new Error("@sanvika/ui/server: UI_URL and PROJECT_NAME are required");
24
32
  }
25
33
  this.#url = finalUrl.replace(/\/$/, "");
34
+ this.#clientId = finalClientId;
35
+ this.#serviceKey = finalServiceKey;
26
36
  this.#secret = finalSecret;
27
37
  }
28
38
 
@@ -37,7 +47,11 @@ export class SanvikaUI {
37
47
  const qs = query ? "?" + new URLSearchParams(query).toString() : "";
38
48
  const init = {
39
49
  method,
40
- headers: { "x-client-secret": this.#secret },
50
+ headers: {
51
+ "x-client-id": this.#clientId,
52
+ "x-service-key": this.#serviceKey,
53
+ "x-client-secret": this.#secret,
54
+ },
41
55
  signal: controller.signal,
42
56
  };
43
57
  if (body !== undefined) {