-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forum): Add local storage for category accordion
- Loading branch information
1 parent
b833df3
commit d421597
Showing
12 changed files
with
124 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { | ||
createContext, | ||
useContext, | ||
type Dispatch, | ||
type SetStateAction | ||
} from "react"; | ||
|
||
interface Args { | ||
open: boolean; | ||
setOpen: Dispatch<SetStateAction<boolean>>; | ||
} | ||
|
||
export const WrapperCategoryForumContext = createContext<Args>({ | ||
open: true, | ||
setOpen: () => {} | ||
}); | ||
|
||
export const useWrapperCategoryForum = () => | ||
useContext(WrapperCategoryForumContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 30 additions & 14 deletions
44
frontend/themes/1/forum/views/forum/forums/category/chevron-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,39 @@ | ||
"use client"; | ||
|
||
import { ChevronDown } from "lucide-react"; | ||
import * as Accordion from "@radix-ui/react-accordion"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { useWrapperCategoryForum } from "@/hooks/forums/forum/use-wrapper-category-forum"; | ||
import { LOCAL_STORAGE_KEY } from "./wrapper"; | ||
|
||
interface Props { | ||
id: number; | ||
} | ||
|
||
export const ChevronCategoryForumButton = ({ id }: Props) => { | ||
const { open, setOpen } = useWrapperCategoryForum(); | ||
|
||
export const ChevronCategoryForumButton = () => { | ||
return ( | ||
<Accordion.Header> | ||
<Accordion.Trigger asChild> | ||
<Button | ||
className="text-muted-foreground hover:text-foreground flex-shrink-0 [&[data-state=open]>svg]:rotate-180 [&>svg]:transition-transform" | ||
variant="ghost" | ||
size="icon" | ||
tooltip="" | ||
> | ||
<ChevronDown /> | ||
</Button> | ||
</Accordion.Trigger> | ||
</Accordion.Header> | ||
<Button | ||
className="text-muted-foreground hover:text-foreground flex-shrink-0 [&[data-state=open]>svg]:rotate-180 [&>svg]:transition-transform" | ||
variant="ghost" | ||
size="icon" | ||
data-state={open ? "open" : "closed"} | ||
tooltip="" | ||
onClick={() => { | ||
setOpen(prev => !prev); | ||
const currentId = id.toString(); | ||
|
||
const prevIds = | ||
localStorage.getItem(LOCAL_STORAGE_KEY)?.split(",") || []; | ||
const valueToSet = prevIds.includes(currentId) | ||
? prevIds.filter(i => i !== currentId) | ||
: [...prevIds, currentId]; | ||
|
||
localStorage.setItem(LOCAL_STORAGE_KEY, valueToSet.join(",")); | ||
}} | ||
> | ||
<ChevronDown /> | ||
</Button> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
frontend/themes/1/forum/views/forum/forums/category/children-wrapper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"use client"; | ||
|
||
import { AnimatePresence, motion } from "framer-motion"; | ||
import type { ReactNode } from "react"; | ||
|
||
import { useWrapperCategoryForum } from "@/hooks/forums/forum/use-wrapper-category-forum"; | ||
|
||
interface Props { | ||
children: ReactNode; | ||
} | ||
|
||
export const ChildrenWrapperCategoryForum = ({ children }: Props) => { | ||
const { open } = useWrapperCategoryForum(); | ||
|
||
return ( | ||
<AnimatePresence initial={false}> | ||
{open && ( | ||
<motion.div | ||
initial="collapsed" | ||
animate="open" | ||
exit="collapsed" | ||
className="overflow-hidden" | ||
variants={{ | ||
open: { opacity: 1, height: "auto" }, | ||
collapsed: { opacity: 0, height: 0 } | ||
}} | ||
> | ||
{children} | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
); | ||
}; |
23 changes: 20 additions & 3 deletions
23
frontend/themes/1/forum/views/forum/forums/category/wrapper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
"use client"; | ||
|
||
import * as Accordion from "@radix-ui/react-accordion"; | ||
import type { ReactNode } from "react"; | ||
import { useState, type ReactNode, useEffect } from "react"; | ||
|
||
import { WrapperCategoryForumContext } from "@/hooks/forums/forum/use-wrapper-category-forum"; | ||
|
||
interface Props { | ||
children: ReactNode; | ||
id: number; | ||
} | ||
|
||
export const LOCAL_STORAGE_KEY = "forum:category-accordion"; | ||
|
||
export const WrapperCategoryForum = ({ children, id }: Props) => { | ||
return <Accordion.Item value={`${id}`}>{children}</Accordion.Item>; | ||
const [open, setOpen] = useState(true); | ||
|
||
useEffect(() => { | ||
const ids = localStorage.getItem(LOCAL_STORAGE_KEY)?.split(","); | ||
|
||
if (ids?.includes(id.toString())) { | ||
setOpen(false); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<WrapperCategoryForumContext.Provider value={{ open, setOpen }}> | ||
{children} | ||
</WrapperCategoryForumContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.