Skip to content

Commit

Permalink
chore: better retry and better example
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Dec 18, 2024
1 parent 49038ea commit 4b90dd2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
32 changes: 21 additions & 11 deletions EXAMPLE_VAULT/.obsidian/plugins/modal-form/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,16 @@
"type": "textarea",
"hidden": false
}
},
{
"name": "dateOfBirth",
"label": "",
"description": "",
"isRequired": false,
"input": {
"type": "date",
"hidden": false
}
}
],
"version": "1",
Expand Down Expand Up @@ -499,35 +509,35 @@
},
{
"_tag": "text",
"value": ");\nconst birthYear = now.year() - age;\n_%>\n\n# "
"value": ");\nconst birthYear = now.year() - age;\nconst dateOfBirth = moment("
},
{
"_tag": "variable",
"value": "name"
"value": "dateOfBirth"
},
{
"_tag": "text",
"value": "'s Profile\n> Created on <% tp.date.now(\"dddd, MMMM Do YYYY\") %> at <% tp.date.now(\"HH:mm\") %>\n\n## Basic Information\n- **Age**: "
"value": ")\n_%>\n\n# "
},
{
"_tag": "variable",
"value": "age"
"value": "name"
},
{
"_tag": "text",
"value": " years old *(born around <%* tR + birthYear %>)*\n- **Date of Birth**: "
"value": "'s Profile\n> Created on <% tp.date.now(\"dddd, MMMM Do YYYY\") %> at <% tp.date.now(\"HH:mm\") %>\n\n## Basic Information\n- **Age**: "
},
{
"_tag": "variable",
"value": "dateOfBirth"
"value": "age"
},
{
"_tag": "text",
"value": "\n- **Best Time to Contact**: "
"value": " years old *(born around <%* tR += birthYear %>)*\n- **Date of Birth**: "
},
{
"_tag": "variable",
"value": "timeOfDay"
"value": "dateOfBirth"
},
{
"_tag": "text",
Expand All @@ -539,23 +549,23 @@
},
{
"_tag": "text",
"value": "\n- **Days until next birthday**: <%* \nif (tp.frontmatter.dateOfBirth) {\n const birthday = moment(tp.frontmatter.dateOfBirth);\n const nextBirthday = moment(birthday).year(now.year());\n if (nextBirthday.isBefore(now)) {\n nextBirthday.add(1, 'year');\n }\n tR + nextBirthday.diff(now, 'days');\n} else {\n tR + \"Unknown\";\n}\n\nconsole.log({ age, birthYear, frontmatter: tp.frontmatter })\n_%> days\n\n## Preferences\n- **Favorite Book**: [["
"value": "\n- **Days until next birthday**: <%* \nif (dateOfBirth) {\n const nextBirthday = dateOfBirth.year(now.year());\n if (nextBirthday.isBefore(now)) {\n nextBirthday.add(1, 'year');\n }\n tR += nextBirthday.diff(now, 'days');\n} else {\n tR += \"Unknown\";\n}\n\nconsole.log({ age, birthYear, frontmatter: tp.frontmatter, dateOfBirth })\n_%> days\n\n## Preferences\n- **Favorite Book**: [["
},
{
"_tag": "variable",
"value": "favorite_book"
},
{
"_tag": "text",
"value": "]]\n<%* if (tp.frontmatter.favorite_book) { %>\n> [!note] Related Books\n> \\`\\`\\`dataview\n> LIST\n> FROM [["
"value": "]]\n<%* if (tp.frontmatter.favorite_book) { %>\n> [!note] Related Books\n> ```dataview\n> LIST\n> FROM #book\n> WHERE contains(file.outlinks, [["
},
{
"_tag": "variable",
"value": "favorite_book"
},
{
"_tag": "text",
"value": "]]-links\n> SORT file.name ASC\n> \\`\\`\\`\n<%* } %>\n\n## Additional Information\n"
"value": "]])\n> SORT file.name ASC\n> ```\n<%* } %>\n\n## Additional Information\n"
},
{
"_tag": "variable",
Expand Down
65 changes: 35 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,37 +351,42 @@ export default class ModalFormPlugin extends Plugin {
noteContent: string,
destinationFolder: string,
): TE.TaskEither<Error, void> {
// Use template service instead of directly creating the file
const loop = (noteContent: string): TE.TaskEither<Error, void> => {
// Use template service instead of directly creating the file
return pipe(
this.templateService.createNoteFromTemplate(
noteContent,
destinationFolder,
noteName,
false, // don't open the new note
),
TE.orElse((error) => {
logger.error(error);
return pipe(
TE.tryCatch(
() =>
this.api.openForm(retryForm, {
values: {
title: error.message,
template: noteContent,
},
}),
E.toError,
),
TE.map((result) => result.get("template")),
TE.chain((template) => {
if (typeof template !== "string") {
notifyWarning("Failed while retrying")("Template is not a string");
return TE.left(new Error("Template is not a string"));
}
return loop(template);
}),
);
}),
);
};
return pipe(
this.templateService.createNoteFromTemplate(
noteContent,
destinationFolder,
noteName,
false, // don't open the new note
),
TE.orElse((error) => {
logger.error(error);
return pipe(
TE.tryCatch(
() =>
this.api.openForm(retryForm, {
values: {
title: error.message,
template: noteContent,
},
}),
E.toError,
),
TE.map((result) => result.get("template")),
TE.chain((template) => {
if (typeof template !== "string") {
notifyWarning("Failed while retrying")("Template is not a string");
return TE.left(new Error("Template is not a string"));
}
return this.createNoteFromTemplate(noteName, template, destinationFolder);
}),
);
}),
loop(noteContent),
TE.tapIO(() => () => {
log_notice(
"Note created successfully",
Expand Down

0 comments on commit 4b90dd2

Please sign in to comment.