From 321deacfb7efac53070da4c5c785f87ebcf45f7a Mon Sep 17 00:00:00 2001 From: Paul Butler Date: Tue, 5 Nov 2024 13:49:40 -0500 Subject: [PATCH] Fix debugger console.log (#327) We want the debugger to be discoverable, but we only want to show it once per session. We used to do this by displaying it in the `useYDoc` hook, but that may be run more than once. Instead, this moves the code that prints it into the `useEffect` of the `YDocProvider`. --- debugger/src/app/debugger.tsx | 4 +--- debugger/src/app/page.tsx | 6 ++++- js-pkg/react/src/main.tsx | 42 ++++++++++++++++++++++------------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/debugger/src/app/debugger.tsx b/debugger/src/app/debugger.tsx index 7c518815..b6455300 100644 --- a/debugger/src/app/debugger.tsx +++ b/debugger/src/app/debugger.tsx @@ -9,9 +9,7 @@ import { Awareness } from 'y-protocols/awareness' import { DebuggableAwareness } from '@/lib/debuggable/yawareness' export function Debugger() { - const doc: Y.Doc = useYDoc({ - hideDebuggerLink: true, - }) + const doc: Y.Doc = useYDoc() const provider = useYjsProvider() const url = provider.ws?.url const awareness = useAwareness() diff --git a/debugger/src/app/page.tsx b/debugger/src/app/page.tsx index 5e86695d..086a95b5 100644 --- a/debugger/src/app/page.tsx +++ b/debugger/src/app/page.tsx @@ -32,7 +32,11 @@ export default function Home() { function DebuggerWrapper({ clientToken }: { clientToken: ClientToken }) { return ( - clientToken}> + clientToken} + showDebuggerLink={false} + > ) diff --git a/js-pkg/react/src/main.tsx b/js-pkg/react/src/main.tsx index ccbe45a0..3c0bce13 100644 --- a/js-pkg/react/src/main.tsx +++ b/js-pkg/react/src/main.tsx @@ -29,6 +29,9 @@ type YjsContextType = { const YjsContext = createContext(null) type YDocOptions = { + /** + * @deprecated since 0.6.0 and not used. Pass `showDebuggerLink={false}` to `YDocProvider` as a prop instead. + */ hideDebuggerLink?: boolean } @@ -40,19 +43,11 @@ type YDocOptions = { export function useYDoc(options?: YDocOptions): Y.Doc { const yjsCtx = useContext(YjsContext) - useEffect(() => { - if (!options?.hideDebuggerLink && yjsCtx) { - const url = debuggerUrl(yjsCtx.provider.clientToken) - console.log( - `%cOpen this in Y-Sweet Debugger ⮕ ${url}`, - 'font-size: 1.5em; display: block; padding: 10px;', - ) - console.log( - '%cTo hide the debugger link, pass { hideDebuggerLink: true } to useYDoc', - 'font-style: italic;', - ) - } - }, [options?.hideDebuggerLink, yjsCtx]) + if (options?.hideDebuggerLink) { + console.warn( + 'The `hideDebuggerLink` option is deprecated and no longer used. Pass `showDebuggerLink={false}` to `YDocProvider` as a prop instead.', + ) + } if (!yjsCtx) { throw new Error('Yjs hooks must be used within a YDocProvider') @@ -185,6 +180,9 @@ export type YDocProviderProps = { /** If set to a string, the URL query parameter with this name * will be set to the doc id provided. */ setQueryParam?: string + + /** Whether to hide the debugger link. Defaults to true. */ + showDebuggerLink?: boolean } /** @@ -208,6 +206,18 @@ export function YDocProvider(props: YDocProviderProps) { disableBc: true, }) + if (props.showDebuggerLink ?? true) { + const url = debuggerUrl(provider.clientToken) + console.log( + `%cOpen this in Y-Sweet Debugger ⮕ ${url}`, + 'font-size: 1.5em; display: block; padding: 10px;', + ) + console.log( + '%cTo hide the debugger link, pass showDebuggerLink={false} to YDocProvider', + 'font-style: italic;', + ) + } + if (canceled) { provider.destroy() return @@ -277,7 +287,7 @@ export type ObjectOptions = { * @returns */ export function useMap(name?: string, objectOptions?: ObjectOptions): Y.Map { - const doc = useYDoc({ hideDebuggerLink: true }) + const doc = useYDoc() const map = useMemo(() => doc.getMap(name), [doc, name]) useObserve(map, objectOptions?.observe || 'deep') @@ -301,7 +311,7 @@ export function useMap(name?: string, objectOptions?: ObjectOptions): Y.Map(name?: string, objectOptions?: ObjectOptions): Y.Array { - const doc = useYDoc({ hideDebuggerLink: true }) + const doc = useYDoc() const array = useMemo(() => doc.getArray(name), [doc, name]) useObserve(array, objectOptions?.observe || 'deep') @@ -324,7 +334,7 @@ export function useArray(name?: string, objectOptions?: ObjectOptions): Y.Arr * @returns */ export function useText(name?: string, observerKind?: ObjectOptions): Y.Text { - const doc = useYDoc({ hideDebuggerLink: true }) + const doc = useYDoc() const text = useMemo(() => doc.getText(name), [doc, name]) useObserve(text, observerKind?.observe || 'deep')