ignore: refactoring and tests (#12460)
This commit is contained in:
63
packages/app/src/components/titlebar-history.test.ts
Normal file
63
packages/app/src/components/titlebar-history.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { applyPath, backPath, forwardPath, type TitlebarHistory } from "./titlebar-history"
|
||||
|
||||
function history(): TitlebarHistory {
|
||||
return { stack: [], index: 0, action: undefined }
|
||||
}
|
||||
|
||||
describe("titlebar history", () => {
|
||||
test("append and trim keeps max bounded", () => {
|
||||
let state = history()
|
||||
state = applyPath(state, "/", 3)
|
||||
state = applyPath(state, "/a", 3)
|
||||
state = applyPath(state, "/b", 3)
|
||||
state = applyPath(state, "/c", 3)
|
||||
|
||||
expect(state.stack).toEqual(["/a", "/b", "/c"])
|
||||
expect(state.stack.length).toBe(3)
|
||||
expect(state.index).toBe(2)
|
||||
})
|
||||
|
||||
test("back and forward indexes stay correct after trimming", () => {
|
||||
let state = history()
|
||||
state = applyPath(state, "/", 3)
|
||||
state = applyPath(state, "/a", 3)
|
||||
state = applyPath(state, "/b", 3)
|
||||
state = applyPath(state, "/c", 3)
|
||||
|
||||
expect(state.stack).toEqual(["/a", "/b", "/c"])
|
||||
expect(state.index).toBe(2)
|
||||
|
||||
const back = backPath(state)
|
||||
expect(back?.to).toBe("/b")
|
||||
expect(back?.state.index).toBe(1)
|
||||
|
||||
const afterBack = applyPath(back!.state, back!.to, 3)
|
||||
expect(afterBack.stack).toEqual(["/a", "/b", "/c"])
|
||||
expect(afterBack.index).toBe(1)
|
||||
|
||||
const forward = forwardPath(afterBack)
|
||||
expect(forward?.to).toBe("/c")
|
||||
expect(forward?.state.index).toBe(2)
|
||||
|
||||
const afterForward = applyPath(forward!.state, forward!.to, 3)
|
||||
expect(afterForward.stack).toEqual(["/a", "/b", "/c"])
|
||||
expect(afterForward.index).toBe(2)
|
||||
})
|
||||
|
||||
test("action-driven navigation does not push duplicate history entries", () => {
|
||||
const state: TitlebarHistory = {
|
||||
stack: ["/", "/a", "/b"],
|
||||
index: 2,
|
||||
action: undefined,
|
||||
}
|
||||
|
||||
const back = backPath(state)
|
||||
expect(back?.to).toBe("/a")
|
||||
|
||||
const next = applyPath(back!.state, back!.to, 10)
|
||||
expect(next.stack).toEqual(["/", "/a", "/b"])
|
||||
expect(next.index).toBe(1)
|
||||
expect(next.action).toBeUndefined()
|
||||
})
|
||||
})
|
||||
57
packages/app/src/components/titlebar-history.ts
Normal file
57
packages/app/src/components/titlebar-history.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
export const MAX_TITLEBAR_HISTORY = 100
|
||||
|
||||
export type TitlebarAction = "back" | "forward" | undefined
|
||||
|
||||
export type TitlebarHistory = {
|
||||
stack: string[]
|
||||
index: number
|
||||
action: TitlebarAction
|
||||
}
|
||||
|
||||
export function applyPath(state: TitlebarHistory, current: string, max = MAX_TITLEBAR_HISTORY): TitlebarHistory {
|
||||
if (!state.stack.length) {
|
||||
const stack = current === "/" ? ["/"] : ["/", current]
|
||||
return { stack, index: stack.length - 1, action: undefined }
|
||||
}
|
||||
|
||||
const active = state.stack[state.index]
|
||||
if (current === active) {
|
||||
if (!state.action) return state
|
||||
return { ...state, action: undefined }
|
||||
}
|
||||
|
||||
if (state.action) return { ...state, action: undefined }
|
||||
|
||||
return pushPath(state, current, max)
|
||||
}
|
||||
|
||||
export function pushPath(state: TitlebarHistory, path: string, max = MAX_TITLEBAR_HISTORY): TitlebarHistory {
|
||||
const stack = state.stack.slice(0, state.index + 1).concat(path)
|
||||
const next = trimHistory(stack, stack.length - 1, max)
|
||||
return { ...state, ...next, action: undefined }
|
||||
}
|
||||
|
||||
export function trimHistory(stack: string[], index: number, max = MAX_TITLEBAR_HISTORY) {
|
||||
if (stack.length <= max) return { stack, index }
|
||||
const cut = stack.length - max
|
||||
return {
|
||||
stack: stack.slice(cut),
|
||||
index: Math.max(0, index - cut),
|
||||
}
|
||||
}
|
||||
|
||||
export function backPath(state: TitlebarHistory) {
|
||||
if (state.index <= 0) return
|
||||
const index = state.index - 1
|
||||
const to = state.stack[index]
|
||||
if (!to) return
|
||||
return { state: { ...state, index, action: "back" as const }, to }
|
||||
}
|
||||
|
||||
export function forwardPath(state: TitlebarHistory) {
|
||||
if (state.index >= state.stack.length - 1) return
|
||||
const index = state.index + 1
|
||||
const to = state.stack[index]
|
||||
if (!to) return
|
||||
return { state: { ...state, index, action: "forward" as const }, to }
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import { useLayout } from "@/context/layout"
|
||||
import { usePlatform } from "@/context/platform"
|
||||
import { useCommand } from "@/context/command"
|
||||
import { useLanguage } from "@/context/language"
|
||||
import { applyPath, backPath, forwardPath } from "./titlebar-history"
|
||||
|
||||
export function Titlebar() {
|
||||
const layout = useLayout()
|
||||
@@ -39,25 +40,9 @@ export function Titlebar() {
|
||||
const current = path()
|
||||
|
||||
untrack(() => {
|
||||
if (!history.stack.length) {
|
||||
const stack = current === "/" ? ["/"] : ["/", current]
|
||||
setHistory({ stack, index: stack.length - 1 })
|
||||
return
|
||||
}
|
||||
|
||||
const active = history.stack[history.index]
|
||||
if (current === active) {
|
||||
if (history.action) setHistory("action", undefined)
|
||||
return
|
||||
}
|
||||
|
||||
if (history.action) {
|
||||
setHistory("action", undefined)
|
||||
return
|
||||
}
|
||||
|
||||
const next = history.stack.slice(0, history.index + 1).concat(current)
|
||||
setHistory({ stack: next, index: next.length - 1 })
|
||||
const next = applyPath(history, current)
|
||||
if (next === history) return
|
||||
setHistory(next)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -65,21 +50,17 @@ export function Titlebar() {
|
||||
const canForward = createMemo(() => history.index < history.stack.length - 1)
|
||||
|
||||
const back = () => {
|
||||
if (!canBack()) return
|
||||
const index = history.index - 1
|
||||
const to = history.stack[index]
|
||||
if (!to) return
|
||||
setHistory({ index, action: "back" })
|
||||
navigate(to)
|
||||
const next = backPath(history)
|
||||
if (!next) return
|
||||
setHistory(next.state)
|
||||
navigate(next.to)
|
||||
}
|
||||
|
||||
const forward = () => {
|
||||
if (!canForward()) return
|
||||
const index = history.index + 1
|
||||
const to = history.stack[index]
|
||||
if (!to) return
|
||||
setHistory({ index, action: "forward" })
|
||||
navigate(to)
|
||||
const next = forwardPath(history)
|
||||
if (!next) return
|
||||
setHistory(next.state)
|
||||
navigate(next.to)
|
||||
}
|
||||
|
||||
command.register(() => [
|
||||
|
||||
Reference in New Issue
Block a user