Skip to content

Commit

Permalink
feat: save group selected
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Jan 3, 2025
1 parent 1e2c271 commit 2f02123
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/components/GroupSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export default function GroupSwitch({
}
}, [])

useEffect(() => {
const savedGroup = sessionStorage.getItem("selectedGroup")
if (savedGroup && tabs.includes(savedGroup)) {
setCurrentTab(savedGroup)
}
}, [tabs, setCurrentTab])

useEffect(() => {
const currentTagRef = tagRefs.current[tabs.indexOf(currentTab)]

Expand Down
8 changes: 7 additions & 1 deletion src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ function Header() {
return (
<div className="mx-auto w-full max-w-5xl">
<section className="flex items-center justify-between header-top">
<section onClick={() => navigate("/")} className="cursor-pointer flex items-center sm:text-base text-sm font-medium">
<section
onClick={() => {
sessionStorage.removeItem("selectedGroup")
navigate("/")
}}
className="cursor-pointer flex items-center sm:text-base text-sm font-medium"
>
<div className="mr-1 flex flex-row items-center justify-start header-logo">
<img
width={40}
Expand Down
29 changes: 25 additions & 4 deletions src/pages/Server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { NezhaWebsocketResponse } from "@/types/nezha-api"
import { ServerGroup } from "@/types/nezha-api"
import { ArrowDownIcon, ArrowUpIcon, ArrowsUpDownIcon, ChartBarSquareIcon, MapIcon, ViewColumnsIcon } from "@heroicons/react/20/solid"
import { useQuery } from "@tanstack/react-query"
import { useEffect, useState } from "react"
import { useEffect, useRef, useState } from "react"
import { useTranslation } from "react-i18next"

export default function Servers() {
Expand All @@ -32,13 +32,27 @@ export default function Servers() {
const [showServices, setShowServices] = useState<string>("0")
const [showMap, setShowMap] = useState<string>("0")
const [inline, setInline] = useState<string>("0")
const containerRef = useRef<HTMLDivElement>(null)
const [settingsOpen, setSettingsOpen] = useState<boolean>(false)
const [currentGroup, setCurrentGroup] = useState<string>("All")

const customBackgroundImage =
// @ts-expect-error CustomBackgroundImage is a global variable
(window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined

const restoreScrollPosition = () => {
const savedPosition = sessionStorage.getItem("scrollPosition")
if (savedPosition && containerRef.current) {
containerRef.current.scrollTop = Number(savedPosition)
}
}

const handleTagChange = (newGroup: string) => {
setCurrentGroup(newGroup)
sessionStorage.setItem("selectedGroup", newGroup)
sessionStorage.setItem("scrollPosition", String(containerRef.current?.scrollTop || 0))
}

useEffect(() => {
const showServicesState = localStorage.getItem("showServices")
if (showServicesState !== null) {
Expand All @@ -53,6 +67,13 @@ export default function Servers() {
}
}, [])

useEffect(() => {
const savedGroup = sessionStorage.getItem("selectedGroup") || "All"
setCurrentGroup(savedGroup)

restoreScrollPosition()
}, [])

const groupTabs = ["All", ...(groupData?.data?.map((item: ServerGroup) => item.group.name) || [])]

if (!connected && !lastMessage) {
Expand Down Expand Up @@ -233,7 +254,7 @@ export default function Servers() {
>
<ViewColumnsIcon className="size-[13px]" />
</button>
<GroupSwitch tabs={groupTabs} currentTab={currentGroup} setCurrentTab={setCurrentGroup} />
<GroupSwitch tabs={groupTabs} currentTab={currentGroup} setCurrentTab={handleTagChange} />
</section>
<Popover onOpenChange={setSettingsOpen}>
<PopoverTrigger asChild>
Expand Down Expand Up @@ -306,14 +327,14 @@ export default function Servers() {
{showMap === "1" && <GlobalMap now={nezhaWsData.now} serverList={nezhaWsData?.servers || []} />}
{showServices === "1" && <ServiceTracker serverList={filteredServers} />}
{inline === "1" && (
<section className="flex flex-col gap-2 overflow-x-scroll scrollbar-hidden mt-6 server-inline-list">
<section ref={containerRef} className="flex flex-col gap-2 overflow-x-scroll scrollbar-hidden mt-6 server-inline-list">
{filteredServers.map((serverInfo) => (
<ServerCardInline now={nezhaWsData.now} key={serverInfo.id} serverInfo={serverInfo} />
))}
</section>
)}
{inline === "0" && (
<section className="grid grid-cols-1 gap-2 md:grid-cols-2 mt-6 server-card-list">
<section ref={containerRef} className="grid grid-cols-1 gap-2 md:grid-cols-2 mt-6 server-card-list">
{filteredServers.map((serverInfo) => (
<ServerCard now={nezhaWsData.now} key={serverInfo.id} serverInfo={serverInfo} />
))}
Expand Down

0 comments on commit 2f02123

Please sign in to comment.