-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable the ability to disable the chat history UI (#2501)
* Enable the ability to disable the chat history UI * forgot files
- Loading branch information
1 parent
e71392d
commit 0524aad
Showing
13 changed files
with
437 additions
and
271 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useEffect, useState } from "react"; | ||
import { FullScreenLoader } from "@/components/Preloader"; | ||
import System from "@/models/system"; | ||
import paths from "@/utils/paths"; | ||
|
||
/** | ||
* Protects the view from system set ups who cannot view chat history. | ||
* If the user cannot view chat history, they are redirected to the home page. | ||
* @param {React.ReactNode} children | ||
*/ | ||
export function CanViewChatHistory({ children }) { | ||
const { loading, viewable } = useCanViewChatHistory(); | ||
if (loading) return <FullScreenLoader />; | ||
if (!viewable) { | ||
window.location.href = paths.home(); | ||
return <FullScreenLoader />; | ||
} | ||
|
||
return <>{children}</>; | ||
} | ||
|
||
/** | ||
* Provides the `viewable` state to the children. | ||
* @returns {React.ReactNode} | ||
*/ | ||
export function CanViewChatHistoryProvider({ children }) { | ||
const { loading, viewable } = useCanViewChatHistory(); | ||
if (loading) return null; | ||
return <>{children({ viewable })}</>; | ||
} | ||
|
||
/** | ||
* Hook that fetches the can view chat history state from local storage or the system settings. | ||
* @returns {Promise<{viewable: boolean, error: string | null}>} | ||
*/ | ||
export function useCanViewChatHistory() { | ||
const [loading, setLoading] = useState(true); | ||
const [viewable, setViewable] = useState(false); | ||
|
||
useEffect(() => { | ||
async function fetchViewable() { | ||
const { viewable } = await System.fetchCanViewChatHistory(); | ||
setViewable(viewable); | ||
setLoading(false); | ||
} | ||
fetchViewable(); | ||
}, []); | ||
|
||
return { loading, viewable }; | ||
} |
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
Oops, something went wrong.