Skip to content

Commit

Permalink
add {current user} as target option to multiple condition relationshi…
Browse files Browse the repository at this point in the history
…ps (#266)
  • Loading branch information
mdroidian committed May 30, 2024
1 parent e84bedd commit ec221ef
Showing 1 changed file with 46 additions and 25 deletions.
71 changes: 46 additions & 25 deletions src/utils/conditionToDatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import getCurrentPageUid from "roamjs-components/dom/getCurrentPageUid";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
import getPageTitlesStartingWithPrefix from "roamjs-components/queries/getPageTitlesStartingWithPrefix";
import extractRef from "roamjs-components/util/extractRef";
import getCurrentUserDisplayName from "roamjs-components/queries/getCurrentUserDisplayName";

type ConditionToDatalog = (condition: Condition) => DatalogClause[];

Expand Down Expand Up @@ -107,6 +108,19 @@ const getTitleDatalog = ({
},
];
}
const currentUserMatch = /^\s*{current user}\s*$/i.test(target);
if (currentUserMatch) {
return [
{
type: "data-pattern",
arguments: [
{ type: "variable", value: source },
{ type: "constant", value: ":node/title" },
{ type: "constant", value: `"${getCurrentUserDisplayName()}"` },
],
},
];
}
if (isRegex(target)) {
const rePattern = regexRePatternValue(target);
return [
Expand Down Expand Up @@ -240,7 +254,12 @@ const translator: Record<string, Translator> = {
],
placeholder: "Enter any placeholder for the node",
targetOptions: () =>
getAllPageNames().concat(["{date}", "{date:today}", "{current}"]),
getAllPageNames().concat([
"{date}",
"{date:today}",
"{current}",
"{current user}",
]),
},
"is in page": {
callback: ({ source, target }) => [
Expand All @@ -259,7 +278,12 @@ const translator: Record<string, Translator> = {
"has title": {
callback: getTitleDatalog,
targetOptions: () =>
getAllPageNames().concat(["{date}", "{date:today}", "{current}"]),
getAllPageNames().concat([
"{date}",
"{date:today}",
"{current}",
"{current user}",
]),
placeholder: "Enter a page name or {date} for any DNP",
},
"with text in title": {
Expand Down Expand Up @@ -517,25 +541,17 @@ const translator: Record<string, Translator> = {
]
: [
...initialDatalog,
{
type: "data-pattern",
arguments: [
{ type: "variable", value: `${source}-User-Display` },
{ type: "constant", value: ":node/title" },
{
type: "constant",
value: `"${normalizePageTitle(target)}"`,
},
],
},
...getTitleDatalog({ source: `${source}-User-Display`, target }),
];
},
targetOptions: () =>
(
window.roamAlphaAPI.data.fast.q(
`[:find (pull ?n [:node/title]) :where [?u :user/display-page ?n]]`
) as [PullBlock][]
).map((d) => d[0][":node/title"] || ""),
)
.map((d) => d[0][":node/title"] || "")
.concat(["{current user}"]),
placeholder: "Enter the display name of any user with access to this graph",
},
"edited by": {
Expand All @@ -556,21 +572,16 @@ const translator: Record<string, Translator> = {
{ type: "variable", value: `${source}-User-Display` },
],
},
{
type: "data-pattern",
arguments: [
{ type: "variable", value: `${source}-User-Display` },
{ type: "constant", value: ":node/title" },
{ type: "constant", value: `"${normalizePageTitle(target)}"` },
],
},
...getTitleDatalog({ source: `${source}-User-Display`, target }),
],
targetOptions: () =>
(
window.roamAlphaAPI.data.fast.q(
`[:find (pull ?n [:node/title]) :where [?u :user/display-page ?n]]`
) as [PullBlock][]
).map((d) => d[0][":node/title"] || ""),
)
.map((d) => d[0][":node/title"] || "")
.concat(["{current user}"]),
placeholder: "Enter the display name of any user with access to this graph",
},
"references title": {
Expand All @@ -586,7 +597,12 @@ const translator: Record<string, Translator> = {
...getTitleDatalog({ source: `${target}-Ref`, target }),
],
targetOptions: () =>
getAllPageNames().concat(["{date}", "{date:today}", "{current}"]),
getAllPageNames().concat([
"{date}",
"{date:today}",
"{current}",
"{current user}",
]),
placeholder: "Enter a page name or {date} for any DNP",
},
"has heading": {
Expand Down Expand Up @@ -616,7 +632,12 @@ const translator: Record<string, Translator> = {
...getTitleDatalog({ source: target, target }),
],
targetOptions: () =>
getAllPageNames().concat(["{date}", "{date:today}", "{current}"]),
getAllPageNames().concat([
"{date}",
"{date:today}",
"{current}",
"{current user}",
]),
placeholder: "Enter a page name or {date} for any DNP",
},
"created after": {
Expand Down

0 comments on commit ec221ef

Please sign in to comment.