Skip to content

Commit

Permalink
feat: layout by pathname (close alist-org/alist#4651)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 26, 2023
1 parent a127ddb commit 8391e06
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Progress, ProgressIndicator } from "@hope-ui/solid"
import { Route, Routes, useIsRouting } from "@solidjs/router"
import {
Component,
createEffect,
createSignal,
lazy,
Match,
Expand Down Expand Up @@ -30,7 +31,7 @@ const App: Component = () => {
globalStyles()
const [, { add }] = useI18n()
const isRouting = useIsRouting()
const { to } = useRouter()
const { to, pathname } = useRouter()
const onTo = (path: string) => {
to(path)
}
Expand All @@ -39,6 +40,10 @@ const App: Component = () => {
bus.off("to", onTo)
})

createEffect(() => {
bus.emit("pathname", pathname())
})

const [err, setErr] = createSignal<string>()
const [loading, data] = useLoading(() =>
Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const getLinkByDirAndObj = (
let host = api
let prefix = type === "direct" ? "/d" : "/p"
if (type === "preview") {
if (!api.startsWith(location.origin)) host = location.origin
prefix = ""
if (!api.startsWith(location.origin)) host = location.origin
}
let ans = `${host}${prefix}${path}`
if (type !== "preview" && obj.sign) {
Expand Down
28 changes: 25 additions & 3 deletions src/store/obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cookieStorage, createStorageSignal } from "@solid-primitives/storage"
import { createSignal } from "solid-js"
import { createStore, produce } from "solid-js/store"
import { Obj, StoreObj } from "~/types"
import { log } from "~/utils"
import { bus, log } from "~/utils"
import { keyPressed } from "./key-event"

export enum State {
Expand Down Expand Up @@ -174,7 +174,29 @@ export const isIndeterminate = () => {
}

export type LayoutType = "list" | "grid" | "image"
const [layout, setLayout] = createStorageSignal<LayoutType>("layout", "list")
const [pathname, setPathname] = createSignal<string>(location.pathname)
const layoutRecord: Record<string, LayoutType> = (() => {
try {
return JSON.parse(localStorage.getItem("layoutRecord") || "{}")
} catch (e) {
return {}
}
})()

bus.on("pathname", (p) => setPathname(p))
const [_layout, _setLayout] = createSignal<LayoutType>(
layoutRecord[pathname()] || "list"
)
export const layout = () => {
const layout = layoutRecord[pathname()]
_setLayout(layout || "list")
return _layout()
}
export const setLayout = (layout: LayoutType) => {
layoutRecord[pathname()] = layout
localStorage.setItem("layoutRecord", JSON.stringify(layoutRecord))
_setLayout(layout)
}

const [_checkboxOpen, setCheckboxOpen] = createStorageSignal<string>(
"checkbox-open",
Expand All @@ -186,7 +208,7 @@ export const toggleCheckbox = () => {
setCheckboxOpen(checkboxOpen() ? "false" : "true")
}

export { objStore, layout, setLayout }
export { objStore }
// browser password
const [_password, _setPassword] = createSignal<string>(
cookieStorage.getItem("browser-password") || ""
Expand Down
1 change: 1 addition & 0 deletions src/utils/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Events = {
to: string
gallery: string
tool: string
pathname: string
}

export const bus = mitt<Events>()

0 comments on commit 8391e06

Please sign in to comment.