f270812219
Add token library/placements, keep play-time moves for the session, lock presentation interactions, and fix export/import modal layout plus freeform trap label. Co-authored-by: Cursor <cursoragent@cursor.com>
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
import { ipcChannels } from '../../../shared/ipc/contracts';
|
|
import type { TokenId } from '../../../shared/types';
|
|
import { getDndApi } from '../dndApi';
|
|
|
|
const cache = new Map<string, string | null>();
|
|
|
|
export function useTokenImageUrl(tokenId: TokenId | null | undefined): string | null {
|
|
const api = getDndApi();
|
|
const [url, setUrl] = useState<string | null>(() =>
|
|
tokenId ? (cache.get(tokenId) ?? null) : null,
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!tokenId) {
|
|
setUrl(null);
|
|
return;
|
|
}
|
|
const cached = cache.get(tokenId);
|
|
if (cached !== undefined) {
|
|
setUrl(cached);
|
|
return;
|
|
}
|
|
let cancelled = false;
|
|
void api.invoke(ipcChannels.tokens.imageUrl, { id: tokenId }).then(({ url: next }) => {
|
|
cache.set(tokenId, next);
|
|
if (!cancelled) setUrl(next);
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [api, tokenId]);
|
|
|
|
useEffect(() => {
|
|
return api.on(ipcChannels.tokens.stateChanged, ({ tokens }) => {
|
|
for (const t of tokens) {
|
|
cache.delete(t.id);
|
|
}
|
|
if (tokenId) {
|
|
void api.invoke(ipcChannels.tokens.imageUrl, { id: tokenId }).then(({ url: next }) => {
|
|
cache.set(tokenId, next);
|
|
setUrl(next);
|
|
});
|
|
}
|
|
});
|
|
}, [api, tokenId]);
|
|
|
|
return url;
|
|
}
|