-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PlainReact: Expose scene features through contexts and hooks and norm…
…al react components (#734) Co-authored-by: Oscar Kilhed <[email protected]>
- Loading branch information
1 parent
207b3f5
commit 80f7384
Showing
77 changed files
with
2,876 additions
and
933 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,6 @@ node_modules | |
build | ||
|
||
.env | ||
|
||
# turbo | ||
.turbo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"packages": [ | ||
"packages/*" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,19 +30,19 @@ | |
"prepare": "husky install", | ||
"packages:publish": "lerna exec --no-private -- npm publish", | ||
"docs": "yarn workspace website run start --port 8080", | ||
"docs:build": "yarn workspace website run build", | ||
"test:lib": "lerna run test --scope '@grafana/scenes' --", | ||
"dev:lib": "lerna run dev --scope '@grafana/scenes' --", | ||
"dev:app": "lerna run dev --scope 'scenes-app' --", | ||
"test": "lerna run test --scope '@grafana/scenes' -- --watch", | ||
"typecheck": "lerna run typecheck" | ||
"docs:build": "yarn turbo build --filter=website", | ||
"build": "yarn turbo build --filter=!website", | ||
"test:scenes": "yarn turbo test --filter=@grafana/scenes -- --watch", | ||
"test:scenes-react": "yarn turbo test --filter=@grafana/scenes-react -- --watch", | ||
"dev": "yarn turbo dev --filter=!website", | ||
"test": "yarn turbo test --filter=!website", | ||
"typecheck": "yarn turbo typecheck --filter=!website" | ||
}, | ||
"resolutions": { | ||
"@types/react": "18.2.74" | ||
}, | ||
"packageManager": "[email protected]", | ||
"workspaces": [ | ||
".", | ||
"packages/*", | ||
"docusaurus/website" | ||
], | ||
|
@@ -51,7 +51,10 @@ | |
"@auto-it/released": "^11.0.7", | ||
"@testing-library/react": "^14.1.2", | ||
"auto": "^11.0.7", | ||
"eslint": "^8.57.0", | ||
"lerna": "^6.5.1", | ||
"lint-staged": "^13.2.0" | ||
"lint-staged": "^13.2.0", | ||
"prettier": "^3.2.5", | ||
"turbo": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
EmbeddedScene, | ||
SceneAppPage, | ||
SceneAppPageState, | ||
SceneComponentProps, | ||
SceneFlexItem, | ||
SceneFlexLayout, | ||
SceneObjectBase, | ||
SceneObjectState, | ||
VizPanel, | ||
} from '@grafana/scenes'; | ||
import { getEmbeddedSceneDefaults, getQueryRunnerWithRandomWalkQuery } from './utils'; | ||
import React from 'react'; | ||
import { useTimeRange } from '@grafana/scenes-react'; | ||
|
||
export function getInteropDemo(defaults: SceneAppPageState) { | ||
return new SceneAppPage({ | ||
...defaults, | ||
subTitle: 'Testing using the hooks and plain react components from normal scene', | ||
getScene: () => { | ||
return new EmbeddedScene({ | ||
...getEmbeddedSceneDefaults(), | ||
// context: new SceneContextObject({}), | ||
key: 'Flex layout embedded scene', | ||
body: new SceneFlexLayout({ | ||
direction: 'column', | ||
children: [ | ||
new SceneFlexItem({ | ||
body: new VizPanel({ | ||
title: 'Graph', | ||
pluginId: 'timeseries', | ||
$data: getQueryRunnerWithRandomWalkQuery({}), | ||
}), | ||
}), | ||
new SceneFlexItem({ | ||
body: new CustomSceneObject({}), | ||
}), | ||
], | ||
}), | ||
}); | ||
}, | ||
}); | ||
} | ||
|
||
class CustomSceneObject extends SceneObjectBase<SceneObjectState> { | ||
static Component = ({ model }: SceneComponentProps<CustomSceneObject>) => { | ||
const [timeRange, _] = useTimeRange(); | ||
|
||
return <div>Time hook: {timeRange.from.toString()}</div>; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { VizConfigBuilders } from '@grafana/scenes'; | ||
import { Breadcrumb, BreadcrumbProvider, VizPanel, useQueryRunner } from '@grafana/scenes-react'; | ||
import React from 'react'; | ||
import { DATASOURCE_REF } from '../constants'; | ||
import { PageWrapper } from './PageWrapper'; | ||
import { DemoVizLayout, urlBase } from './utils'; | ||
import { Route, Switch, useParams } from 'react-router-dom'; | ||
import { PlainGraphWithRandomWalk } from './PlainGraphWithRandomWalk'; | ||
|
||
export function DrilldownDemoPage() { | ||
/** | ||
* The breadcrumb provider should really be wrapping all routes, but placing it here just to show the concept and so far this is the only pages that use it. | ||
* It needs to be above the PageWrapper as it's the the component that uses the BreadcrumbContext | ||
*/ | ||
return ( | ||
<BreadcrumbProvider> | ||
<Breadcrumb text="Drilldown demo" path={`${urlBase}/drilldown`} /> | ||
<Switch> | ||
<Route path={`${urlBase}/drilldown`} component={DrilldownHome} exact /> | ||
<Route path={`${urlBase}/drilldown/lib/:lib`} component={DrilldownLibraryPage} /> | ||
</Switch> | ||
</BreadcrumbProvider> | ||
); | ||
} | ||
|
||
export function DrilldownHome() { | ||
const dataProvider = useQueryRunner({ | ||
queries: [ | ||
{ | ||
scenarioId: 'csv_file', | ||
refId: 'A', | ||
csvFileName: 'js_libraries.csv', | ||
}, | ||
], | ||
datasource: DATASOURCE_REF, | ||
}); | ||
|
||
return ( | ||
<PageWrapper title="Drilldown demo" subTitle="The top level page (for the demo)"> | ||
<DemoVizLayout> | ||
<VizPanel title="JS Libraries" dataProvider={dataProvider} viz={tableWithDrilldown} /> | ||
</DemoVizLayout> | ||
</PageWrapper> | ||
); | ||
} | ||
|
||
export function DrilldownLibraryPage() { | ||
const libraryName = useParams<{ lib: string }>().lib; | ||
|
||
return ( | ||
<PageWrapper title={`Library: ${libraryName}`} subTitle="Library details drilldown page"> | ||
<DemoVizLayout> | ||
<PlainGraphWithRandomWalk title={`${libraryName} trends`} /> | ||
</DemoVizLayout> | ||
</PageWrapper> | ||
); | ||
} | ||
|
||
export const tableWithDrilldown = VizConfigBuilders.table() | ||
.setOverrides((b) => | ||
b.matchFieldsWithName('Library').overrideLinks([ | ||
{ | ||
title: 'Go to library details', | ||
url: '${__url.path}/lib/${__value.text}', | ||
}, | ||
]) | ||
) | ||
.build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useQueryRunner, VizPanel } from '@grafana/scenes-react'; | ||
import { Field, Select, Stack } from '@grafana/ui'; | ||
import React, { useMemo, useState } from 'react'; | ||
import { DATASOURCE_REF } from '../constants'; | ||
import { PageWrapper } from './PageWrapper'; | ||
import { toOption } from '@grafana/data'; | ||
import { SceneDataQuery } from '@grafana/scenes'; | ||
import { plainGraph } from './visualizations'; | ||
import { DemoVizLayout } from './utils'; | ||
|
||
export function DynamicQueriesPage() { | ||
const scenarios = ['Slow query', 'Random walk'].map(toOption); | ||
const [scenario, setScenario] = useState<string>('Random walk'); | ||
const queries = useMemo(() => buildQueriesForScenario(scenario), [scenario]); | ||
|
||
const dataProvider = useQueryRunner({ queries: queries, maxDataPoints: 100, datasource: DATASOURCE_REF }); | ||
|
||
return ( | ||
<PageWrapper title="Dynamic queriues" subTitle="Rebuild queries based on some user input / state"> | ||
<Stack direction="column"> | ||
<Stack> | ||
<Field label="Query scenario"> | ||
<Select value={scenario} options={scenarios} onChange={(x) => setScenario(x.value!)} /> | ||
</Field> | ||
</Stack> | ||
<DemoVizLayout> | ||
<VizPanel title={scenario} dataProvider={dataProvider} viz={plainGraph} /> | ||
</DemoVizLayout> | ||
</Stack> | ||
</PageWrapper> | ||
); | ||
} | ||
|
||
function buildQueriesForScenario(scenario: string): SceneDataQuery[] { | ||
switch (scenario) { | ||
case 'Random walk': | ||
return [ | ||
{ | ||
refId: 'A', | ||
scenarioId: 'random_walk', | ||
alias: 'random walk', | ||
}, | ||
]; | ||
case 'Slow query': | ||
default: | ||
return [ | ||
{ | ||
refId: 'A', | ||
scenarioId: 'slow_query', | ||
alias: 'Slow query', | ||
stringInput: '2s', | ||
}, | ||
]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/scenes-app/src/react-demo/DynamicVariablesPage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CustomVariable, VariableSelect } from '@grafana/scenes-react'; | ||
import { Stack } from '@grafana/ui'; | ||
import React from 'react'; | ||
import { PageWrapper } from './PageWrapper'; | ||
import { PlainGraphWithRandomWalk } from './PlainGraphWithRandomWalk'; | ||
import { DemoVizLayout } from './utils'; | ||
|
||
export function DynamicVariablesPage() { | ||
return ( | ||
<PageWrapper title="Dynamic variables" subTitle="Variables added via react rendering"> | ||
<CustomVariable name="job" query="A, B, C" initialValue="A"> | ||
<Stack direction="column"> | ||
<Stack> | ||
<VariableSelect name="job" /> | ||
</Stack> | ||
<DemoVizLayout> | ||
<PlainGraphWithRandomWalk title={'Testing job = $job'} queryAlias="job = $job" /> | ||
</DemoVizLayout> | ||
</Stack> | ||
</CustomVariable> | ||
</PageWrapper> | ||
); | ||
} |
Oops, something went wrong.