Skip to content

Commit

Permalink
chore: refactor to not use localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwisecodes committed Nov 19, 2024
1 parent b2f1142 commit 23f3276
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ interface SidebarItemProps {
}

export function SidebarItem({ chat }: SidebarItemProps) {
const { setDialogWindow } = useDialog();
const { setDialogWindow, setDialogProps } = useDialog();
const [isHovered, setIsHovered] = useState(false);
function deleteChatById() {
localStorage.setItem("chatIdToDelete", chat.id);
setDialogProps({ chatIdToDelete: chat.id });
setDialogWindow("clear-single-chat");
}
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { DialogTypes } from "../Chat/Chat/types";
interface DialogContextType {
dialogWindow: DialogTypes;
setDialogWindow: React.Dispatch<React.SetStateAction<DialogTypes>>;
dialogProps?: Record<string, unknown>;
setDialogProps: React.Dispatch<React.SetStateAction<Record<string, unknown>>>;
}

const DialogContext = createContext<DialogContextType | undefined>(undefined);
Expand All @@ -15,10 +17,11 @@ export const DialogProvider: React.FC<React.PropsWithChildren> = ({
children,
}) => {
const [dialogWindow, setDialogWindow] = useState<DialogTypes>("");
const [dialogProps, setDialogProps] = useState<Record<string, unknown>>({});

const value = useMemo(
() => ({ dialogWindow, setDialogWindow }),
[dialogWindow],
() => ({ dialogWindow, setDialogWindow, dialogProps, setDialogProps }),
[dialogWindow, dialogProps],
);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useState, useTransition } from "react";
import { useTransition } from "react";
import { toast } from "react-hot-toast";

import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"use client";

import { useTransition } from "react";
import { toast } from "react-hot-toast";

import {
OakFlex,
OakP,
OakSmallPrimaryButton,
OakSmallSecondaryButton,
} from "@oaknational/oak-components";
import { showReportDialog } from "@sentry/nextjs/build/types/server";
import * as Sentry from "@sentry/nextjs";

import { useDialog } from "@/components/AppComponents/DialogContext";
import LoadingWheel from "@/components/LoadingWheel";
import { trpc } from "@/utils/trpc";

Expand All @@ -19,20 +19,20 @@ const ClearSingleChatFromChatHistory = ({
}: {
closeDialog: () => void;
}) => {
const { dialogProps, setDialogProps } = useDialog();
const { mutate, isLoading } =
trpc.chat.appSessions.deleteChatById.useMutation({
onSuccess() {
console.log("Chat history cleared");
closeDialog();
localStorage.removeItem("chatIdToDelete");
setDialogProps({});
},
onError() {
console.error("Failed to clear chat history");
Sentry.captureMessage("Error deleting chat");
},
});

function deleteChatById() {
const id = localStorage.getItem("chatIdToDelete");
const id = dialogProps?.chatIdToDelete;
if (typeof id !== "string") {
return;
}
Expand Down
7 changes: 5 additions & 2 deletions apps/nextjs/src/components/DialogControl/DialogContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ const DialogContents = ({
submit?: () => void;
isShared?: boolean | undefined;
}) => {
const { dialogWindow, setDialogWindow } = useDialog();
const closeDialog = () => setDialogWindow("");
const { dialogWindow, setDialogWindow, setDialogProps } = useDialog();
const closeDialog = () => {
setDialogWindow("");
setDialogProps({});
};

return (
<Dialog.Portal>
Expand Down

0 comments on commit 23f3276

Please sign in to comment.