From 2ffc191d15b9e7ce77992883eff79a66a2e52ca6 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Thu, 27 Mar 2025 15:24:05 +0100 Subject: [PATCH 1/2] Use a global Config context instead of passing props along all the components --- src/components/App.tsx | 24 +++++---- src/components/Breadcrumb.tsx | 10 ++-- src/components/Cell.tsx | 9 ++-- src/components/File.tsx | 13 ++--- src/components/Folder.tsx | 14 ++--- src/components/Page.tsx | 12 ++--- src/components/index.ts | 11 ++-- src/components/viewers/ParquetView.tsx | 23 +++----- src/components/viewers/SlidePanel.tsx | 18 ++----- src/components/viewers/Viewer.tsx | 21 ++------ src/components/viewers/index.ts | 7 ++- src/hooks/useConfig.ts | 60 +++++++++++++++++++++ src/index.ts | 2 + src/lib/index.ts | 1 - src/lib/routes.ts | 7 --- test/components/File.test.tsx | 13 +++-- test/components/Folder.test.tsx | 14 +++-- test/components/viewers/SlidePanel.test.tsx | 42 ++++++++------- 18 files changed, 166 insertions(+), 135 deletions(-) create mode 100644 src/hooks/useConfig.ts diff --git a/src/components/App.tsx b/src/components/App.tsx index 286371e4..3ddef9bc 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1,3 +1,5 @@ +import { useMemo } from 'react' +import { Config, ConfigProvider } from '../hooks/useConfig.js' import { getHttpSource } from '../lib/sources/httpSource.js' import { getHyperparamSource } from '../lib/sources/hyperparamSource.js' import Page from './Page.js' @@ -10,20 +12,20 @@ export default function App() { const source = getHttpSource(sourceId) ?? getHyperparamSource(sourceId, { endpoint: location.origin }) + // Use memo to avoid creating a new object on each render + const config: Config = useMemo(() => ({ + routes: { + getSourceRouteUrl: ({ sourceId }) => `/files?key=${sourceId}`, + getCellRouteUrl: ({ sourceId, col, row }) => `/files?key=${sourceId}&col=${col}&row=${row}`, + }, + }), []) + if (!source) { return
Could not load a data source. You have to pass a valid source in the url.
} return ( - `/files?key=${sourceId}`, - getCellRouteUrl: ({ sourceId, col, row }) => `/files?key=${sourceId}&col=${col}&row=${row}`, - }, - }} - /> + + + ) } diff --git a/src/components/Breadcrumb.tsx b/src/components/Breadcrumb.tsx index 4702c7c0..9f264788 100644 --- a/src/components/Breadcrumb.tsx +++ b/src/components/Breadcrumb.tsx @@ -1,22 +1,22 @@ import type { ReactNode } from 'react' -import type { RoutesConfig } from '../lib/routes.js' +import { useConfig } from '../hooks/useConfig.js' import type { Source } from '../lib/sources/types.js' -export type BreadcrumbConfig = RoutesConfig interface BreadcrumbProps { source: Source, - config?: BreadcrumbConfig children?: ReactNode } /** * Breadcrumb navigation */ -export default function Breadcrumb({ source, config, children }: BreadcrumbProps) { +export default function Breadcrumb({ source, children }: BreadcrumbProps) { + const { routes } = useConfig() + return