-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Move favorite charm logic to its own view and discerns between local and remote state #2181
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { css, html, LitElement, PropertyValues } from "lit"; | ||
| import { property, state } from "lit/decorators.js"; | ||
| import { RuntimeInternals } from "../lib/runtime.ts"; | ||
| import { Task } from "@lit/task"; | ||
|
|
||
| export class XFavoriteButtonElement extends LitElement { | ||
| static override styles = css` | ||
| x-button.emoji-button { | ||
| opacity: 0.7; | ||
| transition: opacity 0.2s; | ||
| font-size: 1rem; | ||
| } | ||
|
|
||
| x-button.emoji-button:hover { | ||
| opacity: 1; | ||
| } | ||
|
|
||
| x-button.auth-button { | ||
| font-size: 1rem; | ||
| } | ||
| `; | ||
|
|
||
| @property() | ||
| rt?: RuntimeInternals; | ||
|
|
||
| @property({ attribute: false }) | ||
| charmId?: string; | ||
|
|
||
| // Local state for favoriting, used when | ||
| // modifying state inbetween server syncs. | ||
| @state() | ||
| isFavorite: boolean | undefined = undefined; | ||
|
|
||
| private async handleFavoriteClick(e: Event) { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| if (!this.rt || !this.charmId) return; | ||
| const manager = this.rt.cc().manager(); | ||
|
|
||
| const isFavorite = this.deriveIsFavorite(); | ||
|
|
||
| // Update local state, and use until overridden by | ||
| // syncing state, or another click. | ||
| this.isFavorite = !isFavorite; | ||
|
|
||
| const charmCell = (await this.rt.cc().get(this.charmId, true)).getCell(); | ||
| if (isFavorite) { | ||
| await manager.removeFavorite(charmCell); | ||
| } else { | ||
| await manager.addFavorite(charmCell); | ||
| } | ||
|
|
||
| this.isFavoriteSync.run(); | ||
| } | ||
|
|
||
| protected override willUpdate(changedProperties: PropertyValues): void { | ||
| if (changedProperties.has("charmId")) { | ||
| this.isFavorite = undefined; | ||
| } | ||
| } | ||
|
|
||
| private deriveIsFavorite(): boolean { | ||
| // If `isFavorite` is defined, we have local state that is not | ||
| // yet synced. Prefer local state if defined, otherwise use server state. | ||
| return this.isFavorite ?? this.isFavoriteSync.value ?? false; | ||
| } | ||
|
|
||
| isFavoriteSync = new Task(this, { | ||
| task: async ( | ||
| [charmId, rt], | ||
| { signal }, | ||
| ): Promise<boolean> => { | ||
| const isFavorite = await isFavoriteSync(rt, charmId); | ||
|
|
||
| // If another favorite request was initiated, store | ||
| // the sync status, but don't overwrite the local state. | ||
| if (signal.aborted) return isFavorite; | ||
|
|
||
| // We update `this.isFavorite` here to `undefined`, | ||
| // indicating that the synced state should be preferred | ||
| // now that it's fresh. | ||
| this.isFavorite = undefined; | ||
| return isFavorite; | ||
| }, | ||
| args: () => [this.charmId, this.rt], | ||
| }); | ||
|
|
||
| override render() { | ||
| const isFavorite = this.deriveIsFavorite(); | ||
|
|
||
| return html` | ||
| <x-button | ||
| class="emoji-button" | ||
| size="small" | ||
| @click="${this.handleFavoriteClick}" | ||
| title="${isFavorite ? "Remove from Favorites" : "Add to Favorites"}" | ||
| > | ||
| ${isFavorite ? "⭐" : "☆"} | ||
| </x-button> | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| globalThis.customElements.define("x-favorite-button", XFavoriteButtonElement); | ||
|
|
||
| async function isFavoriteSync( | ||
| rt?: RuntimeInternals, | ||
| charmId?: string, | ||
| ): Promise<boolean> { | ||
| if (!charmId || !rt) { | ||
| return false; | ||
| } | ||
| const manager = rt.cc().manager(); | ||
| try { | ||
| const charm = await manager.get(charmId, true); | ||
| if (charm) { | ||
| const favorites = manager.getFavorites(); | ||
| await favorites.sync(); | ||
| return manager.isFavorite(charm); | ||
| } else { | ||
| return false; | ||
| } | ||
| } catch (_) { | ||
| // | ||
| } | ||
| return false; | ||
| } | ||
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export * from "./Button.ts"; | ||
| export * from "./CharmLink.ts"; | ||
| export * from "./CTLogo.ts"; | ||
| export * from "./FavoriteButton.ts"; | ||
| export * from "./Flex.ts"; | ||
| export * from "./Spinner.ts"; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: Ensure the sync task runs even when the favorite mutation fails so the optimistic state doesn’t stay stuck after an error.
Prompt for AI agents