Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 12x 13x 13x 13x 13x 13x 13x 13x 13x 13x 11x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 13x 13x 13x 4x 9x 1x | import './BlobImage.css';
import React from 'react';
import {assetReference, type IAssetOptions} from '../../blob/assetReference.js';
import {useBlong} from '../../context/BlongContext.js';
import {deriveInitials} from '../../lib/initials.js';
/**
* `BlobImage` — a stored picture, or the name it stands for.
*
* A record holds a `blob://` reference or a URL; a person who has not chosen a photograph holds
* nothing. So this renders one of three things: the picture, its thumbnail when the store derived one,
* or the initials of a name in the same box — which is what the account menu already does for an avatar
* (@@D-842).
*
* The thumbnail is asked for by reading the record: a variant is a blob of its own with its own key, and
* only the record says which keys exist (@@D-844). One call, then the bytes are the browser's business —
* the cookie the serving route reads is already set, so the `img` needs no header of its own.
*/
export interface IBlobImageProps {
/** The stored value: `blob://<namespace>/<key>`, a URL, or empty. */
value?: string | null;
/** What the initials fall back to, when there is no picture to show. */
name?: string;
/** The box, in pixels. A number is square; `undefined` fills the parent. */
size?: number;
/** A circle (an avatar) or a rounded square (a card). */
shape?: 'circle' | 'square';
/** How the picture fills the box. */
fit?: 'cover' | 'contain';
/** The alternative text; defaults to the name, then to an empty string (a decorative picture). */
alt?: string;
className?: string;
/** Ask the record for its thumbnail before the original; `false` serves the blob as stored. */
thumbnail?: boolean;
/**
* Which stored variant to show, when the record has one — `thumb` by default.
*
* A variant is a blob of its own with its own key, so this is a name the record answers with and
* not a size the browser asks for (`@@D-844`). An unknown name falls back to the original, which
* is what makes a page that asks for `crop` work against a photograph stored without one.
*/
variant?: string;
/** The method that answers a record; a deployment may point this at its own. */
infoMethod?: string;
/** Where the serving route is mounted. */
basePath?: IAssetOptions['basePath'];
/** A test id, so a page test can find the element it rendered. */
testId?: string;
}
/** The runtime handler proxy — any method key, called with (params, $meta). */
type HandlerCall = (params: object, meta: object) => Promise<unknown>;
/** What reading a record answers, narrowed to what this component decides with. */
interface IRecordAnswer {
variants?: Record<string, {key?: string} | undefined>;
}
const DEFAULT_INFO_METHOD = 'blob.blob.get';
/** The size of the box, as CSS says it. */
function boxSize(size: number | undefined): React.CSSProperties {
return size === undefined ? {} : {width: size, height: size};
}
export function BlobImage({
value,
name,
size,
shape = 'circle',
fit = 'cover',
alt,
className,
thumbnail = true,
variant = 'thumb',
infoMethod = DEFAULT_INFO_METHOD,
basePath,
testId,
}: IBlobImageProps) {
const {handler} = useBlong();
const reference = React.useMemo(() => assetReference(value, {basePath}), [value, basePath]);
const base = reference?.src ?? null;
// The thumbnail is remembered against the value it was derived for, so a record that changes does not
// keep showing the picture of the one before it — and no state is set from an effect, which would
// render twice for every new value.
const [derived, setDerived] = React.useState<{for: string; src: string} | null>(null);
const [brokenFor, setBrokenFor] = React.useState<string | null>(null);
const src = derived && derived.for === base ? derived.src : base;
const broken = brokenFor !== null && brokenFor === src;
// The thumbnail, when the record says there is one. A blob reference is the only case that has a
// record to ask; a pasted URL is served as it is.
React.useEffect(() => {
if (!thumbnail || !reference?.namespace || !reference.key) return;
let cancelled = false;
const call = (handler as unknown as Record<string, HandlerCall | undefined>)[infoMethod];
Iif (typeof call !== 'function') return;
void (async () => {
try {
const record = (await call(
{namespace: reference.namespace, key: reference.key},
{},
)) as IRecordAnswer | null;
const chosen = record?.variants?.[variant]?.key;
if (cancelled || !chosen) return;
const thumb = assetReference(`blob://${reference.namespace}/${chosen}`, {
basePath,
});
Eif (thumb && base) setDerived({for: base, src: thumb.src});
} catch {
// The record is unreadable: the original still serves, so nothing to do.
}
})();
return () => {
cancelled = true;
};
}, [
thumbnail,
variant,
base,
reference?.namespace,
reference?.key,
handler,
infoMethod,
basePath,
]);
const initials = React.useMemo(() => deriveInitials(name), [name]);
const classes = ['blong-blob-image', `blong-blob-image--${shape}`, className]
.filter(Boolean)
.join(' ');
if (!src || broken) {
return (
<span
className={`${classes} blong-blob-image--empty`}
style={boxSize(size)}
data-testid={testId}
aria-hidden={initials ? undefined : true}
>
{initials ? (
<span className="blong-blob-image__initials">{initials}</span>
) : (
<i
className="pi pi-user"
aria-hidden
/>
)}
</span>
);
}
return (
<img
className={classes}
style={{...boxSize(size), objectFit: fit}}
src={src}
alt={alt ?? name ?? ''}
data-testid={testId}
onError={() => setBrokenFor(src)}
/>
);
}
|