refactor(docflow): extract doc permission/collab/template logic into …#326
refactor(docflow): extract doc permission/collab/template logic into …#326
Conversation
…hooks - split docs/[room]/page.tsx into useDocumentPermission, useCollaboration and useTemplateInsertion - centralize readonly resolution with precedence: query readonly > server permission > HTTP fallback - gate collaboration bootstrap by IndexedDB restore + server sync before editor init - keep editable state synced via editor.setEditable without recreating editor instance
❌ Deploy Preview for ornate-blancmange-89db6b failed. Why did it fail? →
|
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the DocumentPage component by extracting collaboration, permission, and template insertion logic into dedicated custom hooks (useCollaboration, useDocumentPermission, useTemplateInsertion). This significantly improves modularity and readability. The DocumentPage now primarily orchestrates these hooks and renders the UI. Feedback includes removing a redundant useEffect for editor cleanup, ensuring immediate synchronization of user awareness state in useCollaboration to prevent UI delays, and refining the template insertion logic in useTemplateInsertion to consistently clear templates from local storage after an attempt to use them.
| const updatedContents = { ...templateContents }; | ||
| delete updatedContents[docIdString]; | ||
| storage.set(STORAGE_KEYS.TEMPLATE_CONTENT, updatedContents); | ||
| useEffect(() => () => clearEditor(), [clearEditor]); |
| provider.awareness.on('update', sync); | ||
|
|
||
| return () => provider.awareness?.off('update', sync); |
| try { | ||
| if (!editor || editor.isDestroyed) return; | ||
|
|
||
| const updatedContents = { ...templateContents }; | ||
| delete updatedContents[docIdString]; | ||
|
|
||
| if (editor.getText().trim().length > 0) { | ||
| storage.set(STORAGE_KEYS.TEMPLATE_CONTENT, updatedContents); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!editor.commands.pasteMarkdown) return; | ||
|
|
||
| editor.commands.clearContent(); | ||
|
|
||
| if (editor.commands.pasteMarkdown(templateContent)) { | ||
| storage.set(STORAGE_KEYS.TEMPLATE_CONTENT, updatedContents); | ||
| setTimeout(() => editor.commands.focus('start'), 100); | ||
| } | ||
| } catch { | ||
| // silently ignore | ||
| } |
There was a problem hiding this comment.
这个 hook 顶部的注释说:“无论结果如何,都清除存储的模板”。然而,目前的实现只在文档不为空或 pasteMarkdown 成功时才从存储中删除模板。如果粘贴失败,模板将保留在存储中,并在下次加载时再次尝试插入,这可能不是预期的行为。
为了使行为更健壮并与注释的意图保持一致,应该在读取模板后立即从存储中删除它。
try {
if (!editor || editor.isDestroyed) return;
// 一旦我们尝试使用模板,就认为它已被“消费”。
// 立即从存储中删除它,以防止将来加载时重新插入。
const updatedContents = { ...templateContents };
delete updatedContents[docIdString];
storage.set(STORAGE_KEYS.TEMPLATE_CONTENT, updatedContents);
if (editor.getText().trim().length > 0) {
// 文档已有内容,不插入模板。
return;
}
if (editor.commands.pasteMarkdown) {
editor.commands.clearContent();
if (editor.commands.pasteMarkdown(templateContent)) {
setTimeout(() => editor.commands.focus('start'), 100);
}
}
} catch {
// 静默忽略
}
…hooks
PR 描述
PR 类型
Issue 关联
Closes #
其他信息