Repository navigation
feat(pages): version history with restore, trash with restore/purge, and Space export/import #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ayush7614
wants to merge
1
commit into
CopilotKit:main
Choose a base branch
from
Ayush7614:feat/page-history-trash-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,164 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { History, RotateCcw } from 'lucide-react'; | ||
| import type { Page, PageVersion } from '../server/pages'; | ||
| import { api, ApiError } from './api'; | ||
|
|
||
| export function PageHistory({ | ||
| page, | ||
| onRestored, | ||
| }: { | ||
| page: Page; | ||
| onRestored: (page: Page) => void; | ||
| }) { | ||
| const [open, setOpen] = useState(false); | ||
| const [versions, setVersions] = useState<PageVersion[] | null>(null); | ||
| const [selectedId, setSelectedId] = useState<string | null>(null); | ||
| const [notice, setNotice] = useState(''); | ||
| const [loading, setLoading] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| if (!open || versions !== null) return; | ||
| let active = true; | ||
| setLoading(true); | ||
| api<PageVersion[]>(`/spaces/${page.spaceId}/pages/${page.id}/versions`) | ||
| .then((rows) => { | ||
| if (active) { | ||
| setVersions(rows); | ||
| setSelectedId(rows[0]?.id ?? null); | ||
| setNotice(''); | ||
| } | ||
| }) | ||
| .catch((error) => { | ||
| if (active) | ||
| setNotice( | ||
| error instanceof Error | ||
| ? error.message | ||
| : 'Could not load page history.', | ||
| ); | ||
| }) | ||
| .finally(() => { | ||
| if (active) setLoading(false); | ||
| }); | ||
| return () => { | ||
| active = false; | ||
| }; | ||
| }, [open, versions, page.spaceId, page.id]); | ||
|
|
||
| // A new revision means the cached list is stale; refetch on next open. | ||
| useEffect(() => { | ||
| setVersions(null); | ||
| setSelectedId(null); | ||
| }, [page.revision]); | ||
|
|
||
| const selected = versions?.find((item) => item.id === selectedId) ?? null; | ||
|
|
||
| const restore = async () => { | ||
| if (!selected) return; | ||
| if ( | ||
| !window.confirm( | ||
| `Restore "${selected.title}" from revision ${selected.revision}? Your current draft is kept as a new history entry.`, | ||
| ) | ||
| ) | ||
| return; | ||
| try { | ||
| const next = await api<Page>( | ||
| `/spaces/${page.spaceId}/pages/${page.id}/restore`, | ||
| 'POST', | ||
| { versionId: selected.id, expectedRevision: page.revision }, | ||
| ); | ||
| onRestored(next); | ||
| setNotice(`Restored revision ${selected.revision}.`); | ||
| setVersions(null); | ||
| } catch (error) { | ||
| setNotice( | ||
| error instanceof ApiError && error.status === 409 | ||
| ? 'This page changed. Reload the latest revision before restoring.' | ||
| : error instanceof Error | ||
| ? error.message | ||
| : 'Could not restore this version.', | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="page-history"> | ||
| <button | ||
| className="page-history-toggle" | ||
| aria-expanded={open} | ||
| onClick={() => setOpen(!open)} | ||
| > | ||
| <History size={15} /> | ||
| <span>History</span> | ||
| </button> | ||
| {open && ( | ||
| <div | ||
| className="page-history-panel" | ||
| role="region" | ||
| aria-label="Page history" | ||
| > | ||
| {loading && <p className="page-history-empty">Loading history…</p>} | ||
| {!loading && versions?.length === 0 && ( | ||
| <p className="page-history-empty"> | ||
| No earlier revisions yet. Edit and save this page to create the | ||
| first history entry. | ||
| </p> | ||
| )} | ||
| {!!versions?.length && ( | ||
| <div className="page-history-layout"> | ||
| <ul className="page-history-list"> | ||
| {versions!.map((item) => ( | ||
| <li key={item.id}> | ||
| <button | ||
| className={item.id === selectedId ? 'selected' : ''} | ||
| onClick={() => setSelectedId(item.id)} | ||
| aria-pressed={item.id === selectedId} | ||
| > | ||
| <strong>r{item.revision}</strong> | ||
| <span>{item.title}</span> | ||
| <time title={new Date(item.createdAt).toLocaleString()}> | ||
| {new Date(item.createdAt).toLocaleDateString( | ||
| undefined, | ||
| { | ||
| month: 'short', | ||
| day: 'numeric', | ||
| }, | ||
| )} | ||
| </time> | ||
| </button> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| {selected && ( | ||
| <div className="page-history-preview"> | ||
| <h4>{selected.title}</h4> | ||
| <p className="page-history-meta"> | ||
| Revision {selected.revision} ·{' '} | ||
| {new Date(selected.createdAt).toLocaleString()} | ||
| </p> | ||
| <pre>{selected.content.slice(0, 2000)}</pre> | ||
| {selected.content.length > 2000 && ( | ||
| <p className="page-history-meta"> | ||
| Preview truncated at 2,000 of{' '} | ||
| {selected.content.length.toLocaleString()} characters. | ||
| </p> | ||
| )} | ||
| <button | ||
| className="document-primary" | ||
| onClick={() => void restore()} | ||
| > | ||
| <RotateCcw size={14} /> Restore this version | ||
| </button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| )} | ||
| {notice && ( | ||
| <p className="page-history-notice" role="status"> | ||
| {notice} | ||
| </p> | ||
| )} | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Your current draft is kept as a new history entry" is not what happens with unsaved edits: the history entry is the last saved revision, and
PageDocument.tsx:309-314runsreceive(next)thenuseLatest(), which resets the draft to the remote page (autosave.ts:220). So Restore discards an unsaved draft with no conflict banner. Flushing first (controller.flush(true), asbeforeChatalready does) or wording the dialog honestly would fix it.