Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache information about review blocks #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
62 changes: 36 additions & 26 deletions src/core/loadingCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,24 @@ const srPageTagsToClause = (tags) => "(or " + tags.map((tag) => `[?srPage :node/
//cards with the flag-tag or the "query"-tag are not permissible
const createQueryForAllPermissibleCards = (settings) => `[
:find (pull ?card [
:block/string
:block/uid
{:block/refs [:node/title]}
{:block/_refs
[:block/uid :block/string
{:block/_children
[:block/uid {:block/refs [:node/title]}]}
{:block/refs [:node/title]}
:block/string
:block/uid
{:block/refs [:node/title]}
{:block/_refs
[:block/uid :block/string
{:block/_children
[:block/uid {:block/refs [:node/title]}]}
{:block/refs [:node/title]}
{:block/page [:block/uid]}]}
{:block/_children ...}
])
:where
:where
${srPageTagsToClause(settings.mainTags)}
[?card :block/refs ?srPage]
(not-join [?card]
[?card :block/refs ?srPage]
(not-join [?card]
[?flagPage :node/title "${settings.flagTag}"]
[?card :block/refs ?flagPage])
(not-join [?card]
(not-join [?card]
[?queryPage :node/title "query"]
[?card :block/refs ?queryPage])
]`;
Expand Down Expand Up @@ -129,25 +129,25 @@ const queryDueCards = async (settings, dateBasis, asyncQueryFunction) => {
};

const getTodayQuery = (settings, todayUid) => `[
:find (pull ?card
[:block/uid
{:block/refs [:node/title]}
{:block/_refs
:find (pull ?card
[:block/uid
{:block/refs [:node/title]}
{:block/_refs
[
{:block/page [:block/uid]}
{:block/_children
{:block/_children
[:block/uid {:block/refs [:node/title]}]}
]}])
]}])
(pull ?review [:block/refs])
:where
:where
${srPageTagsToClause(settings.mainTags)}
[?card :block/refs ?srPage]
[?review :block/refs ?card]
[?reviewPage :node/title "roam/sr/review"]
[?reviewParent :block/refs ?reviewPage]
[?reviewParent :block/children ?review]
[?todayPage :block/uid "${todayUid}"]
[?reviewParent :block/page ?todayPage]
[?card :block/refs ?srPage]
[?review :block/refs ?card]
[?reviewPage :node/title "roam/sr/review"]
[?reviewParent :block/refs ?reviewPage]
[?reviewParent :block/children ?review]
[?todayPage :block/uid "${todayUid}"]
[?reviewParent :block/page ?todayPage]
]`;

const queryTodayReviewedCards = async (settings, asyncQueryFunction) => {
Expand Down Expand Up @@ -289,3 +289,13 @@ export const loadCards = async (hasLimits, settings, asyncQueryFunction, dateBas

return { extraCards: extraCardsResult, cards };
};

export const getReviewBlocks = () => {
return window.roamAlphaAPI.q(`[:find ?puid ?u :where
[?r :block/uid ?u]
[?r :block/refs ?b]
[?b :node/title "roam/sr/review"]
[?parent :block/children ?r]
[?parent :block/uid ?puid]
]`);
}
61 changes: 28 additions & 33 deletions src/core/mainFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,34 @@ export const scheduleCardIn = async (card, interval) => {

var nextRoamDate = getRoamDate(nextDate);

// Create daily note if it doesn't exist yet
await window.roamAlphaAPI.createPage({
page: {
title: nextRoamDate.title,
},
});

await sleep();

// Query for the [[roam/sr/review]] block
var queryReviewBlock = window.roamAlphaAPI.q(
'[:find (pull ?reviewBlock [:block/uid]) :in $ ?dailyNoteUID :where [?reviewBlock :block/refs ?reviewPage] [?reviewPage :node/title "roam/sr/review"] [?dailyNote :block/children ?reviewBlock] [?dailyNote :block/uid ?dailyNoteUID]]',
nextRoamDate.uid
);

// Check if it's there; if not, create it
var topLevelUid;
if (queryReviewBlock.length == 0) {
topLevelUid = createUid();
await window.roamAlphaAPI.createBlock({
location: {
"parent-uid": nextRoamDate.uid,
order: 0,
},
block: {
string: "[[roam/sr/review]]",
uid: topLevelUid,
},
});
await sleep();
} else {
topLevelUid = queryReviewBlock[0][0].uid;
}
var cachedNextRoamDate = roamsr.state.reviewBlocks.find(e => e[0] === nextRoamDate.uid);
var topLevelUid;

if (cachedNextRoamDate) {
topLevelUid = cachedNextRoamDate[1];
} else {
await window.roamAlphaAPI.createPage({
page: {
title: nextRoamDate.title,
},
});
await sleep();

topLevelUid = createUid();
await window.roamAlphaAPI.createBlock({
location: {
"parent-uid": nextRoamDate.uid,
order: 0,
},
block: {
string: "[[roam/sr/review]]",
uid: topLevelUid,
},
});
await sleep();

roamsr.state.reviewBlocks.push([nextRoamDate.uid, topLevelUid]);
}

// Generate the block
var block = {
Expand Down
3 changes: 2 additions & 1 deletion src/core/sessions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { removeSelector, goToUid, sleep } from "./helperFunctions";
import { addKeyListener, removeKeyListener } from "./keybindings";
import { loadCards } from "./loadingCards";
import { getReviewBlocks, loadCards } from "./loadingCards";
import { goToCurrentCard } from "./mainFunctions";
import { setCards, setCurrentCardIndex, setLimitActivation, standbyState } from "./state";
import { setCustomStyle, removeCustomStyle, removeRoamsrMainviewCSS } from "../ui/styles";
Expand Down Expand Up @@ -33,6 +33,7 @@ export const loadState = async (i) => {
setLimitActivation(true);
setCurrentCardIndex(i);
const { cards, extraCards } = await loadCards(roamsr.state.limits, roamsr.settings, window.roamAlphaAPI.q);
roamsr.state.reviewBlocks = getReviewBlocks();
setCards(cards, extraCards);
updateCounters(roamsr.state);
return;
Expand Down