Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 29 additions & 96 deletions apps/roam/src/components/DiscourseContextOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,104 +22,41 @@ type DiscourseData = {
};

const cache: {
[title: string]: DiscourseData;
[tag: string]: DiscourseData;
} = {};
const overlayQueue: {
tag: string;
callback: () => Promise<void>;
start: number;
queued: number;
end: number;
mid: number;
id: string;
}[] = [];

const getOverlayInfo = (tag: string, id: string): Promise<DiscourseData> => {
if (cache[tag]) return Promise.resolve(cache[tag]);
const relations = getDiscourseRelations();
const nodes = getDiscourseNodes(relations);
const getOverlayInfo = async (tag: string): Promise<DiscourseData> => {
try {
if (cache[tag]) return cache[tag];

const relations = getDiscourseRelations();
const nodes = getDiscourseNodes(relations);

return new Promise((resolve) => {
const triggerNow = overlayQueue.length === 0;
overlayQueue.push({
id,
start: 0,
end: 0,
mid: 0,
queued: new Date().valueOf(),
async callback() {
const self = this;
const start = (self.start = new Date().valueOf());
// @ts-ignore
const queryResult = await window.roamAlphaAPI.data.backend.q(
`[:find ?a :where [?b :node/title "${normalizePageTitle(
tag,
)}"] [?a :block/refs ?b]]`,
);
const refs = queryResult.length;
return getDiscourseContextResults({
uid: getPageUidByPageTitle(tag),
nodes,
relations,
}).then(function resultCallback(results) {
self.mid = new Date().valueOf();
const output = (cache[tag] = {
results,
const [results, refs] = await Promise.all([
getDiscourseContextResults({
uid: getPageUidByPageTitle(tag),
nodes,
relations,
}),
// @ts-ignore - backend to be added to roamjs-components
window.roamAlphaAPI.data.backend.q(
`[:find ?a :where [?b :node/title "${normalizePageTitle(tag)}"] [?a :block/refs ?b]]`,
),
]);

refs,
});
const runTime = (self.end = new Date().valueOf() - start);
setTimeout(() => {
overlayQueue.splice(0, 1);
if (overlayQueue.length) {
overlayQueue[0].callback();
}
}, runTime * 4);
resolve(output);
});
},
tag,
return (cache[tag] = {
results,
refs: refs.length,
});
if (triggerNow) overlayQueue[0].callback?.();
});
} catch (error) {
console.error(`Error getting overlay info for ${tag}:`, error);
return {
results: [],
refs: 0,
};
}
};

// const experimentalGetOverlayInfo = (title: string) =>
// Promise.all([
// getDiscourseContextResults({ uid: getPageUidByPageTitle(title) }),
// fireWorkerQuery({
// where: [
// {
// type: "data-pattern",
// arguments: [
// { type: "variable", value: "b" },
// { type: "constant", value: ":node/title" },
// { type: "constant", value: `"${title}"` },
// ],
// },
// {
// type: "data-pattern",
// arguments: [
// { type: "variable", value: "a" },
// { type: "constant", value: ":block/refs" },
// { type: "variable", value: `b` },
// ],
// },
// ],
// pull: [],
// }),
// ]).then(([results, allrefs]) => ({ results, refs: allrefs.length }));

export const refreshUi: { [k: string]: () => void } = {};
const refreshAllUi = () =>
Object.entries(refreshUi).forEach(([k, v]) => {
if (document.getElementById(k)) {
v();
} else {
delete refreshUi[k];
}
});

const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => {
const tagUid = useMemo(() => getPageUidByPageTitle(tag), [tag]);
const [loading, setLoading] = useState(true);
Expand All @@ -128,10 +65,7 @@ const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => {
const [score, setScore] = useState<number | string>(0);
const getInfo = useCallback(
() =>
// localStorageGet("experimental") === "true"
// ? experimentalGetOverlayInfo(tag)
// :
getOverlayInfo(tag, id)
getOverlayInfo(tag)
.then(({ refs, results }) => {
const discourseNode = findDiscourseNode(tagUid);
if (discourseNode) {
Expand All @@ -158,7 +92,6 @@ const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => {
getInfo();
}, [getInfo, setLoading]);
useEffect(() => {
refreshUi[id] = refresh;
getInfo();
}, [refresh, getInfo]);
return (
Expand Down