Skip to content

Commit 1cc312d

Browse files
authored
Merge pull request #8 from DevelopmentFinanceObservatory/release/website
Sync dfo-website@ef36749088f9
2 parents 0afd2e2 + 99f1824 commit 1cc312d

12 files changed

Lines changed: 239 additions & 34 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"chart.js": "^4.5.1",
3333
"classnames": "^2.5.1",
3434
"drizzle-orm": "^0.45.2",
35+
"html-to-image": "^1.11.13",
3536
"ioredis": "^6",
3637
"lowlight": "^3.3.0",
3738
"mermaid": "^12.1.0",

‎pnpm-lock.yaml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/components/organisms/CreateCanvas/ShowcaseCards.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const extras = (title: string, view: CardExtras['view']): CardExtras => ({
5353
onRemove: NOOP,
5454
onAsk: NOOP,
5555
onDownload: null,
56+
onDownloadImage: null,
5657
onEdit: NOOP,
5758
editLabel: '',
5859
// Set (to nothing) so the chrome shows the attribution line, not the tag.

‎src/components/organisms/DataSearch/DataSearch.module.scss‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,19 @@ $table-head-cover: rem(48);
354354

355355
// One warning a reader needs, in body colour; a degraded one reads as a
356356
// problem.
357+
// The one-time nudge toward the sentence's editable values (SearchHint),
358+
// between the sentence and the actions under it. Quieter than a note line:
359+
// it is an aside, and it leaves for good the moment a value is tapped.
360+
.hintLine {
361+
@include font-size(13px, 1.3);
362+
363+
margin-top: rem(14);
364+
font-family: var(--font-dm-mono);
365+
font-weight: 400;
366+
color: var(--color-sage-dark);
367+
letter-spacing: -0.02em;
368+
}
369+
357370
.noteLine {
358371
@include font-size(15px, 1.3);
359372

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use client';
2+
3+
import { useEffect, useState } from 'react';
4+
5+
import styles from './DataSearch.module.scss';
6+
7+
const SEEN_KEY = 'dfo.search.refineHintSeen';
8+
9+
// Whether this browser has already been told. A functional preference — it
10+
// remembers that a hint was read, it does not describe anyone — so unlike
11+
// the usage visitor id (src/lib/events/client) it is not behind a consent
12+
// gate. Storage can be blocked outright, and a reader who cannot be
13+
// remembered is better shown the hint again than shown an error.
14+
const seen = (): boolean => {
15+
try {
16+
return localStorage.getItem(SEEN_KEY) === '1';
17+
} catch {
18+
return false;
19+
}
20+
};
21+
22+
const remember = (): void => {
23+
try {
24+
localStorage.setItem(SEEN_KEY, '1');
25+
} catch {
26+
// Nothing to do: it will be offered again next time.
27+
}
28+
};
29+
30+
/**
31+
* The one-time nudge toward the sentence above: its values are editable, and
32+
* nothing but an underline says so.
33+
*
34+
* Doing the thing is the dismissal — there is no close button, because a
35+
* hint that has been acted on has nothing left to say. It watches for a tap
36+
* on a slot token anywhere on the page rather than being told about one,
37+
* which keeps the heading and the sentence body free of a flag that only
38+
* concerns this line. The tokens carry `styles.slot` from the stylesheet
39+
* this file imports, so the selector is the same hashed class they render
40+
* with.
41+
*
42+
* The seen flag is read straight into state rather than in an effect: this
43+
* sits under a resolved answer, so the subtree only ever exists on the
44+
* client and there is no server render to disagree with.
45+
*/
46+
const SearchHint = () => {
47+
const [hidden, setHidden] = useState(seen);
48+
49+
useEffect(() => {
50+
if (hidden) {
51+
return;
52+
}
53+
54+
// Capture, so it still counts when the token's own menu stops the
55+
// click from travelling any further.
56+
const onClick = (event: MouseEvent) => {
57+
if ((event.target as Element | null)?.closest?.(`.${styles.slot}`)) {
58+
remember();
59+
setHidden(true);
60+
}
61+
};
62+
63+
document.addEventListener('click', onClick, true);
64+
65+
return () => document.removeEventListener('click', onClick, true);
66+
}, [hidden]);
67+
68+
if (hidden) {
69+
return null;
70+
}
71+
72+
return <p className={styles.hintLine}>Tap the underlined values to refine your results</p>;
73+
};
74+
75+
export default SearchHint;

‎src/components/organisms/DataSearch/SearchSentence.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
useToggleSelection,
3232
} from './SlotMenu';
3333
import { GraphInfo, MeasureInfo, SourceInfo } from './GraphInfo';
34+
import SearchHint from './SearchHint';
3435
import { TokenAbout } from './TokenAbout';
3536
import { UnitPicker } from './UnitPicker';
3637
import WhyPanel from './WhyPanel';
@@ -2378,6 +2379,13 @@ export const SearchSentenceBody = ({
23782379
</p>
23792380
))}
23802381

2382+
{/* Only where there is something to tap. NOT `refine`: that list
2383+
is the bindings no visible token states, so a sentence whose
2384+
values are all on show — the very case the hint is for —
2385+
leaves it empty. A resolved part is what puts the editable
2386+
tokens in the heading. */}
2387+
{definiteParts.length > 0 && <SearchHint />}
2388+
23812389
<div className={styles.actions}>
23822390
<RefineMenu
23832391
items={refine}

‎src/components/organisms/EmbedView/EmbedView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const SingleCard = ({
5555
onAsk: noop,
5656
onInfo: isText ? null : () => onCardInfo(widget.id),
5757
onDownload: null,
58+
onDownloadImage: null,
5859
onEdit: noop,
5960
editLabel: isText ? 'Edit Card' : 'Edit Title',
6061
note: isText ? undefined : saved.notes?.[widget.id],

‎src/components/organisms/Studio/canvas/Whiteboard.tsx‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ import { useIsPhone } from '@/hooks/useMediaQuery';
3333
import type { CardLayout } from '@/lib/chat/history';
3434
import type { Widget } from '@/lib/chat/widgets';
3535
import { cardRefOf } from '@/lib/studio/cardDownload';
36+
import { cardImageName, cardNode, downloadCardImage } from '@/lib/studio/cardImage';
3637
import { cardLiveOf, type LiveCanvas } from '@/lib/studio/liveCards';
3738
import { type ThemeDerived, themeVars } from '@/lib/studio/theme';
3839

39-
// A card's note waits for the pointer to settle — crossing a sheet of cards
40-
// should not trail popups behind it — and then outlives the pointer leaving
41-
// by enough to cross the few px down to it, where its remove button is
42-
// (CardNote).
43-
const NOTE_INTENT_MS = 500;
40+
// A card's note outlives the pointer leaving by enough to cross the few px
41+
// down to it, where its remove button is (CardNote).
4442
const NOTE_GRACE_MS = 150;
43+
// And it stays down this long after a card is dropped, then comes back by
44+
// itself. The drag ends under the pointer: nothing leaves, nothing
45+
// re-enters, and the pointer may not move again at all, so the note has to
46+
// be put back rather than wait to be asked for.
47+
const NOTE_DROP_MS = 400;
4548

4649
export type WhiteboardProps = {
4750
widgets: Widget[];
@@ -152,29 +155,23 @@ const Whiteboard = ({
152155
});
153156
const widgetIds = useMemo(() => new Set(widgets.map((widget) => widget.id)), [widgets]);
154157
// Which card the pointer is resting on — the whole card, not a tag in it,
155-
// is what brings a user's note up (see CardNote). Leaving one is held for
156-
// a moment: the note hangs a few px below the card, and the pointer has
157-
// to cross that gap to reach the remove button inside it.
158+
// is what brings a user's note up (see CardNote), and it comes up at
159+
// once. Leaving is what is held for a moment: the note hangs a few px
160+
// below the card, and the pointer has to cross that gap to reach the
161+
// remove button inside it.
158162
const [hoveredId, setHoveredId] = useState<string | null>(null);
159-
const showNote = useRef(0);
160163
const hideNote = useRef(0);
161-
const holdHover = (id: string) => {
162-
// Back on the card whose note is already up: keep it, and do not
163-
// make the reader wait the intent out a second time.
164-
if (hoveredId === id) {
165-
window.clearTimeout(hideNote.current);
164+
const afterDrop = useRef(0);
166165

167-
return;
168-
}
169-
170-
window.clearTimeout(showNote.current);
171-
showNote.current = window.setTimeout(() => setHoveredId(id), NOTE_INTENT_MS);
166+
const holdHover = (id: string) => {
167+
window.clearTimeout(hideNote.current);
168+
setHoveredId(id);
172169
};
173170
const releaseHover = (id: string) => {
174-
window.clearTimeout(showNote.current);
171+
// The pointer left: a note queued by a drop is no longer wanted.
172+
window.clearTimeout(afterDrop.current);
175173

176-
// Left before its note ever came up, or left a different card: the
177-
// pending hide belongs to whichever note is actually showing.
174+
// The pending hide belongs to whichever note is actually showing.
178175
if (hoveredId !== id) {
179176
return;
180177
}
@@ -184,24 +181,36 @@ const Whiteboard = ({
184181
NOTE_GRACE_MS,
185182
);
186183
};
187-
188-
// A dropped card keeps the pointer that was dragging it, so nothing
189-
// leaves and nothing re-enters — the note would simply reappear the
190-
// instant the drag ended. The wait starts over instead.
191-
const restartHover = (id: string) => {
192-
window.clearTimeout(showNote.current);
184+
// Take the note down for the drop, then put it back where it was: the
185+
// card is still under the pointer, and no event is coming to say so.
186+
const settleHover = (id: string) => {
193187
window.clearTimeout(hideNote.current);
188+
window.clearTimeout(afterDrop.current);
194189
setHoveredId(null);
195-
showNote.current = window.setTimeout(() => setHoveredId(id), NOTE_INTENT_MS);
190+
afterDrop.current = window.setTimeout(() => setHoveredId(id), NOTE_DROP_MS);
196191
};
197192

198193
useEffect(
199194
() => () => {
200-
window.clearTimeout(showNote.current);
201195
window.clearTimeout(hideNote.current);
196+
window.clearTimeout(afterDrop.current);
202197
},
203198
[],
204199
);
200+
201+
// Download Image. The card is found in the document rather than held in
202+
// a ref: the menu that asks for it is rendered inside the very card
203+
// being photographed, and the sheet already labels each slot with its
204+
// widget id.
205+
const saveCardImage = (id: string, title: string) => {
206+
const node = cardNode(id);
207+
208+
if (!node) {
209+
return;
210+
}
211+
212+
void downloadCardImage(node, cardImageName(title));
213+
};
205214
const dragging = useCardDrag({ boardRef, CELL, f, slots, setSlots, raise, widgetIds });
206215
const pan = usePan(boardRef);
207216
const { canvasRef: fxCanvasRef, spawnWave } = useShockwave({
@@ -425,6 +434,11 @@ const Whiteboard = ({
425434
onInfo: isText ? null : () => onCardInfo(widget.id),
426435
onDownload:
427436
onDownload && cardRefOf(widget) ? () => onDownload(widget.id) : null,
437+
// Every kind of card photographs the same way, a text
438+
// card included — it is the card's own face, not its
439+
// data, so there is nothing to have or lack.
440+
onDownloadImage:
441+
isLeaving || readOnly ? null : () => saveCardImage(widget.id, title),
428442
onEdit: isText ? () => onEditText(widget.id) : () => onEditTitle(widget.id),
429443
editLabel: isText ? 'Edit Card' : 'Edit Title',
430444
note: isText ? undefined : notes[widget.id],
@@ -461,6 +475,7 @@ const Whiteboard = ({
461475
)}
462476
// The card's own module reads these (lift tint, inert
463477
// when filtered out).
478+
data-card-id={widget.id}
464479
data-dragging={isDragged ? '' : undefined}
465480
data-dimmed={dimmed ? '' : undefined}
466481
style={
@@ -505,14 +520,14 @@ const Whiteboard = ({
505520
onPointerMove: dragging.moveDrag,
506521
onPointerUp: () => {
507522
if (drag && extras.note) {
508-
restartHover(widget.id);
523+
settleHover(widget.id);
509524
}
510525

511526
dragging.endDrag(true)();
512527
},
513528
onPointerCancel: () => {
514529
if (drag && extras.note) {
515-
restartHover(widget.id);
530+
settleHover(widget.id);
516531
}
517532

518533
dragging.endDrag(false)();

‎src/components/organisms/Studio/canvas/cards/CardActions.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const CardMenu = ({
4242
onAsk,
4343
onInfo,
4444
onDownload,
45+
onDownloadImage,
4546
onShare,
4647
onEdit,
4748
editLabel,
@@ -82,6 +83,11 @@ const CardMenu = ({
8283
Download Data
8384
</MenuRow>
8485
)}
86+
{onDownloadImage && (
87+
<MenuRow icon="download" onSelect={onDownloadImage}>
88+
Download Image
89+
</MenuRow>
90+
)}
8591
{onShare && (
8692
<MenuRow icon="share" onSelect={onShare}>
8793
Share
@@ -165,7 +171,9 @@ const MenuRow = ({
165171
// sheet), with the note mark ahead of them when there is a note to mark.
166172
const CardActions = (extras: CardExtras) =>
167173
extras.readOnly ? null : (
168-
<div className={styles.cardActions}>
174+
// Out of the card's own picture (lib/studio/cardImage): the pin and
175+
// the menu belong to the canvas, not to the card's content.
176+
<div className={styles.cardActions} data-no-export="">
169177
{extras.note && <NoteMark />}
170178
<PinButton pinned={extras.pinned} onPin={extras.onPin} />
171179
<CardMenu {...extras} />

‎src/components/organisms/Studio/canvas/cards/types.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export type CardExtras = {
1717
onAsk: () => void;
1818
// null when the card offers nothing to download, and the row is hidden.
1919
onDownload: (() => void) | null;
20+
// Writes the card itself to a PNG. Null on a read-only sheet, where the
21+
// menu is gone anyway, and on a card still fading out.
22+
onDownloadImage: (() => void) | null;
2023
// Opens the edit modal — "Edit Card" (title + body) for text cards,
2124
// "Edit Title" for every other card.
2225
onEdit: () => void;

0 commit comments

Comments
 (0)