RoamJS / roamjs-components

MIT License
15 stars 5 forks source link

Build Error #8

Closed mdroidian closed 1 year ago

mdroidian commented 1 year ago

@dvargas92495 Build Error. Issue with PullBlock / ActionParams type mismatch.

Error in mockRoamEnvironment.ts

createBlock: async (action) => {
      if (!action.block) throw new Error(`block field is required`);
      if (!action.location) throw new Error(`location field is required`);
      const parent = graph.uids[action.location["parent-uid"]];
      if (!parent)
        throw new Error(
          `Could not find parent by uid: ${action.location["parent-uid"]}`
        );
      const parentBlock = graph.state[parent];
      const id = getDbId();
      const block = {
        ":block/string": action.block.string,
        ":block/uid":
          action.block.uid || window.roamAlphaAPI.util.generateUID(),
        ":block/parents": ([{ ":db/id": parent }] as PullBlock[]).concat(
          parentBlock[":block/parents"] || []
        ),
        ":db/id": id,
        ":block/order": action.location.order,
      };
      graph.uids[block[":block/uid"]] = id;
      parentBlock[":block/children"] = (
        parentBlock[":block/children"] || []
      ).concat({ ":db/id": id });
      graph.state[id] = block;  // ERROR HERE - Types of property '":block/order"' are incompatible.
    },

I tried just adding "last" to Pullblock, but that gave a bunch of other errors, for instance in getBasicTreeByParentUid

const sortBasicNodes = (c: PullBlock[]): RoamBasicNode[] =>
  c
    .sort((a, b) => (a[":block/order"] || 0) - (b[":block/order"] || 0))
    .map((node) => ({
      children: sortBasicNodes(node[":block/children"] || []),
      uid: node[":block/uid"] || "",
      text: node[":block/string"] || "",
    }));

Which makes sense. I image PullBlock doesn't return "last".

What is the relationship between ActionParams and PullBlock? Do we create a separate CreateBlock type?

dvargas92495 commented 1 year ago

mockRoamEnvironment isn't used much - I'd just ts-ignore it. you probably also could just do, which is prob more correct:

":block/order": action.location.order === "last" ? (parentBlock[":block/children"] || []).length : action.location.order

ActionParams is meant to be the "separate" Create Block request type. PullBlock is what is usually returned from queries

mdroidian commented 1 year ago

I'll go with @ts-ignore

I tested pull and pullwatch and only got numbers returned. Probably good to keep it that way.