fix: browser history
This commit is contained in:
parent
a014b4e6cb
commit
f709c50809
5 changed files with 34 additions and 22 deletions
|
@ -5,7 +5,7 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"android": "react-native run-android",
|
"android": "react-native run-android",
|
||||||
"ios": "react-native run-ios --simulator=\"iPhone 14\"",
|
"ios": "react-native run-ios --simulator=\"iPhone 14\"",
|
||||||
"web": "webpack-dev-server --config ./web/webpack.config.js -d inline-source-map --hot --color",
|
"web": "webpack-dev-server --config ./web/webpack.config.js -d inline-source-map --hot --color --history-api-fallback",
|
||||||
"start": "react-native start",
|
"start": "react-native start",
|
||||||
"clean-cache": "rm -rf node_modules/.cache/babel-loader/*",
|
"clean-cache": "rm -rf node_modules/.cache/babel-loader/*",
|
||||||
"test": "jest --forceExit --testTimeout=20000 --bail",
|
"test": "jest --forceExit --testTimeout=20000 --bail",
|
||||||
|
@ -39,6 +39,7 @@
|
||||||
"base64-js": "^1.5.1",
|
"base64-js": "^1.5.1",
|
||||||
"email-validator": "^2.0.4",
|
"email-validator": "^2.0.4",
|
||||||
"he": "^1.2.0",
|
"he": "^1.2.0",
|
||||||
|
"history": "^5.3.0",
|
||||||
"lodash.chunk": "^4.2.0",
|
"lodash.chunk": "^4.2.0",
|
||||||
"lodash.clonedeep": "^4.5.0",
|
"lodash.clonedeep": "^4.5.0",
|
||||||
"lodash.isequal": "^4.5.0",
|
"lodash.isequal": "^4.5.0",
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import React, {useState, useEffect} from 'react'
|
import React, {useState, useEffect} from 'react'
|
||||||
import {SafeAreaProvider} from 'react-native-safe-area-context'
|
import {SafeAreaProvider} from 'react-native-safe-area-context'
|
||||||
|
import {getInitialURL} from 'platform/urls'
|
||||||
import * as view from './view/index'
|
import * as view from './view/index'
|
||||||
import {RootStoreModel, setupState, RootStoreProvider} from './state'
|
import {RootStoreModel, setupState, RootStoreProvider} from './state'
|
||||||
import {WebShell} from './view/shell/web'
|
import {WebShell} from './view/shell/web'
|
||||||
|
@ -13,7 +14,14 @@ function App() {
|
||||||
// init
|
// init
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
view.setup()
|
view.setup()
|
||||||
setupState().then(setRootStore)
|
setupState().then(store => {
|
||||||
|
setRootStore(store)
|
||||||
|
getInitialURL().then(url => {
|
||||||
|
if (url) {
|
||||||
|
store.nav.handleLink(url)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
// show nothing prior to init
|
// show nothing prior to init
|
||||||
|
|
|
@ -1,31 +1,20 @@
|
||||||
import {Linking} from 'react-native'
|
import {Linking} from 'react-native'
|
||||||
import {isIOS, isAndroid, isNative, isWeb} from './detection'
|
import {createBrowserHistory, createMemoryHistory} from 'history'
|
||||||
|
import {isNative, isWeb} from './detection'
|
||||||
|
|
||||||
export function makeAppUrl(path = '') {
|
export async function getInitialURL(): Promise<string | undefined> {
|
||||||
if (isIOS) {
|
|
||||||
return `bskyapp://${path}`
|
|
||||||
} else if (isAndroid) {
|
|
||||||
return `bsky://app${path}`
|
|
||||||
} else {
|
|
||||||
// @ts-ignore window exists -prf
|
|
||||||
return `${window.location.origin}${path}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractHashFragment(url: string): string {
|
|
||||||
return url.split('#')[1] || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getInitialURL(): Promise<string> {
|
|
||||||
if (isNative) {
|
if (isNative) {
|
||||||
const url = await Linking.getInitialURL()
|
const url = await Linking.getInitialURL()
|
||||||
if (url) {
|
if (url) {
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
return makeAppUrl()
|
return undefined
|
||||||
} else {
|
} else {
|
||||||
// @ts-ignore window exists -prf
|
// @ts-ignore window exists -prf
|
||||||
return window.location.toString()
|
if (window.location.pathname !== '/') {
|
||||||
|
return window.location.pathname
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,3 +24,11 @@ export function clearHash() {
|
||||||
window.location.hash = ''
|
window.location.hash = ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getHistory() {
|
||||||
|
if (isWeb) {
|
||||||
|
return createBrowserHistory()
|
||||||
|
} else {
|
||||||
|
return createMemoryHistory()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {RootStoreModel} from './root-store'
|
||||||
import {makeAutoObservable} from 'mobx'
|
import {makeAutoObservable} from 'mobx'
|
||||||
import {TABS_ENABLED} from 'lib/build-flags'
|
import {TABS_ENABLED} from 'lib/build-flags'
|
||||||
import {segmentClient} from 'lib/analytics'
|
import {segmentClient} from 'lib/analytics'
|
||||||
|
import {getHistory} from 'platform/urls'
|
||||||
|
|
||||||
let __id = 0
|
let __id = 0
|
||||||
function genId() {
|
function genId() {
|
||||||
|
@ -41,6 +42,7 @@ export type HistoryPtr = string // `{tabId}-{historyId}`
|
||||||
export class NavigationTabModel {
|
export class NavigationTabModel {
|
||||||
id = genId()
|
id = genId()
|
||||||
history: HistoryItem[]
|
history: HistoryItem[]
|
||||||
|
browserHistory: any
|
||||||
index = 0
|
index = 0
|
||||||
isNewTab = false
|
isNewTab = false
|
||||||
|
|
||||||
|
@ -48,6 +50,7 @@ export class NavigationTabModel {
|
||||||
this.history = [
|
this.history = [
|
||||||
{url: TabPurposeMainPath[fixedTabPurpose], ts: Date.now(), id: genId()},
|
{url: TabPurposeMainPath[fixedTabPurpose], ts: Date.now(), id: genId()},
|
||||||
]
|
]
|
||||||
|
this.browserHistory = getHistory()
|
||||||
makeAutoObservable(this, {
|
makeAutoObservable(this, {
|
||||||
serialize: false,
|
serialize: false,
|
||||||
hydrate: false,
|
hydrate: false,
|
||||||
|
@ -122,6 +125,7 @@ export class NavigationTabModel {
|
||||||
this.history.push({url: fixedUrl, ts: Date.now(), id: genId()})
|
this.history.push({url: fixedUrl, ts: Date.now(), id: genId()})
|
||||||
}
|
}
|
||||||
this.history.push({url, title, ts: Date.now(), id: genId()})
|
this.history.push({url, title, ts: Date.now(), id: genId()})
|
||||||
|
this.browserHistory.push(url)
|
||||||
this.index = this.history.length - 1
|
this.index = this.history.length - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,6 +146,7 @@ export class NavigationTabModel {
|
||||||
goBack() {
|
goBack() {
|
||||||
if (this.canGoBack) {
|
if (this.canGoBack) {
|
||||||
this.index--
|
this.index--
|
||||||
|
this.browserHistory.back()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,12 +160,14 @@ export class NavigationTabModel {
|
||||||
goForward() {
|
goForward() {
|
||||||
if (this.canGoForward) {
|
if (this.canGoForward) {
|
||||||
this.index++
|
this.index++
|
||||||
|
this.browserHistory.forward()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
goToIndex(index: number) {
|
goToIndex(index: number) {
|
||||||
if (index >= 0 && index <= this.history.length - 1) {
|
if (index >= 0 && index <= this.history.length - 1) {
|
||||||
this.index = index
|
this.index = index
|
||||||
|
this.browserHistory.go(index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -120,7 +120,6 @@ export class RootStoreModel {
|
||||||
async handleSessionChange(agent: AtpAgent) {
|
async handleSessionChange(agent: AtpAgent) {
|
||||||
this.log.debug('RootStoreModel:handleSessionChange')
|
this.log.debug('RootStoreModel:handleSessionChange')
|
||||||
this.agent = agent
|
this.agent = agent
|
||||||
this.nav.clear()
|
|
||||||
this.me.clear()
|
this.me.clear()
|
||||||
await this.me.load()
|
await this.me.load()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue