-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh change request cache when click refresh button (#2489)
- Loading branch information
Showing
8 changed files
with
192 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'gitbook': minor | ||
--- | ||
|
||
Revalidate change request cached content when pressing refresh button |
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
70 changes: 70 additions & 0 deletions
70
packages/gitbook/src/components/AdminToolbar/RefreshChangeRequestButton.tsx
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,70 @@ | ||
'use client'; | ||
import { Icon } from '@gitbook/icons'; | ||
import React from 'react'; | ||
|
||
import { useCheckForContentUpdate } from '@/components/AutoRefreshContent'; | ||
import { tcls } from '@/lib/tailwind'; | ||
|
||
import { ToolbarButton } from './Toolbar'; | ||
|
||
// We don't show the button if the content has been updated 30s ago or less. | ||
const minInterval = 1000 * 30; // 5 minutes | ||
|
||
/** | ||
* Button to refresh the page if the content has been updated. | ||
*/ | ||
export function RefreshChangeRequestButton(props: { | ||
spaceId: string; | ||
changeRequestId: string; | ||
revisionId: string; | ||
updatedAt: number; | ||
}) { | ||
const { updatedAt } = props; | ||
|
||
const [visible, setVisible] = React.useState(false); | ||
const [loading, setLoading] = React.useState(false); | ||
const checkForUpdates = useCheckForContentUpdate(props); | ||
|
||
const refresh = React.useCallback(async () => { | ||
setLoading(true); | ||
try { | ||
await checkForUpdates(); | ||
} finally { | ||
setLoading(false); | ||
setVisible(false); | ||
} | ||
}, [checkForUpdates]); | ||
|
||
// Show the button if the content has been updated more than 30s ago. | ||
React.useEffect(() => { | ||
if (updatedAt < Date.now() - minInterval) { | ||
setVisible(true); | ||
} | ||
}, [updatedAt]); | ||
|
||
// 30sec after being hidden, we show the button again | ||
React.useEffect(() => { | ||
if (!visible) { | ||
const timeout = setTimeout(() => { | ||
setVisible(true); | ||
}, minInterval); | ||
return () => clearTimeout(timeout); | ||
} | ||
}, [visible]); | ||
|
||
if (!visible) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ToolbarButton | ||
title="Refresh" | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
refresh(); | ||
}} | ||
> | ||
<Icon icon="rotate" className={tcls('size-4', loading ? 'animate-spin' : null)} /> | ||
</ToolbarButton> | ||
); | ||
} |
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,66 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
|
||
import { tcls } from '@/lib/tailwind'; | ||
|
||
export function Toolbar(props: { children: React.ReactNode }) { | ||
const { children } = props; | ||
|
||
return ( | ||
<div | ||
className={tcls( | ||
'flex', | ||
'flex-row', | ||
'items-center', | ||
'gap-4', | ||
'text-sm', | ||
'px-4', | ||
'py-1', | ||
'rounded-full', | ||
'truncate', | ||
'text-light', | ||
'dark:text-light', | ||
)} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
export function ToolbarBody(props: { children: React.ReactNode }) { | ||
return <div className="flex flex-col gap-1">{props.children}</div>; | ||
} | ||
|
||
export function ToolbarButtonGroups(props: { children: React.ReactNode }) { | ||
return <div className="flex flex-row gap-2">{props.children}</div>; | ||
} | ||
|
||
export function ToolbarButton(props: React.HTMLProps<HTMLAnchorElement>) { | ||
const { children, ...rest } = props; | ||
return ( | ||
<a | ||
{...rest} | ||
className={tcls( | ||
'flex', | ||
'flex-col', | ||
'items-center', | ||
'justify-center', | ||
'size-11', | ||
'gap-1', | ||
'text-sm', | ||
'rounded-full', | ||
'hover:bg-dark-1', | ||
'hover:text-white', | ||
'truncate', | ||
'text-light', | ||
'dark:text-light', | ||
'dark:hover:bg-dark-2', | ||
'hover:shadow-lg', | ||
'cursor-pointer', | ||
)} | ||
> | ||
{children} | ||
</a> | ||
); | ||
} |
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 @@ | ||
export * from './useCheckForContentUpdate'; |
15 changes: 15 additions & 0 deletions
15
packages/gitbook/src/components/AutoRefreshContent/server-actions.ts
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,15 @@ | ||
'use server'; | ||
|
||
import { getChangeRequest } from '@/lib/api'; | ||
|
||
/** | ||
* Return true if a change-request has been updated. | ||
*/ | ||
export async function hasContentBeenUpdated(props: { | ||
spaceId: string; | ||
changeRequestId: string; | ||
revisionId: string; | ||
}) { | ||
const changeRequest = await getChangeRequest.revalidate(props.spaceId, props.changeRequestId); | ||
return changeRequest.revision !== props.revisionId; | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/gitbook/src/components/AutoRefreshContent/useCheckForContentUpdate.ts
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,24 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import { hasContentBeenUpdated } from './server-actions'; | ||
|
||
/** | ||
* Return a callback to check if a change request has been updated and to refresh the page if it has. | ||
*/ | ||
export function useCheckForContentUpdate(props: { | ||
spaceId: string; | ||
changeRequestId: string; | ||
revisionId: string; | ||
}) { | ||
const { spaceId, changeRequestId, revisionId } = props; | ||
|
||
return React.useCallback(async () => { | ||
const updated = await hasContentBeenUpdated({ spaceId, changeRequestId, revisionId }); | ||
|
||
if (updated) { | ||
window.location.reload(); | ||
} | ||
}, [spaceId, changeRequestId, revisionId]); | ||
} |
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