diff --git a/packages/pinorama-studio/src/app.tsx b/packages/pinorama-studio/src/app.tsx index 85e83b0..de4d2e7 100644 --- a/packages/pinorama-studio/src/app.tsx +++ b/packages/pinorama-studio/src/app.tsx @@ -2,29 +2,37 @@ import { useState } from "react" import { LogExplorer } from "./components/log-explorer" import type { SearchFilters } from "./components/log-explorer/components/log-filters/types" import { TitleBar } from "./components/title-bar/title-bar" +import { useAppConfig } from "./contexts" function App() { + const appConfig = useAppConfig() + const [searchText, setSearchText] = useState("") const [filters, setFilters] = useState({}) const hasFilters = searchText.length > 0 || Object.keys(filters).length > 0 + const handleResetFilters = () => { setSearchText("") setFilters({}) } + const isConnected = appConfig?.config.connectionStatus === "connected" + return (
{/* Header */} {/* Container */} - + {isConnected ? ( + + ) : null}
) } diff --git a/packages/pinorama-studio/src/components/connection-status/connection-status.tsx b/packages/pinorama-studio/src/components/connection-status/connection-status.tsx index 529b108..e06c6ab 100644 --- a/packages/pinorama-studio/src/components/connection-status/connection-status.tsx +++ b/packages/pinorama-studio/src/components/connection-status/connection-status.tsx @@ -18,7 +18,7 @@ import { Input } from "../ui/input" import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover" const formSchema = z.object({ - serverUrl: z.string().url("Invalid URL") + connectionUrl: z.string().url("Invalid URL") }) export function ConnectionStatus() { @@ -30,14 +30,14 @@ export function ConnectionStatus() { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { - serverUrl: appConfig?.config.serverUrl + connectionUrl: appConfig?.config.connectionUrl || "" } }) const onSubmit = (values: z.infer) => { appConfig?.setConfig({ ...appConfig.config, - serverUrl: values.serverUrl + connectionUrl: values.connectionUrl }) setOpen(false) @@ -46,7 +46,13 @@ export function ConnectionStatus() { let statusColor = "" let statusText = "" + const connectionStatus = appConfig?.config.connectionStatus + switch (true) { + case connectionStatus === "disconnected": + statusColor = "bg-gray-500" + statusText = "Disconnected" + break case status === "pending" && fetchStatus === "fetching": statusColor = "bg-orange-500" statusText = "Connecting..." @@ -59,6 +65,10 @@ export function ConnectionStatus() { statusColor = "bg-red-500" statusText = "Connection failed" break + default: + statusColor = "bg-gray-500" + statusText = "Unknown" + break } return ( @@ -68,7 +78,7 @@ export function ConnectionStatus() {
{statusText} - {appConfig?.config.serverUrl ?? "Unknown"} + {appConfig?.config.connectionUrl ?? "Unknown"} @@ -77,10 +87,10 @@ export function ConnectionStatus() {
( - Pinorama Server URL + Server URL @@ -99,7 +109,7 @@ export function ConnectionStatus() { Reset
diff --git a/packages/pinorama-studio/src/components/connection-toggle/connection-toggle.tsx b/packages/pinorama-studio/src/components/connection-toggle/connection-toggle.tsx new file mode 100644 index 0000000..f74a708 --- /dev/null +++ b/packages/pinorama-studio/src/components/connection-toggle/connection-toggle.tsx @@ -0,0 +1,24 @@ +import { useAppConfig } from "@/contexts" +import { Button } from "../ui/button" + +export function ConnectionToggle() { + const appConfig = useAppConfig() + + const connectionStatus = appConfig?.config.connectionStatus + + const text = connectionStatus === "connected" ? "Disconnect" : "Connect" + + const handleClick = () => { + appConfig?.setConfig({ + ...appConfig.config, + connectionStatus: + connectionStatus === "connected" ? "disconnected" : "connected" + }) + } + + return ( + + ) +} diff --git a/packages/pinorama-studio/src/components/title-bar/title-bar.tsx b/packages/pinorama-studio/src/components/title-bar/title-bar.tsx index 37e45d7..9a6d3fb 100644 --- a/packages/pinorama-studio/src/components/title-bar/title-bar.tsx +++ b/packages/pinorama-studio/src/components/title-bar/title-bar.tsx @@ -1,4 +1,5 @@ import { ConnectionStatus } from "../connection-status/connection-status" +import { ConnectionToggle } from "../connection-toggle/connection-toggle" import { PinoramaLogo } from "../pinorama-logo/pinorama-logo" import { ThemeToggle } from "../theme-toggle/theme-toggle" import { Button } from "../ui/button" @@ -33,6 +34,7 @@ export function TitleBar(props: TitleBarProps) { Reset Filters )} + diff --git a/packages/pinorama-studio/src/contexts/app-config-context.tsx b/packages/pinorama-studio/src/contexts/app-config-context.tsx index 54053e2..a131bcd 100644 --- a/packages/pinorama-studio/src/contexts/app-config-context.tsx +++ b/packages/pinorama-studio/src/contexts/app-config-context.tsx @@ -1,7 +1,8 @@ import { createContext, useContext, useEffect, useState } from "react" export type AppConfig = { - serverUrl: string + connectionStatus: "connected" | "disconnected" + connectionUrl?: string | null } type AppConfigContextType = { @@ -9,32 +10,36 @@ type AppConfigContextType = { setConfig: (config: AppConfig) => void } -type AppConfigProviderProps = { - children: React.ReactNode -} - const DEFAULT_CONFIG: AppConfig = { - serverUrl: "http://localhost:6200" + connectionStatus: "disconnected", + connectionUrl: "http://localhost:6200" } -const getQueryParams = (): Partial => { +const getAppConfigFromQueryParams = () => { + const appConfig: Partial = {} const params = new URLSearchParams(window.location.search) - return { - serverUrl: params.get("url") || DEFAULT_CONFIG.serverUrl + + const connectionUrl = params.get("connectionUrl") + if (connectionUrl) { + appConfig.connectionUrl = connectionUrl } + + return appConfig } const AppConfigContext = createContext(null) -export function AppConfigProvider(props: AppConfigProviderProps) { - const queryConfig = getQueryParams() - - const [config, setConfig] = useState({ - serverUrl: queryConfig.serverUrl || DEFAULT_CONFIG.serverUrl - }) +export function AppConfigProvider(props: { children: React.ReactNode }) { + const [config, setConfig] = useState(DEFAULT_CONFIG) useEffect(() => { - const queryConfig = getQueryParams() + const queryConfig = getAppConfigFromQueryParams() + + const autoConnect = !!queryConfig.connectionUrl + if (autoConnect) { + queryConfig.connectionStatus = "connected" + } + setConfig((prevConfig) => ({ ...prevConfig, ...queryConfig })) }, []) diff --git a/packages/pinorama-studio/src/contexts/pinorama-client-context.tsx b/packages/pinorama-studio/src/contexts/pinorama-client-context.tsx index 65966ee..cdf787a 100644 --- a/packages/pinorama-studio/src/contexts/pinorama-client-context.tsx +++ b/packages/pinorama-studio/src/contexts/pinorama-client-context.tsx @@ -13,14 +13,23 @@ const PinoramaClientContext = createContext(null) export function PinoramaClientProvider({ children }: PinoramaClientProviderProps) { - const queryClient = new QueryClient() - const appConfig = useAppConfig() - const pinoramaClient = new PinoramaClient({ - url: appConfig?.config.serverUrl + const queryClient = new QueryClient({ + defaultOptions: { + queries: { + enabled: appConfig?.config.connectionStatus === "connected" + } + } }) + let pinoramaClient: PinoramaClient | null = null + if (appConfig?.config.connectionUrl) { + pinoramaClient = new PinoramaClient({ + url: appConfig?.config.connectionUrl + }) + } + return ( diff --git a/packages/pinorama-studio/src/hooks/use-pinorama-introspection.ts b/packages/pinorama-studio/src/hooks/use-pinorama-introspection.ts index 0c54ec2..db1689c 100644 --- a/packages/pinorama-studio/src/hooks/use-pinorama-introspection.ts +++ b/packages/pinorama-studio/src/hooks/use-pinorama-introspection.ts @@ -10,7 +10,6 @@ export const usePinoramaIntrospection = () => { const response: any = await client?.introspection() return response }, - enabled: !!client, refetchInterval: 3000 }) diff --git a/packages/pinorama-studio/src/hooks/use-pinorama-styles.ts b/packages/pinorama-studio/src/hooks/use-pinorama-styles.ts index bbf50c0..e9a51f4 100644 --- a/packages/pinorama-studio/src/hooks/use-pinorama-styles.ts +++ b/packages/pinorama-studio/src/hooks/use-pinorama-styles.ts @@ -11,7 +11,6 @@ export const usePinoramaStyles = () => { const response: any = await client?.styles() return response }, - enabled: !!client, staleTime: Number.POSITIVE_INFINITY })