Skip to content

Commit 8e523b8

Browse files
authored
fix(history): fix history caching (#262)
1 parent e57ebf8 commit 8e523b8

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

src/hooks/usePath.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
recoverHistory,
1212
clearHistory,
1313
me,
14-
recordHistory,
1514
} from "~/store"
1615
import {
1716
fsGet,
@@ -137,7 +136,6 @@ export const usePath = () => {
137136
ObjStore.setRelated(data.related ?? [])
138137
ObjStore.setRawUrl(data.raw_url)
139138
ObjStore.setState(State.File)
140-
recordHistory(path, index)
141139
}
142140
},
143141
handleErr,
@@ -175,7 +173,6 @@ export const usePath = () => {
175173
ObjStore.setWrite(data.write)
176174
ObjStore.setProvider(data.provider)
177175
ObjStore.setState(State.Folder)
178-
recordHistory(path, index ?? 1)
179176
},
180177
handleErr,
181178
)

src/hooks/useRouter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ const useRouter = () => {
3131
navigate(path, options)
3232
},
3333
replace: (to: string) => {
34-
navigate(encodePath(pathJoin(pathDir(location.pathname), to), true))
34+
const path = encodePath(pathJoin(pathDir(location.pathname), to), true)
35+
clearHistory(decodeURIComponent(path))
36+
navigate(path)
3537
},
3638
pushHref: (to: string): string => {
3739
return encodePath(pathJoin(pathname(), to))

src/pages/home/Obj.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
getPagination,
1616
objStore,
1717
password,
18-
recordScroll,
18+
recordHistory,
1919
setPassword,
2020
/*layout,*/ State,
2121
} from "~/store"
@@ -37,15 +37,15 @@ export const Obj = () => {
3737
const pagination = getPagination()
3838
const page = createMemo(() => {
3939
return pagination.type === "pagination"
40-
? parseInt(searchParams["page"]) || 1
40+
? parseInt(searchParams["page"], 10) || 1
4141
: undefined
4242
})
4343
let lastPathname: string
4444
let lastPage: number | undefined
4545
createEffect(
4646
on([pathname, page], async ([pathname, page]) => {
4747
if (lastPathname) {
48-
recordScroll(lastPathname, lastPage)
48+
recordHistory(lastPathname, lastPage)
4949
}
5050
lastPathname = pathname
5151
lastPage = page

src/pages/manage/tasks/helper.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { createSignal, JSX } from "solid-js"
22
import { me } from "~/store"
33
import { TaskNameAnalyzer } from "./Tasks"
4-
import { useRouter, useT } from "~/hooks"
5-
import { encodePath } from "~/utils"
4+
import { useT } from "~/hooks"
65

76
export const getPath = (
87
device: string,
@@ -13,18 +12,15 @@ export const getPath = (
1312
const prefix = me().base_path === "/" ? "" : me().base_path
1413
const accessible = fullPath.startsWith(prefix)
1514
const [underline, setUnderline] = createSignal(false)
16-
const { to } = useRouter()
1715
return accessible && asLink ? (
18-
<p
16+
<a
1917
style={underline() ? "text-decoration: underline" : ""}
2018
onMouseOver={() => setUnderline(true)}
2119
onMouseOut={() => setUnderline(false)}
22-
on:click={(_: MouseEvent) => {
23-
to(encodePath(fullPath.slice(prefix.length)))
24-
}}
20+
href={fullPath.slice(prefix.length)}
2521
>
2622
{fullPath}
27-
</p>
23+
</a>
2824
) : (
2925
<p>{fullPath}</p>
3026
)

src/store/history.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const waitForNextFrame = () => {
1414
}
1515

1616
export const getHistoryKey = (path: string, page?: number) => {
17-
return page && page > 1 ? `${path}?page=${page}` : path
17+
const pathname = path.split("?")[0]
18+
return page && page > 1 ? `${pathname}?page=${page}` : pathname
1819
}
1920

2021
export const recordHistory = (path: string, page?: number) => {
@@ -33,15 +34,8 @@ export const recordHistory = (path: string, page?: number) => {
3334
page: page ?? getGlobalPage(),
3435
scroll: window.scrollY,
3536
}
36-
console.log(`record history: [${key}]`)
3737
HistoryMap.set(key, history)
38-
}
39-
40-
export const recordScroll = (path: string, page?: number) => {
41-
const key = getHistoryKey(path, page)
42-
if (!HistoryMap.has(key)) return
43-
const history = HistoryMap.get(key)!
44-
history.scroll = window.scrollY
38+
console.log(`record history: [${key}]`)
4539
}
4640

4741
export const recoverHistory = async (path: string, page?: number) => {
@@ -63,5 +57,21 @@ export const hasHistory = (path: string, page?: number) => {
6357

6458
export const clearHistory = (path: string, page?: number) => {
6559
const key = getHistoryKey(path, page)
66-
HistoryMap.delete(key)
60+
if (hasHistory(path, page)) {
61+
HistoryMap.delete(key)
62+
console.log(`clear history: [${key}]`)
63+
}
6764
}
65+
66+
document.addEventListener(
67+
"click",
68+
(e) => {
69+
let target = e.target as HTMLElement
70+
let link = target.closest("a")
71+
let path = link?.getAttribute("href")
72+
if (path) {
73+
clearHistory(decodeURIComponent(path))
74+
}
75+
},
76+
true,
77+
)

0 commit comments

Comments
 (0)