|
| 1 | +import type { IconName } from '@blueprintjs/icons'; |
| 2 | +import { IconNames } from '@blueprintjs/icons'; |
| 3 | +import styled from '@emotion/styled'; |
| 4 | +import init from '@zakodium/nmrium-core-plugins'; |
| 5 | +import { |
| 6 | + createContext, |
| 7 | + useCallback, |
| 8 | + useContext, |
| 9 | + useMemo, |
| 10 | + useState, |
| 11 | +} from 'react'; |
| 12 | +import { Toolbar } from 'react-science/ui'; |
| 13 | + |
| 14 | +import { NMRium } from '../../component/main/index.js'; |
| 15 | + |
| 16 | +import type { ViewProps } from './View.helpers.js'; |
| 17 | +import { useView } from './View.helpers.js'; |
| 18 | + |
| 19 | +const Container = styled.section` |
| 20 | + display: flex; |
| 21 | + flex-direction: column; |
| 22 | + height: 100%; |
| 23 | +
|
| 24 | + & > h1 { |
| 25 | + font-weight: 700; |
| 26 | + font-size: 1.5em; |
| 27 | + line-height: 1.4em; |
| 28 | + padding: 0.75em; |
| 29 | + } |
| 30 | +`; |
| 31 | + |
| 32 | +const NMRiumContainer = styled.div` |
| 33 | + flex: 1; |
| 34 | +`; |
| 35 | + |
| 36 | +const DemoContext = createContext<{ |
| 37 | + setRandom: () => void; |
| 38 | + icon: IconName; |
| 39 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 40 | +}>({ icon: 'console', setRandom: () => {} }); |
| 41 | + |
| 42 | +function DemoTopBarRight() { |
| 43 | + const { setRandom, icon } = useContext(DemoContext); |
| 44 | + |
| 45 | + return ( |
| 46 | + <Toolbar> |
| 47 | + <Toolbar.Item |
| 48 | + id="demo-topbar-right" |
| 49 | + icon={icon} |
| 50 | + tooltip="Demo topbar right" |
| 51 | + onClick={setRandom} |
| 52 | + /> |
| 53 | + </Toolbar> |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +const core = init([ |
| 58 | + { |
| 59 | + id: 'demo-plugin-topbar', |
| 60 | + version: 1, |
| 61 | + migrations: [], |
| 62 | + ui: { |
| 63 | + 'topbar.right': DemoTopBarRight, |
| 64 | + }, |
| 65 | + }, |
| 66 | +]); |
| 67 | + |
| 68 | +const possibleIcons = Object.values(IconNames); |
| 69 | +export default function PluginUITopBar(props: ViewProps) { |
| 70 | + const [data, otherProps] = useView(props); |
| 71 | + |
| 72 | + const { workspace, customWorkspaces } = otherProps; |
| 73 | + |
| 74 | + const [icon, setIcon] = useState<IconName>('console'); |
| 75 | + const setRandom = useCallback(() => { |
| 76 | + const randomIndex = Math.floor(Math.random() * possibleIcons.length); |
| 77 | + setIcon(possibleIcons[randomIndex]); |
| 78 | + }, []); |
| 79 | + const context = useMemo(() => ({ setRandom, icon }), [icon, setRandom]); |
| 80 | + |
| 81 | + return ( |
| 82 | + <Container> |
| 83 | + <h1>TopBar Plugin</h1> |
| 84 | + |
| 85 | + <NMRiumContainer> |
| 86 | + <DemoContext.Provider value={context}> |
| 87 | + <NMRium |
| 88 | + core={core} |
| 89 | + data={data} |
| 90 | + {...(workspace && { workspace })} |
| 91 | + {...(customWorkspaces && { customWorkspaces })} |
| 92 | + /> |
| 93 | + </DemoContext.Provider> |
| 94 | + </NMRiumContainer> |
| 95 | + </Container> |
| 96 | + ); |
| 97 | +} |
0 commit comments