-
Notifications
You must be signed in to change notification settings - Fork 46
make an "add as friend" web component #826
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
SharonStrats
wants to merge
2
commits into
staging
Choose a base branch
from
feat/add-friends-component
base: staging
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
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/components/button-add-friend/ButtonAddFriend.stories.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { html } from 'lit' | ||
| import { sym } from 'rdflib' | ||
| import { DataBrowserContext } from 'pane-registry' | ||
| import { defineAuthStoryRender, USER_OPTIONS } from '@/storybook' | ||
|
|
||
| import './ButtonAddFriend' | ||
|
|
||
| type StoryArgs = { | ||
| user: typeof USER_OPTIONS.control | ||
| subjectUri: string | ||
| friendExists: boolean | ||
| } | ||
|
|
||
| const meta = { | ||
| title: 'ButtonAddFriend', | ||
| args: { | ||
| user: 'Guest', | ||
| subjectUri: 'https://example.com/profile/card#me', | ||
| friendExists: false, | ||
| }, | ||
| argTypes: { | ||
| user: USER_OPTIONS.control, | ||
| subjectUri: { control: 'text' }, | ||
| friendExists: { control: 'boolean' }, | ||
| }, | ||
| } as const | ||
|
|
||
| function createMockContext(friendExists: boolean): DataBrowserContext { | ||
| const store = { | ||
| fetcher: { | ||
| load: async () => undefined, | ||
| }, | ||
| updater: { | ||
| update: async () => undefined, | ||
| }, | ||
| whether: () => (friendExists ? 1 : 0), | ||
| } | ||
|
|
||
| return { | ||
| dom: document, | ||
| environment: { layout: 'desktop' }, | ||
| session: { store }, | ||
| } as unknown as DataBrowserContext | ||
| } | ||
|
|
||
| const render = defineAuthStoryRender<StoryArgs>(({ subjectUri, friendExists }) => { | ||
| const context = createMockContext(friendExists) | ||
| const subject = sym(subjectUri) | ||
|
|
||
| return html` | ||
| <solid-ui-button-add-friend .context=${context} .subject=${subject}></solid-ui-button-add-friend> | ||
| ` | ||
| }) | ||
|
|
||
| export default meta | ||
|
|
||
| export const Guest = { | ||
| render, | ||
| } | ||
|
|
||
| export const LoggedIn = { | ||
| render, | ||
| args: { | ||
| user: 'Alice', | ||
| }, | ||
| } | ||
|
|
||
| export const FriendExists = { | ||
| render, | ||
| args: { | ||
| user: 'Alice', | ||
| friendExists: true, | ||
| }, | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/components/button-add-friend/ButtonAddFriend.styles.css
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,19 @@ | ||
| .button-add-friend__status { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| gap: 8px; | ||
| margin-top: 12px; | ||
| padding: 8px 12px; | ||
| border: 1px solid var(--red-300, #f5c2c7); | ||
| border-radius: 8px; | ||
| background: #fee; | ||
| color: var(--red-700, #b42318); | ||
| } | ||
|
|
||
| .button-add-friend__status span { | ||
| line-height: 1.2; | ||
| } | ||
|
|
||
| .button-add-friend__status solid-ui-button { | ||
| flex: 0 0 auto; | ||
| } | ||
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,182 @@ | ||
| import { customElement, WebComponent } from '@/lib/components' | ||
| import { consume } from '@lit/context' | ||
| import { html, nothing } from 'lit' | ||
| import { property } from 'lit/decorators.js' | ||
| import '@/components/button' | ||
| import '~icons/lucide/x' | ||
| import '~icons/lucide/user-round-plus' | ||
| import styles from './ButtonAddFriend.styles.css' | ||
| import * as debug from '../../lib/debug' | ||
| import { authContext, AuthContext, DEFAULT_AUTH_CONTEXT } from '@/lib/auth' | ||
| import { DataBrowserContext } from 'pane-registry' | ||
| import { LiveStore, NamedNode, st, sym } from 'rdflib' | ||
| import ns from '../../lib/ns' | ||
| import { ensureStandardMutationPrefixes } from './helpers' | ||
|
|
||
| const addMeToYourFriendsButtonText = 'Add as Friend' | ||
| const logInAddMeToYourFriendsButtonText = 'Log in to add as friend' | ||
| const friendExistsMessage = 'This friend is already in your list' | ||
| const friendNotAddedMessage = 'Error adding friend, friend not added' | ||
| const userNotLoggedInErrorMessage = 'Please log in first' | ||
|
|
||
| @customElement('solid-ui-button-add-friend') | ||
| export default class ButtonAddFriend extends WebComponent { | ||
| static styles = styles | ||
|
|
||
| @consume({ context: authContext, subscribe: true }) | ||
| private accessor auth: AuthContext = DEFAULT_AUTH_CONTEXT | ||
|
|
||
| @property({ type: Boolean }) | ||
| accessor disabled: boolean | undefined = undefined | ||
|
|
||
| @property({ attribute: false }) | ||
| accessor subject: NamedNode | undefined = undefined | ||
|
|
||
| @property({ attribute: false }) | ||
| accessor context: DataBrowserContext | undefined = undefined | ||
|
|
||
| @property({ attribute: false }) | ||
| accessor buttonLabel = addMeToYourFriendsButtonText | ||
|
|
||
| @property({ attribute: false }) | ||
| accessor statusMessage = '' | ||
|
|
||
| private checkIfAnyUserLoggedIn(me: NamedNode | null): me is NamedNode { | ||
| return Boolean(me) | ||
| } | ||
|
|
||
| private currentUser (): NamedNode | null { | ||
| return this.auth.account ? sym(this.auth.account.webId) : null | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| protected firstUpdated () { | ||
| void this.refreshButton() | ||
| } | ||
|
|
||
| protected updated (changedProperties: Map<PropertyKey, unknown>) { | ||
| super.updated(changedProperties) | ||
|
|
||
| if (changedProperties.has('subject') || changedProperties.has('context') || changedProperties.has('auth')) { | ||
| void this.refreshButton() | ||
| } | ||
| } | ||
|
|
||
| private async refreshButton() { | ||
| if (!this.subject || !this.context) { | ||
| this.buttonLabel = logInAddMeToYourFriendsButtonText | ||
| this.disabled = true | ||
| this.statusMessage = '' | ||
| return | ||
| } | ||
|
|
||
| const me = this.currentUser() | ||
| const store = this.context.session.store as unknown as LiveStore | ||
|
|
||
| if (!this.checkIfAnyUserLoggedIn(me)) { | ||
| this.buttonLabel = logInAddMeToYourFriendsButtonText | ||
| this.disabled = true | ||
| this.statusMessage = '' | ||
| return | ||
| } | ||
|
|
||
| const friendExists = await this.checkIfThingExists(store, me, this.subject, ns.foaf('knows')) | ||
| if (friendExists) { | ||
| this.buttonLabel = friendExistsMessage | ||
| this.disabled = true | ||
| this.statusMessage = '' | ||
| return | ||
| } | ||
|
|
||
| this.buttonLabel = addMeToYourFriendsButtonText | ||
| this.disabled = false | ||
| this.statusMessage = '' | ||
| } | ||
|
|
||
| private async saveNewThing( | ||
| subject: NamedNode, | ||
| context: DataBrowserContext, | ||
| predicate: NamedNode | ||
| ): Promise<void> { | ||
| const me = this.currentUser() | ||
| const store = context.session.store as unknown as LiveStore | ||
|
|
||
| if (this.checkIfAnyUserLoggedIn(me)) { | ||
| if (!(await this.checkIfThingExists(store, me, subject, predicate))) { | ||
| await store.fetcher.load(me) | ||
| const updater = store.updater | ||
| if (!updater) { | ||
| throw new Error('Store updater is unavailable') | ||
| } | ||
| const toBeInserted = [st(me, predicate, subject, me.doc())] | ||
| try { | ||
| ensureStandardMutationPrefixes(store) | ||
| await updater.update([], toBeInserted) | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : String(error) | ||
| const message = errorMessage.includes('Unauthenticated') ? userNotLoggedInErrorMessage : errorMessage | ||
| throw new Error(message) | ||
| } | ||
| } else { | ||
| throw new Error(friendExistsMessage) | ||
| } | ||
| } else { | ||
| throw new Error(userNotLoggedInErrorMessage) | ||
| } | ||
| } | ||
|
|
||
| private async checkIfThingExists( | ||
| store: LiveStore, | ||
| me: NamedNode, | ||
| subject: NamedNode, | ||
| predicate: NamedNode | ||
| ): Promise<boolean> { | ||
| await store.fetcher.load(me) | ||
| return store.whether(me, predicate, subject, me.doc()) !== 0 | ||
| } | ||
|
|
||
| render () { | ||
| return html` | ||
| <solid-ui-button | ||
| variant="secondary" | ||
| ?disabled=${this.disabled} | ||
| @click=${this.onClick} | ||
| > | ||
| <icon-lucide-user-round-plus slot="left-icon"></icon-lucide-user-round-plus> | ||
| ${this.buttonLabel} | ||
| </solid-ui-button> | ||
| ${this.statusMessage | ||
| ? html` | ||
| <div role="status" aria-live="polite" class="button-add-friend__status"> | ||
| <span>${this.statusMessage}</span> | ||
| <solid-ui-button variant="ghost" @click=${this.clearStatusMessage}> | ||
| <span class="sr-only">Close</span> | ||
| <icon-lucide-x slot="icon"></icon-lucide-x> | ||
| </solid-ui-button> | ||
| </div> | ||
| ` | ||
| : nothing} | ||
| ` | ||
| } | ||
|
|
||
| private async onClick (event: Event) { | ||
| event.preventDefault() | ||
|
|
||
| if (!this.subject || !this.context) { | ||
| this.disabled = true | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| await this.saveNewThing(this.subject, this.context, ns.foaf('knows')) | ||
| await this.refreshButton() | ||
| } catch (error) { | ||
| this.disabled = true | ||
| this.statusMessage = error instanceof Error ? error.message : friendNotAddedMessage | ||
| debug.error(error) | ||
| } | ||
| } | ||
|
|
||
| private clearStatusMessage () { | ||
| this.statusMessage = '' | ||
| } | ||
| } | ||
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,48 @@ | ||
| import { LiveStore } from 'rdflib' | ||
|
|
||
| type PrefixCapable = { | ||
| setPrefixForURI?: (prefix: string, uri: string) => void | ||
| namespaces?: Record<string, string> | ||
| store?: PrefixCapable | ||
| } | ||
|
|
||
| const STANDARD_MUTATION_PREFIXES: Record<string, string> = { | ||
| rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', | ||
| vcard: 'http://www.w3.org/2006/vcard/ns#', | ||
| foaf: 'http://xmlns.com/foaf/0.1/', | ||
| solid: 'http://www.w3.org/ns/solid/terms#', | ||
| schema: 'http://schema.org/', | ||
| org: 'http://www.w3.org/ns/org#', | ||
| owl: 'http://www.w3.org/2002/07/owl#', | ||
| dc: 'http://purl.org/dc/elements/1.1/' | ||
| } | ||
|
|
||
| function registerStorePrefix(target: PrefixCapable | undefined, prefix: string, uri: string): void { | ||
| if (!target) return | ||
| if (typeof target.setPrefixForURI === 'function') { | ||
| target.setPrefixForURI(prefix, uri) | ||
| return | ||
| } | ||
| if (!target.namespaces) { | ||
| target.namespaces = {} | ||
| } | ||
| target.namespaces[prefix] = uri | ||
| } | ||
|
|
||
| function getStoreUpdater(store: LiveStore): PrefixCapable | undefined { | ||
| return store.updater as PrefixCapable | undefined | ||
| } | ||
|
|
||
| export function ensureStandardMutationPrefixes(store: LiveStore | undefined): void { | ||
| if (!store) return | ||
|
|
||
| const updater = getStoreUpdater(store) | ||
| const nestedStore = (updater as { store?: PrefixCapable } | undefined)?.store | ||
| const targets: Array<PrefixCapable | undefined> = [store as PrefixCapable, updater, nestedStore] | ||
|
|
||
| Object.entries(STANDARD_MUTATION_PREFIXES).forEach(([prefix, uri]) => { | ||
| targets.forEach((target) => { | ||
| registerStorePrefix(target, prefix, uri) | ||
| }) | ||
| }) | ||
| } |
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,4 @@ | ||
| import ButtonAddFriend from './ButtonAddFriend' | ||
|
|
||
| export { ButtonAddFriend } | ||
| export default ButtonAddFriend |
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.
Uh oh!
There was an error while loading. Please reload this page.