Files
immich-magic-proxy/app/src/magic/views.tsx
T

372 lines
16 KiB
TypeScript

import { ComponentChildren } from 'preact'
import { CommentRecord, Frequency, FREQUENCIES, Recipient, ShareRecord } from './store'
const FREQUENCY_LABELS: Record<Frequency, string> = {
off: 'Never',
daily: 'Daily',
weekly: 'Weekly',
monthly: 'Monthly'
}
const STYLE = `
main.container { max-width: 760px; padding-top: 2rem; padding-bottom: 3rem; }
.muted { color: var(--pico-muted-color); }
.notice { padding: .75rem 1rem; border-radius: var(--pico-border-radius); background: var(--pico-card-sectioning-background-color); margin-bottom: 1rem; }
.notice.error { border-left: 4px solid var(--pico-del-color); }
.notice.ok { border-left: 4px solid var(--pico-ins-color); }
.link-box { word-break: break-all; font-family: var(--pico-font-family-monospace); font-size: .85rem; }
.row-actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.row-actions form { margin: 0; }
.row-actions button { margin: 0; padding: .35rem .75rem; font-size: .85rem; }
table td, table th { vertical-align: top; font-size: .9rem; }
fieldset.checks label { display: block; }
.albums a { display: block; padding: .6rem .9rem; margin-bottom: .5rem; border-radius: var(--pico-border-radius); background: var(--pico-card-background-color); box-shadow: var(--pico-card-box-shadow); text-decoration: none; }
`
// Forms with data-confirm ask before submitting
const CONFIRM_SCRIPT = `document.addEventListener('submit', function (e) {
var message = e.target.getAttribute('data-confirm')
if (message && !confirm(message)) e.preventDefault()
})`
function Layout ({ title, children }: { title: string, children: ComponentChildren }) {
return (
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="referrer" content="no-referrer"/>
<meta name="robots" content="noindex, nofollow"/>
<title>{title}</title>
<link rel="icon" href="/share/static/favicon.ico" type="image/x-icon"/>
<link type="text/css" rel="stylesheet" href="/share/static/pico.min.css"/>
<style dangerouslySetInnerHTML={{ __html: STYLE }}/>
</head>
<body>
<main class="container">{children}</main>
<script dangerouslySetInnerHTML={{ __html: CONFIRM_SCRIPT }}/>
</body>
</html>
)
}
function Notice ({ kind, children }: { kind: 'ok' | 'error', children: ComponentChildren }) {
return <div class={'notice ' + kind} role={kind === 'error' ? 'alert' : 'status'}>{children}</div>
}
// ----- visitors --------------------------------------------------------------
/** Shown in place of anything gated. Reveals nothing about what was asked for. */
export function AccessDenied ({ signedIn, invalidLink }: { signedIn: boolean, invalidLink?: boolean }) {
return (
<Layout title="Private photos">
<h1>Private photos</h1>
{invalidLink && <Notice kind="error">That link is no longer valid.</Notice>}
{signedIn
? <p>This page isn't available to you. <a href="/me">See the albums you can view</a>.</p>
: <>
<p>These photos are only visible through a personal invitation link. Open the link from your invitation email to continue.</p>
<RequestLinkForm/>
</>}
</Layout>
)
}
function RequestLinkForm () {
return (
<form method="post" action="/access">
<label>
Lost your link? Enter the email address you were invited with and we'll send a new one.
<input type="email" name="email" required autocomplete="email" placeholder="you@example.com"/>
</label>
<button type="submit">Email me a link</button>
</form>
)
}
export function LinkRequested () {
return (
<Layout title="Check your email">
<h1>Check your email</h1>
<p>If that address has been invited, a sign-in link is on its way. It works for 7 days.</p>
</Layout>
)
}
export interface RecipientHomeProps {
recipient: Recipient
shares: ShareRecord[]
saved?: boolean
emailEnabled: boolean
}
export function RecipientHome ({ recipient, shares, saved, emailEnabled }: RecipientHomeProps) {
return (
<Layout title="Your albums">
<hgroup>
<h1>Hi {recipient.name}</h1>
<p class="muted">Bookmark this page to come back to your albums. This browser stays signed in.</p>
</hgroup>
<section class="albums">
<h2>Albums</h2>
{shares.length
? shares.map(share => <a key={share.key} href={'/share/' + share.key}>{share.title || 'Shared album'}</a>)
: <p class="muted">Nothing has been shared with you yet.</p>}
</section>
<section>
<h2>Email notifications</h2>
{saved && <Notice kind="ok">Saved.</Notice>}
<form method="post" action="/me/notifications">
<fieldset>
<legend>Email me about new photos in these albums ({recipient.email}):</legend>
{FREQUENCIES.map(frequency => (
<label key={frequency}>
<input type="radio" name="frequency" value={frequency} checked={recipient.frequency === frequency}/>
{frequency === 'off' ? 'Never' : FREQUENCY_LABELS[frequency] + ' summary'}
</label>
))}
</fieldset>
{!emailEnabled && <p class="muted"><small>Email isn't set up on this server yet, so nothing will be sent for now.</small></p>}
<button type="submit">Save</button>
</form>
</section>
<section>
<form method="post" action="/logout">
<button type="submit" class="secondary outline">Sign out of this browser</button>
</form>
</section>
</Layout>
)
}
// ----- admin -----------------------------------------------------------------
export function AdminLogin ({ failed }: { failed?: boolean }) {
return (
<Layout title="Admin sign-in">
<h1>Admin</h1>
{failed && <Notice kind="error">Wrong password, or too many attempts. Try again in a few minutes.</Notice>}
<form method="post" action="/admin/login">
<input type="password" name="password" placeholder="Admin password" aria-label="Admin password" required autofocus/>
<button type="submit">Sign in</button>
</form>
</Layout>
)
}
export interface AdminShareRow { share: ShareRecord, recipients: number, assets: number }
export interface AdminRecipientRow { recipient: Recipient, shares: ShareRecord[], sessions: number }
export interface AdminDashboardProps {
shares: AdminShareRow[]
recipients: AdminRecipientRow[]
comments: Array<CommentRecord & { shareTitle: string }>
emailEnabled: boolean
message?: { kind: 'ok' | 'error', text: string }
}
export function AdminDashboard ({ shares, recipients, comments, emailEnabled, message }: AdminDashboardProps) {
return (
<Layout title="Admin">
<nav>
<ul><li><strong>Admin</strong></li></ul>
<ul>
<li>
<form method="post" action="/admin/notify" style="margin:0">
<button type="submit" class="secondary outline" style="margin:0">Check for new photos now</button>
</form>
</li>
<li>
<form method="post" action="/admin/logout" style="margin:0">
<button type="submit" class="secondary outline" style="margin:0">Sign out</button>
</form>
</li>
</ul>
</nav>
{message && <Notice kind={message.kind}>{message.text}</Notice>}
{!emailEnabled && <Notice kind="error">Email is not configured (SMTP_URL or SMTP_HOST, plus PUBLIC_BASE_URL), so invitations and notifications can't be sent. You can still copy links by hand.</Notice>}
<section>
<h2>Albums</h2>
<p class="muted">Add an Immich shared link here, then give recipients access to it. Only recipients can open it through this proxy.</p>
{shares.length > 0 && (
<table>
<thead><tr><th>Album</th><th>Recipients</th><th>Photos tracked</th><th></th></tr></thead>
<tbody>
{shares.map(({ share, recipients: count, assets }) => (
<tr key={share.key}>
<td>{share.title || 'Untitled'}<br/><small class="muted">{share.slug ? 's/' + share.slug : share.key.slice(0, 12) + '…'}{share.password ? ' · password stored' : ''}</small></td>
<td>{count}</td>
<td>{assets}</td>
<td>
<form method="post" action={'/admin/shares/' + encodeURIComponent(share.key) + '/delete'}
data-confirm='Remove this album? Recipients lose access and its comments are deleted.'>
<button type="submit" class="secondary outline" style="margin:0;padding:.3rem .6rem;font-size:.8rem">Remove</button>
</form>
</td>
</tr>
))}
</tbody>
</table>
)}
<details open={!shares.length}>
<summary role="button" class="outline">Add an album</summary>
<form method="post" action="/admin/shares">
<label>
Immich share link or key
<input name="link" required placeholder="https://immich.example.com/share/AbC123… or /s/my-album"/>
</label>
<label>
Share password <small class="muted">(only if the Immich link has one; stored so recipients aren't asked for it)</small>
<input type="password" name="password" autocomplete="off"/>
</label>
<button type="submit">Add album</button>
</form>
</details>
</section>
<section>
<h2>Recipients</h2>
{recipients.length > 0 && (
<table>
<thead><tr><th>Name</th><th>Albums</th><th>Emails</th><th>Signed-in browsers</th></tr></thead>
<tbody>
{recipients.map(({ recipient, shares: granted, sessions }) => (
<tr key={recipient.id}>
<td><a href={'/admin/recipients/' + recipient.id}>{recipient.name}</a>{recipient.disabled && <> <small>(disabled)</small></>}<br/><small class="muted">{recipient.email}</small></td>
<td>{granted.map(s => s.title).join(', ') || <span class="muted">none</span>}</td>
<td>{FREQUENCY_LABELS[recipient.frequency]}</td>
<td>{sessions}</td>
</tr>
))}
</tbody>
</table>
)}
{shares.length
? (
<details open={!recipients.length}>
<summary role="button" class="outline">Add a recipient</summary>
<form method="post" action="/admin/recipients">
<div class="grid">
<label>Name<input name="name" required/></label>
<label>Email<input type="email" name="email" required/></label>
</div>
<ShareChecks shares={shares.map(row => row.share)} selected={[]}/>
<FrequencySelect value="weekly"/>
<label><input type="checkbox" name="sendInvite" value="1" checked={emailEnabled} disabled={!emailEnabled}/> Email them their invitation link now</label>
<button type="submit">Add recipient</button>
</form>
</details>
)
: <p class="muted">Add an album first.</p>}
</section>
<section>
<h2>Recent comments</h2>
{comments.length
? (
<table>
<tbody>
{comments.map(comment => (
<tr key={comment.id}>
<td style="width:72px"><img src={'/admin/thumb/' + encodeURIComponent(comment.shareKey) + '/' + comment.assetId} alt="" width={64} height={64} style="object-fit:cover;border-radius:4px" loading="lazy"/></td>
<td><strong>{comment.authorName}</strong> on {comment.shareTitle}<br/><small class="muted">{new Date(comment.createdAt).toLocaleString('en-GB')}</small></td>
<td style="white-space:pre-wrap">{comment.body}</td>
<td>
<form method="post" action={'/admin/comments/' + comment.id + '/delete'}>
<button type="submit" class="secondary outline" style="margin:0;padding:.3rem .6rem;font-size:.8rem">Delete</button>
</form>
</td>
</tr>
))}
</tbody>
</table>
)
: <p class="muted">No comments yet.</p>}
</section>
</Layout>
)
}
function ShareChecks ({ shares, selected }: { shares: ShareRecord[], selected: string[] }) {
return (
<fieldset class="checks">
<legend>Albums they can see</legend>
{shares.map(share => (
<label key={share.key}>
<input type="checkbox" name="shares" value={share.key} checked={selected.includes(share.key)}/>
{share.title || 'Untitled'}
</label>
))}
</fieldset>
)
}
function FrequencySelect ({ value }: { value: Frequency }) {
return (
<label>
New-photo emails
<select name="frequency">
{FREQUENCIES.map(frequency => <option key={frequency} value={frequency} selected={value === frequency}>{FREQUENCY_LABELS[frequency]}</option>)}
</select>
</label>
)
}
export interface AdminRecipientProps {
recipient: Recipient
shares: ShareRecord[]
granted: string[]
sessions: number
activeLinks: number
emailEnabled: boolean
/** A link created by this request; shown once, never stored in plain text. */
newLink?: string
message?: { kind: 'ok' | 'error', text: string }
}
export function AdminRecipient ({ recipient, shares, granted, sessions, activeLinks, emailEnabled, newLink, message }: AdminRecipientProps) {
const base = '/admin/recipients/' + recipient.id
return (
<Layout title={recipient.name + ' · Admin'}>
<p><a href="/admin">← Admin</a></p>
<h1>{recipient.name}</h1>
{message && <Notice kind={message.kind}>{message.text}</Notice>}
{newLink && (
<article>
<header><strong>Personal link for {recipient.name}</strong></header>
<p class="link-box">{newLink}</p>
<footer><small class="muted">Shown once. Anyone with this link can see their albums. It doesn't expire; revoke access below to cancel it.</small></footer>
</article>
)}
<form method="post" action={base}>
<div class="grid">
<label>Name<input name="name" required value={recipient.name}/></label>
<label>Email<input type="email" name="email" required value={recipient.email}/></label>
</div>
<ShareChecks shares={shares} selected={granted}/>
<FrequencySelect value={recipient.frequency}/>
<label><input type="checkbox" name="disabled" value="1" checked={recipient.disabled}/> Disabled (blocks all their links and sessions)</label>
<button type="submit">Save</button>
</form>
<h2>Access</h2>
<p>{sessions} signed-in browser{sessions === 1 ? '' : 's'} · {activeLinks} working link{activeLinks === 1 ? '' : 's'}</p>
<div class="row-actions">
<form method="post" action={base + '/link'}>
<button type="submit">Create a new link</button>
</form>
<form method="post" action={base + '/link'}>
<input type="hidden" name="send" value="1"/>
<button type="submit" disabled={!emailEnabled}>Email them a new link</button>
</form>
<form method="post" action={base + '/revoke'} data-confirm='Sign them out everywhere and cancel all their links?'>
<button type="submit" class="secondary">Revoke all access</button>
</form>
<form method="post" action={base + '/delete'} data-confirm='Delete this recipient? Their comments stay, unattributed to an account.'>
<button type="submit" class="secondary outline">Delete recipient</button>
</form>
</div>
</Layout>
)
}