[nextgen/cardstack] big improvements

- Implement return types
- page numbers
- better subpage resolve delay
- error handling and system error cards
- code cleanup
This commit is contained in:
bignutty 2025-02-14 02:12:26 +01:00
parent 7e850e5b5d
commit 18bd28591c
6 changed files with 249 additions and 125 deletions

View file

@ -2,6 +2,7 @@ const { createEmbed, page } = require("#utils/embed");
const { acknowledge } = require("#utils/interactions");
const { DynamicCardStack } = require("../../../labscore/cardstack/DynamicCardStack");
const {CARD_STACK_CONSTANTS} = require("#cardstack");
module.exports = {
label: "text",
@ -25,6 +26,9 @@ module.exports = {
createEmbed("default", context, { description: "page 1"}),
createEmbed("default", context, { description: "page 2. this has a conditional button."})
].map((p, index)=>page(p, {}, { key: `t_${index}` })),
pageNumberGenerator: (pg)=>{
return `Test ${pg.index}`
},
interactive: {
always_active_button: {
label: "single sub page",
@ -32,9 +36,12 @@ module.exports = {
visible: true,
disableCache: true,
resolvePage: ()=>{
return [
createEmbed("success", context, "smiley")
].map((p)=>page(p))
return {
type: CARD_STACK_CONSTANTS.RESOLVE_CALLBACK_TYPES.SUBSTACK,
cards: [
createEmbed("success", context, "smiley")
].map((p)=>page(p))
}
}
},
conditional_button: {
@ -46,16 +53,20 @@ module.exports = {
return (page.getState("key") === "t_1")
},
resolvePage: (pg) => {
return [
createEmbed("default", context, { description: "this is a conditional sub page"}),
createEmbed("default", context, { description: "this is a conditional sub page two"})
].map((p)=>page(p));
return {
type: CARD_STACK_CONSTANTS.RESOLVE_CALLBACK_TYPES.SUBSTACK,
cards: [
createEmbed("default", context, { description: "this is a conditional sub page"}),
createEmbed("default", context, { description: "this is a conditional sub page two"})
].map((p)=>page(p))
}
}
},
dynamic_button: {
// Button Label
label: (page) => {
return page.getState("key");
console.log(page.getState("key"))
return page.getState("key") || "test";
},
// Next to pagination or new row
inline: false,
@ -63,67 +74,19 @@ module.exports = {
// Renders the loading state card
renderLoadingState: (page) => {
return createEmbed("default", context, {
description: "-# Subpage Loading :)",
description: "-# replacing papa card",
})
},
resolvePage: async (pg) => {
console.log("resolving page")
return [
createEmbed("default", context, { description: "this is a dynamic sub page " + Math.random()}),
createEmbed("default", context, { description: "this is a dynamic sub page " + Math.random()}),
createEmbed("default", context, { description: "this is a dynamic sub page " + Math.random()}),
createEmbed("default", context, { description: "this is a dynamic sub page " + Math.random()})
].map((p)=>page(p));
return {
type: CARD_STACK_CONSTANTS.RESOLVE_CALLBACK_TYPES.REPLACE_PARENT_CARD,
card: page(createEmbed("default", context, { description: "this is the new over lord " + new Date()}), {}, {
key: Date.now()
})
};
}
},
conditional_button_2: {
// Button Label
label: "Conditional",
// Next to pagination or new row
inline: false,
visible: true,
resolvePage: (pg) => {throw "a"}
},
conditional_button_3: {
// Button Label
label: "Conditional",
// Next to pagination or new row
inline: false,
visible: true,
resolvePage: (pg) => {throw "a"}
},
conditional_button_4: {
// Button Label
label: "Conditional",
// Next to pagination or new row
inline: false,
visible: true,
resolvePage: (pg) => {throw "a"}
},
conditional_button_5: {
// Button Label
label: "Conditional",
// Next to pagination or new row
inline: false,
visible: true,
resolvePage: (pg) => {throw "a"}
},
conditional_button_6: {
// Button Label
label: "Conditional",
// Next to pagination or new row
inline: false,
visible: true,
resolvePage: (pg) => {throw "a"}
},
conditional_button_7: {
// Button Label
label: "Conditional",
// Next to pagination or new row
inline: false,
visible: true,
resolvePage: (pg) => {throw "a"}
},
}
}
})
}catch(e){

View file

@ -1,10 +1,10 @@
const { anime, animeSupplemental} = require('#api');
const { PERMISSION_GROUPS, OMNI_ANIME_FORMAT_TYPES, COLORS_HEX} = require('#constants');
const { createDynamicCardStack } = require("#cardstack");
const { createDynamicCardStack, CARD_STACK_CONSTANTS } = require("#cardstack");
const { hexToDecimalColor } = require("#utils/color");
const { createEmbed, formatPaginationEmbeds, page } = require('#utils/embed');
const { createEmbed, page } = require('#utils/embed');
const { acknowledge } = require('#utils/interactions');
const { smallPill, link, pill, stringwrapPreserveWords, timestamp, TIMESTAMP_FLAGS} = require('#utils/markdown');
const { editOrReply } = require('#utils/message');
@ -106,7 +106,7 @@ module.exports = {
if(!pages.length) return editOrReply(context, createEmbed("warning", context, `No results found.`))
createDynamicCardStack(context, {
cards: formatPaginationEmbeds(pages),
cards: pages,
interactive: {
episodes_button: {
label: "Episodes",
@ -152,7 +152,15 @@ module.exports = {
return page(card)
})
return formatPaginationEmbeds(cards);
return {
type: CARD_STACK_CONSTANTS.RESOLVE_CALLBACK_TYPES.SUBSTACK,
cards: cards.length >= 1 ? cards : [
// This happens if the episode metadata resolver fails.
page(createEmbed("defaultNoFooter", context, {
description: `-# ${pg.getState("name")} **Episodes**\n## Episodes Unavailable\n\nWe're unable to display episode details for this content.`
}))
],
};
}
},
characters_button: {
@ -190,7 +198,10 @@ module.exports = {
return page(card)
})
return formatPaginationEmbeds(cards);
return {
type: CARD_STACK_CONSTANTS.RESOLVE_CALLBACK_TYPES.SUBSTACK,
cards: cards
};
}
},
related_button: {
@ -214,9 +225,12 @@ module.exports = {
let cards = episodes.response.body.relations.map((e) => renderAnimeResultsPage(context, e, false))
return formatPaginationEmbeds(cards);
return {
type: CARD_STACK_CONSTANTS.RESOLVE_CALLBACK_TYPES.SUBSTACK,
cards: cards
};
}
},
}
}
});
}catch(e){