This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Added an embed mode which works via the URL, not any data in our db #323
Open
mattpocock
wants to merge
10
commits into
dev
Choose a base branch
from
matt/added-an-embed-mode-which-works-via-the-url-not-any-data-in-our-db
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fab734d
Added an embed mode which works via the URL, not any data in our db
mattpocock 8697c33
Completed work
mattpocock 3ec5dbc
Update src/pages/view-only.tsx
mattpocock 170610b
Update src/pages/view-only.tsx
mattpocock bddbdca
Update src/pages/view-only.tsx
mattpocock f8eab8e
Update src/pages/view-only.tsx
mattpocock 29e3bcb
Update src/withReadyRouter.tsx
mattpocock 38f1959
Empty commit for visualisation
mattpocock 850025f
Respond to feedback
mattpocock 317bb3e
Empty commit for visualisation
mattpocock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { createContext, useContext } from 'react'; | ||
import { EmbedContext } from './types'; | ||
import { createRequiredContext } from './utils'; | ||
|
||
export const [EmbedProvider, useEmbed] = createRequiredContext< | ||
EmbedContext | undefined | ||
>('Embed'); | ||
const EmbedReactContext = createContext(null as EmbedContext); | ||
|
||
export const EmbedProvider = EmbedReactContext.Provider; | ||
|
||
export const useEmbed = () => useContext(EmbedReactContext); |
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,148 @@ | ||
import { Box, ChakraProvider } from '@chakra-ui/react'; | ||
import { useInterpret, useMachine } from '@xstate/react'; | ||
import Head from 'next/head'; | ||
import { NextRouter, useRouter } from 'next/router'; | ||
import React, { useEffect, useMemo } from 'react'; | ||
import { createMachine, MachineConfig, sendParent } from 'xstate'; | ||
import { CanvasProvider } from '../CanvasContext'; | ||
import { CanvasView } from '../CanvasView'; | ||
import { EmbedProvider } from '../embedContext'; | ||
import { SimulationProvider } from '../SimulationContext'; | ||
import { simModel, simulationMachine } from '../simulationMachine'; | ||
import { theme } from '../theme'; | ||
import { NextComponentWithMeta } from '../types'; | ||
import { useInterpretCanvas } from '../useInterpretCanvas'; | ||
import { parseEmbedQuery, withoutEmbedQueryParams } from '../utils'; | ||
import { withReadyRouter } from '../withReadyRouter'; | ||
import lzString from 'lz-string'; | ||
|
||
const parseMachineFromQuery = (query: NextRouter['query']) => { | ||
if (!query.machine) { | ||
throw new Error("`machine` query param is required"); | ||
} | ||
|
||
if (Array.isArray(query.machine)) { | ||
throw new Error("`machine` query param can't be an array"); | ||
} | ||
|
||
const lzResult = lzString.decompressFromEncodedURIComponent(query.machine); | ||
|
||
if (!lzResult) throw new Error("`machine` query param couldn't be decompressed"); | ||
|
||
const machineConfig = JSON.parse(lzResult); | ||
|
||
// Tests that the machine is valid | ||
try { | ||
return createMachine(machineConfig); | ||
} catch { | ||
throw new Error("decompressed `machine` couldn't be used to `createMachine`") | ||
} | ||
}; | ||
|
||
const viewOnlyPageMachine = createMachine<{ | ||
query: NextRouter['query']; | ||
}>({ | ||
initial: 'checkingIfMachineIsValid', | ||
states: { | ||
checkingIfMachineIsValid: { | ||
invoke: { | ||
src: async (context) => { | ||
return parseMachineFromQuery(context.query); | ||
}, | ||
onDone: { | ||
target: 'valid', | ||
}, | ||
onError: { | ||
target: 'notValid', | ||
}, | ||
}, | ||
}, | ||
notValid: { | ||
type: 'final', | ||
entry: (context, event) => { | ||
console.error('Could not parse machine.', event); | ||
}, | ||
}, | ||
valid: { | ||
type: 'final', | ||
entry: sendParent((context) => | ||
simModel.events['MACHINES.REGISTER']([ | ||
parseMachineFromQuery(context.query), | ||
]), | ||
), | ||
}, | ||
}, | ||
}); | ||
|
||
/** | ||
* Displays a view-only page which can render a machine | ||
* to the canvas from the URL | ||
* | ||
* Use this example URL: http://localhost:3000/viz/view-only?machine=N4IglgdmAuYIYBsQC4QHcD2aQBoQGdo5oBTfFUTbZYAXzwhOrttqA | ||
* | ||
* This is for loading OG images quickly, and for many other applications | ||
* | ||
* To create the machine hash, use the lzString.compressToEncodedURIComponent | ||
* function on a JSON.stringified machine config. | ||
* | ||
* You can also use the typical embed controls | ||
*/ | ||
const ViewOnlyPage = withReadyRouter(() => { | ||
const canvasService = useInterpretCanvas({ | ||
sourceID: null, | ||
}); | ||
const simulationService = useInterpret(simulationMachine); | ||
const router = useRouter(); | ||
|
||
useMachine(viewOnlyPageMachine, { | ||
mattpocock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
context: { | ||
query: router.query, | ||
}, | ||
parent: simulationService, | ||
}); | ||
|
||
const embed = useMemo( | ||
() => ({ | ||
...parseEmbedQuery(router.query), | ||
isEmbedded: true, | ||
originalUrl: withoutEmbedQueryParams(router.query), | ||
}), | ||
[router.query], | ||
); | ||
|
||
return ( | ||
<> | ||
mattpocock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<EmbedProvider value={embed}> | ||
<ChakraProvider theme={theme}> | ||
<CanvasProvider value={canvasService}> | ||
<SimulationProvider value={simulationService}> | ||
<Box | ||
data-testid="app" | ||
data-viz-theme="dark" | ||
as="main" | ||
height="100vh" | ||
> | ||
<CanvasView /> | ||
</Box> | ||
</SimulationProvider> | ||
</CanvasProvider> | ||
</ChakraProvider> | ||
</EmbedProvider> | ||
</> | ||
); | ||
}); | ||
|
||
const ViewOnlyPageParent: NextComponentWithMeta = () => { | ||
return ( | ||
<> | ||
<Head> | ||
<script src="https://unpkg.com/elkjs@0.7.1/lib/elk.bundled.js"></script> | ||
</Head> | ||
<ViewOnlyPage /> | ||
</> | ||
); | ||
}; | ||
|
||
ViewOnlyPageParent.preventAuth = true; | ||
|
||
export default ViewOnlyPageParent; |
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,25 @@ | ||
import { useRouter } from 'next/router'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Ensures that Next's router is always ready (i.e. has | ||
* query params loaded) | ||
*/ | ||
export const withReadyRouter = (WrappedComponent: any) => { | ||
WrappedComponent.displayName = `WithReadyRouter${WrappedComponent.displayName}`; | ||
mattpocock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const WithReadyRouter = () => { | ||
const router = useRouter(); | ||
const [isReady, setIsReady] = useState(false); | ||
|
||
useEffect(() => { | ||
setIsReady(router.isReady); | ||
}, [router.isReady]); | ||
|
||
if (!isReady) return null; | ||
|
||
return <WrappedComponent />; | ||
}; | ||
|
||
return WithReadyRouter; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
View on Stately Viz