feat(tokens): app-local non-player tokens with session moves and UI polish
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>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user