Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | "use client" |
| 2 | |
| 3 | import { |
| 4 | TERMINAL_THEMES, |
| 5 | type TerminalTheme, |
| 6 | } from "@/lib/terminal-themes" |
| 7 | import * as React from "react" |
| 8 | |
| 9 | const STORAGE_KEY = "terminal-kit:theme" |
| 10 | |
| 11 | const listeners = new Set<() => void>() |
| 12 | |
| 13 | function readStoredTheme(): TerminalTheme { |
| 14 | if (typeof window === "undefined") return "default" |
| 15 | |
| 16 | const stored = window.localStorage.getItem(STORAGE_KEY) |
| 17 | if (stored && TERMINAL_THEMES.includes(stored as TerminalTheme)) { |
| 18 | return stored as TerminalTheme |
| 19 | } |
| 20 | |
| 21 | return "default" |
| 22 | } |
| 23 | |
| 24 | function subscribe(listener: () => void) { |
| 25 | listeners.add(listener) |
| 26 | return () => listeners.delete(listener) |
| 27 | } |
| 28 | |
| 29 | function getSnapshot() { |
| 30 | return readStoredTheme() |
| 31 | } |
| 32 | |
| 33 | function getServerSnapshot(): TerminalTheme { |
| 34 | return "default" |
| 35 | } |
| 36 | |
| 37 | function setStoredTheme(next: TerminalTheme) { |
| 38 | window.localStorage.setItem(STORAGE_KEY, next) |
| 39 | listeners.forEach((listener) => listener()) |
| 40 | } |
| 41 | |
| 42 | type TerminalThemeContextValue = { |
| 43 | theme: TerminalTheme |
| 44 | setTheme: (theme: TerminalTheme) => void |
| 45 | } |
| 46 | |
| 47 | const TerminalThemeContext = React.createContext<TerminalThemeContextValue | null>( |
| 48 | null |
| 49 | ) |
| 50 | |
| 51 | export function TerminalThemeProvider({ children }: { children: React.ReactNode }) { |
| 52 | const theme = React.useSyncExternalStore( |
| 53 | subscribe, |
| 54 | getSnapshot, |
| 55 | getServerSnapshot |
| 56 | ) |
| 57 | |
| 58 | const setTheme = React.useCallback((next: TerminalTheme) => { |
| 59 | setStoredTheme(next) |
| 60 | }, []) |
| 61 | |
| 62 | return ( |
| 63 | <TerminalThemeContext.Provider value={{ theme, setTheme }}> |
| 64 | {children} |
| 65 | </TerminalThemeContext.Provider> |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | export function useTerminalTheme() { |
| 70 | const context = React.useContext(TerminalThemeContext) |
| 71 | if (!context) { |
| 72 | throw new Error("useTerminalTheme must be used within TerminalThemeProvider") |
| 73 | } |
| 74 | |
| 75 | return context |
| 76 | } |
| 77 | |
| 78 | export function useTerminalThemeOptional() { |
| 79 | return React.useContext(TerminalThemeContext) |
| 80 | } |