Skip to content

Commit

Permalink
Zustand logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
lourou committed Oct 25, 2024
1 parent ae5cf8d commit d6e5753
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ import { create } from "zustand";

import { RolledUpReactions } from "../MessageReactions.types";

const initialMessageReactionsState: RolledUpReactions = {
emojis: [],
totalReactions: 0,
userReacted: false,
details: {},
};

export interface IMessageReactionsStore {
rolledUpReactions: RolledUpReactions;
}
setRolledUpReactions: (reactions: RolledUpReactions) => void;

export const initialMessageReactionsState: IMessageReactionsStore = {
rolledUpReactions: {
emojis: [],
totalReactions: 0,
userReacted: false,
details: {},
},
};
// TODO: update state when new reactions come up and drawer is open
// updateReactions: (updates: Partial<RolledUpReactions>) => void;
}

export const useMessageReactionsStore = create<IMessageReactionsStore>(
(set, get) => ({
...initialMessageReactionsState,
(set) => ({
rolledUpReactions: initialMessageReactionsState,
setRolledUpReactions: (reactions) => set({ rolledUpReactions: reactions }),
})
);

export const resetMessageReactionsStore = () => {
useMessageReactionsStore.setState(initialMessageReactionsState);
useMessageReactionsStore
.getState()
.setRolledUpReactions(initialMessageReactionsState);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createBottomSheetModalRef } from "@design-system/BottomSheet/BottomShee

import { RolledUpReactions } from "../MessageReactions.types";
import {
initialMessageReactionsState,
resetMessageReactionsStore,
useMessageReactionsStore,
} from "./MessageReactions.store";

Expand All @@ -11,8 +11,16 @@ export const bottomSheetModalRef = createBottomSheetModalRef();
export function openMessageReactionsDrawer(
rolledUpReactions: RolledUpReactions
) {
bottomSheetModalRef.current?.present();
useMessageReactionsStore.setState({ rolledUpReactions });
try {
if (!bottomSheetModalRef.current) {
throw new Error("Modal reference not initialized");
}
useMessageReactionsStore.getState().setRolledUpReactions(rolledUpReactions);
bottomSheetModalRef.current.present();
} catch (error) {
console.error("Failed to open message reactions drawer:", error);
resetMessageReactionsDrawer();
}
}

export function closeMessageReactionsDrawer(arg?: { resetStore?: boolean }) {
Expand All @@ -24,7 +32,7 @@ export function closeMessageReactionsDrawer(arg?: { resetStore?: boolean }) {
}

export function resetMessageReactionsDrawer() {
useMessageReactionsStore.setState(initialMessageReactionsState);
resetMessageReactionsStore();
}

export function useMessageReactionsRolledUpReactions() {
Expand Down

0 comments on commit d6e5753

Please sign in to comment.