Skip to content

Commit

Permalink
fix: toast 토글을 개별적으로 설정하여 각 토스트가 3초가 지나면 사라지게 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hyonun321 committed Nov 28, 2024
1 parent 4797dd8 commit df786a3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
19 changes: 11 additions & 8 deletions client/src/components/Toast/ToastContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AnimatePresence } from "framer-motion";
import { useToastStore } from "@stores/useToastStore";
import { Toast } from "./Toast";
import { ToastContainerStyle } from "./ToastContainer.style";
Expand All @@ -7,14 +8,16 @@ export const ToastContainer = () => {

return (
<div className={ToastContainerStyle}>
{toasts.map((toast) => (
<Toast
key={toast.id}
message={toast.message}
duration={toast.duration}
onClose={() => removeToast(toast.id)}
/>
))}
<AnimatePresence>
{toasts.map((toast) => (
<Toast
key={toast.id}
message={toast.message}
duration={toast.duration}
onClose={() => removeToast(toast.id)}
/>
))}
</AnimatePresence>
</div>
);
};
5 changes: 1 addition & 4 deletions client/src/components/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useModal } from "@components/modal/useModal";
import { MAX_VISIBLE_PAGE } from "@src/constants/page";
import { AuthButton } from "@src/features/auth/AuthButton";
import { useSocketStore } from "@src/stores/useSocketStore";
import { useToastStore } from "@src/stores/useToastStore";
import { Page } from "@src/types/page";
import { useIsSidebarOpen, useSidebarActions } from "@stores/useSidebarStore";
import { animation, contentVariants, sidebarVariants } from "./Sidebar.animation";
Expand Down Expand Up @@ -43,7 +42,6 @@ export const Sidebar = ({
const { isOpen, openModal, closeModal } = useModal();
const { sendPageDeleteOperation, clientId } = useSocketStore();

const { addToast } = useToastStore();
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [pageToDelete, setPageToDelete] = useState<Page | null>(null);

Expand All @@ -69,11 +67,10 @@ export const Sidebar = ({
return;
}

addToast(`페이지(${pageToDelete!.title})가 삭제되었습니다.`);

sendPageDeleteOperation({
type: "pageDelete",
workspaceId: "default",
pageTitle: pageToDelete!.title,
pageId,
clientId,
});
Expand Down
3 changes: 3 additions & 0 deletions client/src/features/workSpace/hooks/usePagesManage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Page as CRDTPage } from "@noctaCrdt/Page";
import { WorkSpace } from "@noctaCrdt/WorkSpace";
import { useEffect, useState, useRef, useCallback } from "react";
import { useSocketStore } from "@src/stores/useSocketStore";
import { useToastStore } from "@src/stores/useToastStore";
import { Page } from "@src/types/page";

const PAGE_OFFSET = 60;
Expand All @@ -17,6 +18,7 @@ export const usePagesManage = (workspace: WorkSpace | null, clientId: number | n
const { subscribeToPageOperations, sendPageCreateOperation, sendPageUpdateOperation } =
useSocketStore();
const subscriptionRef = useRef(false);
const { addToast } = useToastStore();
useEffect(() => {
if (!workspace) return;
if (subscriptionRef.current) return;
Expand Down Expand Up @@ -52,6 +54,7 @@ export const usePagesManage = (workspace: WorkSpace | null, clientId: number | n
addPage(newPage);
},
onRemotePageDelete: (operation) => {
addToast(`${operation.clientId}번 유저가 페이지(${operation.pageTitle})를 삭제하였습니다.`);
workspace.remotePageDelete?.({
pageId: operation.pageId,
workspaceId: operation.workspaceId,
Expand Down
15 changes: 12 additions & 3 deletions client/src/stores/useToastStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ interface ToastStore {

export const useToastStore = create<ToastStore>((set) => ({
toasts: [],
addToast: (message, duration = 3000) =>
addToast: (message, duration = 3000) => {
const id = Date.now();
set((state) => ({
toasts: [...state.toasts, { id: Date.now(), message, duration }],
})),
toasts: [...state.toasts, { id, message, duration }],
}));
// duration 후에 자동으로 해당 toast 제거
setTimeout(() => {
set((state) => ({
toasts: state.toasts.filter((toast) => toast.id !== id),
}));
}, duration);
},

removeToast: (id) =>
set((state) => ({
toasts: state.toasts.filter((toast) => toast.id !== id),
Expand Down

0 comments on commit df786a3

Please sign in to comment.