git clone https://garage44.eu/jeroen/cartopolis.git.. /crates/atlas/web/src/components/elements/providers/providers.tsx
import m, {MithrilComponent} from '@garage44/mithril'
import {Button} from '@garage44/mithril/components'
import type {Provider, ProviderKind} from '@/types'
import {$s} from '@/app'
import {asksServer, startUrl} from '@/lib/signin'
/*
* The providers' own marks, as the mockups draw them
* (`docs/mockups/external-sign-in/`). Their colours are the providers', not the
* UI's, which is why they are the one place in this app that names a colour:
* a Google "G" in the theme's accent would not be Google's.
*/
const MARKS: Record<string, {box: string; body: () => m.Children}> = {
codeberg: {
body: () => [
<path d="M12 3L1.5 20.5h21z" fill="#2185D0" opacity=".55" />,
<path d="M12 3l-4.2 17.5h8.4z" fill="#2185D0" />,
],
box: '0 0 24 24',
},
discord: {
body: () => [
<rect fill="#5865F2" height="22" rx="6" width="22" x="1" y="1" />,
<path
d="M6 8.2c1.6-1.1 3.2-1.5 3.6-1.5l.4.8c1.3-.2 2.7-.2 4 0l.4-.8c.4 0 2 .4 3.6 1.5 1.3 2.6 1.6 5.2 1.4 7.6-1.4 1-2.6 1.4-3.6 1.6l-.8-1.2c.6-.2 1.1-.5 1.5-.8-2.6 1.2-6.4 1.2-9 0 .4.3.9.6 1.5.8l-.8 1.2c-1-.2-2.2-.6-3.6-1.6-.2-2.4.1-5 1.4-7.6z"
fill="#fff"
/>,
<circle cx="9.4" cy="12.6" fill="#5865F2" r="1.3" />,
<circle cx="14.6" cy="12.6" fill="#5865F2" r="1.3" />,
],
box: '0 0 24 24',
},
github: {
body: () => (
<path
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"
fill="currentColor"
/>
),
box: '0 0 16 16',
},
google: {
body: () => [
<path
d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"
fill="#EA4335"
/>,
<path
d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"
fill="#4285F4"
/>,
<path
d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"
fill="#FBBC05"
/>,
<path
d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"
fill="#34A853"
/>,
],
box: '0 0 48 48',
},
mastodon: {
body: () => [
<rect fill="#6364FF" height="20" rx="6" width="20" x="2" y="2" />,
<path
d="M7.5 16V10a2.25 2.25 0 0 1 4.5 0v3M12 13v-3a2.25 2.25 0 0 1 4.5 0v6"
fill="none"
stroke="#fff"
stroke-linecap="round"
stroke-width="2"
/>,
],
box: '0 0 24 24',
},
reddit: {
body: () => [
<circle cx="12" cy="12" fill="#FF4500" r="11" />,
<ellipse cx="12" cy="14" fill="#fff" rx="6.2" ry="4.3" />,
<circle cx="17.6" cy="10.4" fill="#fff" r="1.5" />,
<circle cx="6.4" cy="10.4" fill="#fff" r="1.5" />,
<circle cx="16" cy="5.6" fill="#fff" r="1.2" />,
<path d="M12 9.8l1-4.2 3 .6" fill="none" stroke="#fff" stroke-width="1" />,
<circle cx="9.7" cy="13.4" fill="#FF4500" r="1" />,
<circle cx="14.3" cy="13.4" fill="#FF4500" r="1" />,
<path d="M9.8 15.8c1.3.8 3.1.8 4.4 0" fill="none" stroke="#FF4500" stroke-linecap="round" stroke-width=".9" />,
],
box: '0 0 24 24',
},
// A community's own OpenID server: a key, in the text colour.
key: {
body: () => [
<circle cx="8" cy="15" fill="none" r="4" stroke="currentColor" stroke-width="2" />,
<path d="M11 12l8-8M16 7l2.5 2.5M14 9l2 2" fill="none" stroke="currentColor" stroke-linecap="round" stroke-width="2" />,
],
box: '0 0 24 24',
},
}
/**
* A provider's mark: by id for the six presets, by kind for the rest (a
* second GitHub-like server still looks like what it talks to), and a key for
* a custom OpenID server.
*/
export function ProviderLogo(p: {id: string; kind?: ProviderKind}, size = 18) {
const mark = MARKS[p.id] ?? (p.kind && p.kind !== 'oidc' ? MARKS[p.kind] : undefined) ?? MARKS.key
return (
<svg aria-hidden="true" class="provider-logo" height={size} viewBox={mark.box} width={size}>
{mark.body()}
</svg>
)
}
/**
* Leave for a provider: the fediverse asks which server first, everything else
* goes straight to the server's start address, which redirects onwards.
*/
export function signInWith(p: Provider, ret: string, app?: string) {
if (asksServer(p)) {
m.route.set('/signin/fediverse', {return: ret, ...(app ? {app} : {})})
return
}
location.assign(startUrl(p.id, ret, {app}))
}
interface ChoicesAttrs {
/** Disables the buttons while the card's own form is busy. */
disabled?: boolean
/** Words for the line under the grid; the default is the sign-in card's. */
or?: string
/** Where to come back to; the current page by default. */
return?: string
}
/**
* The provider buttons above a sign-in form, two to a row, in the operator's
* order, with the line that leads into the name and password under them. Draws
* nothing on a server with none switched on, so the card is exactly what it was.
*/
export class ProviderChoices extends MithrilComponent<ChoicesAttrs> {
view(vnode: m.Vnode<ChoicesAttrs>) {
const providers = $s.registration?.providers ?? []
if (!providers.length) return null
const ret = vnode.attrs.return ?? location.pathname + location.search
return (
<div class="provider-choices">
<div class="provider-grid">
{providers.map((p) => (
<Button
class="provider-button"
disabled={vnode.attrs.disabled}
icon={ProviderLogo(p)}
key={p.id}
onclick={() => signInWith(p, ret)}
>
{p.name}
</Button>
))}
</div>
<div class="provider-or">{vnode.attrs.or ?? 'or with your Cartopolis name'}</div>
</div>
)
}
}