-
Notifications
You must be signed in to change notification settings - Fork 32
Add permalinks for comments and replies #246
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
ruibaby
wants to merge
6
commits into
main
Choose a base branch
from
feat/comment-permalinks
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e49948
feat: add comment and reply permalinks
ruibaby 0b6d349
Fix permalink detail focus and reply state preservation
ruibaby 275ca69
Fix lazy loading and scrolling for comment permalinks
ruibaby 9d0d9ad
Adapt comment permalinks to Halo 2.27 Core APIs
ruibaby 97b356d
Fix permalink navigation lifecycle and browser test setup
ruibaby 4c332e5
Merge remote-tracking branch 'origin/main' into feat/comment-permalinks
ruibaby 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
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,147 @@ | ||
| import type { CommentVo, ReplyVo } from '@halo-dev/api-client'; | ||
| import { consume } from '@lit/context'; | ||
| import { msg } from '@lit/localize'; | ||
| import { css, html, LitElement } from 'lit'; | ||
| import { property, state } from 'lit/decorators.js'; | ||
| import { keyed } from 'lit/directives/keyed.js'; | ||
| import { ofetch } from 'ofetch'; | ||
| import { | ||
| baseUrlContext, | ||
| groupContext, | ||
| kindContext, | ||
| nameContext, | ||
| } from './context'; | ||
| import baseStyles from './styles/base'; | ||
| import { type CommentTarget, scrollWhenVisible } from './utils/comment-link'; | ||
| import type { CommentManagedDetail } from './utils/comment-management'; | ||
| import './comment-item'; | ||
| import './loading-block'; | ||
|
|
||
| export class CommentDetail extends LitElement { | ||
| @consume({ context: baseUrlContext }) @state() baseUrl = ''; | ||
| @consume({ context: groupContext }) @state() group = ''; | ||
| @consume({ context: kindContext }) @state() kind = ''; | ||
| @consume({ context: nameContext }) @state() name = ''; | ||
| @property({ attribute: false }) target!: CommentTarget; | ||
| @state() private comment?: CommentVo; | ||
| @state() private reply?: ReplyVo; | ||
| @state() private loading = true; | ||
| @state() private error = ''; | ||
| @state() private retryable = false; | ||
| private requestId = 0; | ||
| private cancelScroll?: () => void; | ||
| private activeReplyItem?: { closeReplyForm(): void }; | ||
|
|
||
| override connectedCallback() { | ||
| super.connectedCallback(); | ||
| void this.load(); | ||
| } | ||
|
|
||
| override disconnectedCallback() { | ||
| ++this.requestId; | ||
| this.cancelScroll?.(); | ||
| this.activeReplyItem?.closeReplyForm(); | ||
| super.disconnectedCallback(); | ||
| } | ||
|
|
||
| private async load() { | ||
| this.cancelScroll?.(); | ||
| const requestId = ++this.requestId; | ||
| this.loading = true; | ||
| this.error = ''; | ||
| this.retryable = false; | ||
| try { | ||
| const commentName = encodeURIComponent(this.target.commentName); | ||
| const comment = await ofetch<CommentVo>( | ||
| `${this.baseUrl}/apis/api.halo.run/v1alpha1/comments/${commentName}`, | ||
| { retry: 0 } | ||
| ); | ||
| if (requestId !== this.requestId) return; | ||
| const subject = comment.spec.subjectRef; | ||
| if ( | ||
| subject.group !== this.group || | ||
| subject.kind !== this.kind || | ||
| subject.name !== this.name | ||
| ) { | ||
| ++this.requestId; | ||
| this.dispatchEvent(new CustomEvent('comment-subject-mismatch')); | ||
| return; | ||
| } | ||
| const reply = this.target.replyName | ||
| ? await ofetch<ReplyVo>( | ||
| `${this.baseUrl}/apis/api.halo.run/v1alpha1/comments/${commentName}/reply/${encodeURIComponent(this.target.replyName)}`, | ||
| { retry: 0 } | ||
| ) | ||
| : undefined; | ||
| if (requestId !== this.requestId) return; | ||
| this.comment = comment; | ||
| this.reply = reply; | ||
| } catch (error) { | ||
| if (requestId !== this.requestId) return; | ||
| this.retryable = (error as { status?: number }).status !== 404; | ||
| this.error = !this.retryable | ||
| ? msg('Comment not found or unavailable') | ||
| : msg('Failed to load comment, please try again'); | ||
| } finally { | ||
| if (requestId === this.requestId) { | ||
| this.loading = false; | ||
| await this.updateComplete; | ||
| if ( | ||
| requestId === this.requestId && | ||
| this.isConnected && | ||
| (!this.target.replyName || this.error) | ||
| ) { | ||
| this.cancelScroll = scrollWhenVisible(this, 'start'); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private retry() { | ||
| this.tabIndex = -1; | ||
| this.focus({ preventScroll: true }); | ||
| void this.load(); | ||
| } | ||
|
|
||
| private returnToList() { | ||
| this.dispatchEvent( | ||
| new CustomEvent('comment-list-requested', { | ||
| bubbles: true, | ||
| composed: true, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| override render() { | ||
| return html`<div class="detail mt-5" @comment-managed=${( | ||
| event: CustomEvent<CommentManagedDetail> | ||
| ) => { | ||
| if (event.detail.restoreFocus) { | ||
| this.tabIndex = -1; | ||
| this.focus({ preventScroll: true }); | ||
| } | ||
| void this.load(); | ||
| }} @reply-form-open=${(event: CustomEvent<{ closeReplyForm(): void }>) => { | ||
| event.stopPropagation(); | ||
| if (this.activeReplyItem !== event.detail) | ||
| this.activeReplyItem?.closeReplyForm(); | ||
| this.activeReplyItem = event.detail; | ||
| }}> | ||
| <button type="button" class="back text-sm text-primary-1" @click=${this.returnToList}>${msg('Back to comments')}</button> | ||
| ${this.loading ? html`<loading-block></loading-block>` : this.error ? html`<p role="status" class="text-sm text-text-2 my-3">${this.error}</p>${this.retryable ? html`<button type="button" class="retry back text-sm text-primary-1" @click=${this.retry}>${msg('Retry')}</button>` : ''}` : keyed(this.requestId, html`<comment-item .comment=${this.comment} .detail=${true} .targetReply=${this.reply}></comment-item>`)} | ||
| </div>`; | ||
| } | ||
|
|
||
| static override styles = [ | ||
| ...baseStyles, | ||
| css` | ||
| :host { display: block; scroll-margin-top: 5rem; } | ||
| .back { cursor: pointer; text-decoration: underline; text-underline-offset: 3px; } | ||
| .back:focus-visible { outline: 2px solid var(--halo-cw-primary-1-color); outline-offset: 2px; } | ||
| @unocss-placeholder; | ||
| `, | ||
| ]; | ||
| } | ||
|
|
||
| customElements.get('comment-detail') || | ||
| customElements.define('comment-detail', CommentDetail); | ||
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
Oops, something went wrong.
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.
When widgets target two API versions with the same group, kind, and resource name, this check accepts a comment from the other version and renders it in the current widget. The normal list and comment submission both include
version, so permalink detail should also consumeversionContextand requiresubject.version === this.versionto preserve the same subject boundary.Useful? React with 👍 / 👎.