one
Version:
One is a new React Framework that makes Vite serve both native and web.
53 lines (41 loc) • 1.39 kB
text/typescript
import { isWebClient } from '../constants'
import type { OneRouter } from '../interfaces/router'
import {
getLinkingConfig as createLinkingConfig,
type OneLinkingOptions,
} from './getLinkingConfig'
import type { RouteNode } from './Route'
let linkingConfig: OneLinkingOptions | undefined
export function getLinking() {
return linkingConfig
}
export function setLinking(_: OneLinkingOptions) {
linkingConfig = _
}
export function resetLinking() {
linkingConfig = undefined
}
export function setupLinking(
routeNode: RouteNode | null,
initialLocation?: URL
): OneRouter.ResultState | undefined {
let initialState: OneRouter.ResultState | undefined
if (routeNode) {
linkingConfig = createLinkingConfig(routeNode)
if (initialLocation) {
linkingConfig.getInitialURL = () => initialLocation.toString()
let path = initialLocation.pathname + (initialLocation.search || '')
if (isWebClient) {
// Check history.state for temp location (client-only)
const historyState = window.history.state
if (historyState?.__tempLocation?.pathname && !historyState.__tempKey) {
path =
historyState.__tempLocation.pathname +
(historyState.__tempLocation.search || '')
}
}
initialState = linkingConfig.getStateFromPath?.(path, linkingConfig.config)
}
}
return initialState
}