|
| 1 | +import { customElement, WebComponent } from '@/lib/components' |
| 2 | +import { consume } from '@lit/context' |
| 3 | +import { html, nothing } from 'lit' |
| 4 | +import { property } from 'lit/decorators.js' |
| 5 | +import '@/components/button' |
| 6 | +import '~icons/lucide/x' |
| 7 | +import '~icons/lucide/user-round-plus' |
| 8 | +import styles from './ButtonAddFriend.styles.css' |
| 9 | +import * as debug from '../../lib/debug' |
| 10 | +import { authContext, AuthContext, DEFAULT_AUTH_CONTEXT } from '@/lib/auth' |
| 11 | +import { DataBrowserContext } from 'pane-registry' |
| 12 | +import { LiveStore, NamedNode, st, sym } from 'rdflib' |
| 13 | +import ns from '../../lib/ns' |
| 14 | +import { ensureStandardMutationPrefixes } from './helpers' |
| 15 | + |
| 16 | +const addMeToYourFriendsButtonText = 'Add as Friend' |
| 17 | +const logInAddMeToYourFriendsButtonText = 'Log in to add as friend' |
| 18 | +const friendExistsMessage = 'This friend is already in your list' |
| 19 | +const friendNotAddedMessage = 'Error adding friend, friend not added' |
| 20 | +const userNotLoggedInErrorMessage = 'Please log in first' |
| 21 | + |
| 22 | +@customElement('solid-ui-button-add-friend') |
| 23 | +export default class ButtonAddFriend extends WebComponent { |
| 24 | + static styles = styles |
| 25 | + |
| 26 | + @consume({ context: authContext, subscribe: true }) |
| 27 | + private accessor auth: AuthContext = DEFAULT_AUTH_CONTEXT |
| 28 | + |
| 29 | + @property({ type: Boolean }) |
| 30 | + accessor disabled: boolean | undefined = undefined |
| 31 | + |
| 32 | + @property({ attribute: false }) |
| 33 | + accessor subject: NamedNode | undefined = undefined |
| 34 | + |
| 35 | + @property({ attribute: false }) |
| 36 | + accessor context: DataBrowserContext | undefined = undefined |
| 37 | + |
| 38 | + @property({ attribute: false }) |
| 39 | + accessor buttonLabel = addMeToYourFriendsButtonText |
| 40 | + |
| 41 | + @property({ attribute: false }) |
| 42 | + accessor statusMessage = '' |
| 43 | + |
| 44 | + private checkIfAnyUserLoggedIn(me: NamedNode | null): me is NamedNode { |
| 45 | + return Boolean(me) |
| 46 | + } |
| 47 | + |
| 48 | + private currentUser (): NamedNode | null { |
| 49 | + return this.auth.account ? sym(this.auth.account.webId) : null |
| 50 | + } |
| 51 | + |
| 52 | + protected firstUpdated () { |
| 53 | + void this.refreshButton() |
| 54 | + } |
| 55 | + |
| 56 | + protected updated (changedProperties: Map<PropertyKey, unknown>) { |
| 57 | + super.updated(changedProperties) |
| 58 | + |
| 59 | + if (changedProperties.has('subject') || changedProperties.has('context') || changedProperties.has('auth')) { |
| 60 | + void this.refreshButton() |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private async refreshButton() { |
| 65 | + if (!this.subject || !this.context) { |
| 66 | + this.buttonLabel = logInAddMeToYourFriendsButtonText |
| 67 | + this.disabled = true |
| 68 | + this.statusMessage = '' |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + const me = this.currentUser() |
| 73 | + const store = this.context.session.store as unknown as LiveStore |
| 74 | + |
| 75 | + if (!this.checkIfAnyUserLoggedIn(me)) { |
| 76 | + this.buttonLabel = logInAddMeToYourFriendsButtonText |
| 77 | + this.disabled = true |
| 78 | + this.statusMessage = '' |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + const friendExists = await this.checkIfThingExists(store, me, this.subject, ns.foaf('knows')) |
| 83 | + if (friendExists) { |
| 84 | + this.buttonLabel = friendExistsMessage |
| 85 | + this.disabled = true |
| 86 | + this.statusMessage = '' |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + this.buttonLabel = addMeToYourFriendsButtonText |
| 91 | + this.disabled = false |
| 92 | + this.statusMessage = '' |
| 93 | + } |
| 94 | + |
| 95 | + private async saveNewThing( |
| 96 | + subject: NamedNode, |
| 97 | + context: DataBrowserContext, |
| 98 | + predicate: NamedNode |
| 99 | + ): Promise<void> { |
| 100 | + const me = this.currentUser() |
| 101 | + const store = context.session.store as unknown as LiveStore |
| 102 | + |
| 103 | + if (this.checkIfAnyUserLoggedIn(me)) { |
| 104 | + if (!(await this.checkIfThingExists(store, me, subject, predicate))) { |
| 105 | + await store.fetcher.load(me) |
| 106 | + const updater = store.updater |
| 107 | + if (!updater) { |
| 108 | + throw new Error('Store updater is unavailable') |
| 109 | + } |
| 110 | + const toBeInserted = [st(me, predicate, subject, me.doc())] |
| 111 | + try { |
| 112 | + ensureStandardMutationPrefixes(store) |
| 113 | + await updater.update([], toBeInserted) |
| 114 | + } catch (error) { |
| 115 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 116 | + const message = errorMessage.includes('Unauthenticated') ? userNotLoggedInErrorMessage : errorMessage |
| 117 | + throw new Error(message) |
| 118 | + } |
| 119 | + } else { |
| 120 | + throw new Error(friendExistsMessage) |
| 121 | + } |
| 122 | + } else { |
| 123 | + throw new Error(userNotLoggedInErrorMessage) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private async checkIfThingExists( |
| 128 | + store: LiveStore, |
| 129 | + me: NamedNode, |
| 130 | + subject: NamedNode, |
| 131 | + predicate: NamedNode |
| 132 | + ): Promise<boolean> { |
| 133 | + await store.fetcher.load(me) |
| 134 | + return store.whether(me, predicate, subject, me.doc()) !== 0 |
| 135 | + } |
| 136 | + |
| 137 | + render () { |
| 138 | + return html` |
| 139 | + <solid-ui-button |
| 140 | + variant="secondary" |
| 141 | + ?disabled=${this.disabled} |
| 142 | + @click=${this.onClick} |
| 143 | + > |
| 144 | + <icon-lucide-user-round-plus slot="left-icon"></icon-lucide-user-round-plus> |
| 145 | + ${this.buttonLabel} |
| 146 | + </solid-ui-button> |
| 147 | + ${this.statusMessage |
| 148 | + ? html` |
| 149 | + <div role="status" aria-live="polite" class="button-add-friend__status"> |
| 150 | + <span>${this.statusMessage}</span> |
| 151 | + <solid-ui-button variant="ghost" @click=${this.clearStatusMessage}> |
| 152 | + <span class="sr-only">Close</span> |
| 153 | + <icon-lucide-x slot="icon"></icon-lucide-x> |
| 154 | + </solid-ui-button> |
| 155 | + </div> |
| 156 | + ` |
| 157 | + : nothing} |
| 158 | + ` |
| 159 | + } |
| 160 | + |
| 161 | + private async onClick (event: Event) { |
| 162 | + event.preventDefault() |
| 163 | + |
| 164 | + if (!this.subject || !this.context) { |
| 165 | + this.disabled = true |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + try { |
| 170 | + await this.saveNewThing(this.subject, this.context, ns.foaf('knows')) |
| 171 | + await this.refreshButton() |
| 172 | + } catch (error) { |
| 173 | + this.disabled = true |
| 174 | + this.statusMessage = error instanceof Error ? error.message : friendNotAddedMessage |
| 175 | + debug.error(error) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private clearStatusMessage () { |
| 180 | + this.statusMessage = '' |
| 181 | + } |
| 182 | +} |
0 commit comments