{
  "$schema": "./.schemas/page.schema.json",
  "title": "Linear",
  "icon": "square.stack.3d.up",
  "sources": {
    "linear": {
      "url": "https://api.linear.app",
      "secret": "linear",
      "auth": {
        "type": "header",
        "name": "Authorization"
      },
      "every": "2m",
      "requests": {
        "gql": {
          "path": "/graphql",
          "method": "POST",
          "body": {
            "query": "{ viewer { id teams(first: 50) { pageInfo { hasNextPage } nodes { key name issueEstimationType activeCycle { id number endsAt scopeHistory completedScopeHistory issueCountHistory completedIssueCountHistory } } } } doing: issues(first: 100, filter: { assignee: { isMe: { eq: true } }, state: { type: { eq: \"started\" } } }) { pageInfo { hasNextPage } nodes { identifier title url priority dueDate state { name type position } } } todo: issues(first: 100, filter: { assignee: { isMe: { eq: true } }, state: { type: { in: [\"unstarted\", \"triage\"] } } }) { pageInfo { hasNextPage } nodes { identifier title url priority dueDate state { name type position } } } backlog: issues(first: 100, filter: { assignee: { isMe: { eq: true } }, state: { type: { eq: \"backlog\" } } }) { pageInfo { hasNextPage } nodes { identifier title url priority dueDate state { name type position } } } done: issues(first: 50, orderBy: updatedAt, filter: { assignee: { isMe: { eq: true } }, completedAt: { gt: \"-P7D\" } }) { pageInfo { hasNextPage } nodes { identifier title url completedAt } } notificationsUnreadCount notifications(first: 50) { nodes { title url createdAt readAt snoozedUntilAt } } }"
          }
        }
      },
      "fail": {
        "when": {
          "path": "gql.errors",
          "exists": true
        },
        "message": "gql.errors[0].message"
      },
      "derive": [
        {
          "name": "doing_rows",
          "value": {
            "each": "gql.data.doing.nodes",
            "fields": {
              "title": "title",
              "url": "url",
              "detail": {
                "if": {
                  "path": "state.name",
                  "not_in": [
                    "In Progress",
                    "Todo",
                    "Backlog"
                  ]
                },
                "then": "state.name",
                "else": {
                  "if": {
                    "path": "dueDate",
                    "exists": true
                  },
                  "then": {
                    "if": {
                      "value": {
                        "template": "{dueDate|day}"
                      },
                      "in": [
                        "Today",
                        "Tomorrow",
                        "Yesterday"
                      ]
                    },
                    "then": {
                      "template": "Due {dueDate|day|lower}"
                    },
                    "else": {
                      "template": "Due {dueDate|day}"
                    }
                  },
                  "else": "identifier"
                }
              },
              "icon": {
                "path": "priority",
                "values": {
                  "1": "exclamationmark.square.fill",
                  "2": "chevron.up.2"
                },
                "default": ""
              },
              "color": {
                "path": "priority",
                "values": {
                  "1": "orange"
                },
                "default": ""
              },
              "order": {
                "path": "priority",
                "values": {
                  "1": 1,
                  "2": 2,
                  "3": 3,
                  "4": 4,
                  "0": 5
                },
                "default": 5
              },
              "position": "state.position",
              "key": "identifier"
            }
          }
        },
        {
          "name": "todo_rows",
          "value": {
            "each": "gql.data.todo.nodes",
            "fields": {
              "title": "title",
              "url": "url",
              "detail": {
                "if": {
                  "path": "state.name",
                  "not_in": [
                    "In Progress",
                    "Todo",
                    "Backlog"
                  ]
                },
                "then": "state.name",
                "else": {
                  "if": {
                    "path": "dueDate",
                    "exists": true
                  },
                  "then": {
                    "if": {
                      "value": {
                        "template": "{dueDate|day}"
                      },
                      "in": [
                        "Today",
                        "Tomorrow",
                        "Yesterday"
                      ]
                    },
                    "then": {
                      "template": "Due {dueDate|day|lower}"
                    },
                    "else": {
                      "template": "Due {dueDate|day}"
                    }
                  },
                  "else": "identifier"
                }
              },
              "icon": {
                "path": "priority",
                "values": {
                  "1": "exclamationmark.square.fill",
                  "2": "chevron.up.2"
                },
                "default": ""
              },
              "color": {
                "path": "priority",
                "values": {
                  "1": "orange"
                },
                "default": ""
              },
              "order": {
                "path": "priority",
                "values": {
                  "1": 1,
                  "2": 2,
                  "3": 3,
                  "4": 4,
                  "0": 5
                },
                "default": 5
              },
              "position": "state.position",
              "key": "identifier"
            }
          }
        },
        {
          "name": "backlog_rows",
          "value": {
            "each": "gql.data.backlog.nodes",
            "fields": {
              "title": "title",
              "url": "url",
              "detail": {
                "if": {
                  "path": "state.name",
                  "not_in": [
                    "In Progress",
                    "Todo",
                    "Backlog"
                  ]
                },
                "then": "state.name",
                "else": {
                  "if": {
                    "path": "dueDate",
                    "exists": true
                  },
                  "then": {
                    "if": {
                      "value": {
                        "template": "{dueDate|day}"
                      },
                      "in": [
                        "Today",
                        "Tomorrow",
                        "Yesterday"
                      ]
                    },
                    "then": {
                      "template": "Due {dueDate|day|lower}"
                    },
                    "else": {
                      "template": "Due {dueDate|day}"
                    }
                  },
                  "else": "identifier"
                }
              },
              "icon": {
                "path": "priority",
                "values": {
                  "1": "exclamationmark.square.fill",
                  "2": "chevron.up.2"
                },
                "default": ""
              },
              "color": {
                "path": "priority",
                "values": {
                  "1": "orange"
                },
                "default": ""
              },
              "order": {
                "path": "priority",
                "values": {
                  "1": 1,
                  "2": 2,
                  "3": 3,
                  "4": 4,
                  "0": 5
                },
                "default": 5
              },
              "position": "state.position",
              "key": "identifier"
            }
          }
        },
        {
          "name": "cycle_rows",
          "value": {
            "each": "gql.data.viewer.teams.nodes",
            "where": {
              "path": "activeCycle.id",
              "exists": true
            },
            "fields": {
              "key": "key",
              "number": "activeCycle.number",
              "ends": "activeCycle.endsAt",
              "done": {
                "if": {
                  "path": "issueEstimationType",
                  "equals": "notUsed"
                },
                "then": {
                  "first": "$",
                  "of": "activeCycle.completedIssueCountHistory",
                  "last": 1
                },
                "else": {
                  "first": "$",
                  "of": "activeCycle.completedScopeHistory",
                  "last": 1
                }
              },
              "scope": {
                "if": {
                  "path": "issueEstimationType",
                  "equals": "notUsed"
                },
                "then": {
                  "first": "$",
                  "of": "activeCycle.issueCountHistory",
                  "last": 1
                },
                "else": {
                  "first": "$",
                  "of": "activeCycle.scopeHistory",
                  "last": 1
                }
              },
              "unit": {
                "if": {
                  "path": "issueEstimationType",
                  "equals": "notUsed"
                },
                "then": {
                  "template": "issues"
                },
                "else": {
                  "template": "points"
                }
              }
            }
          }
        },
        {
          "name": "cycles",
          "value": {
            "each": "cycle_rows",
            "where": {
              "path": "scope",
              "gt": 0
            },
            "fields": {
              "key": "key",
              "number": "number",
              "ends": "ends",
              "done": "done",
              "scope": "scope",
              "unit": "unit"
            }
          }
        },
        {
          "name": "unread",
          "value": {
            "each": "gql.data.notifications.nodes",
            "where": {
              "all": [
                {
                  "path": "readAt",
                  "exists": false
                },
                {
                  "path": "snoozedUntilAt",
                  "exists": false
                }
              ]
            },
            "fields": {
              "title": "title",
              "url": "url",
              "at": "createdAt"
            }
          }
        },
        {
          "name": "unread_all",
          "value": {
            "count": "gql.data.notifications.nodes",
            "where": {
              "path": "readAt",
              "exists": false
            },
            "default": 0
          }
        },
        {
          "name": "older_unread",
          "value": {
            "if": {
              "all": [
                {
                  "value": {
                    "count": "gql.data.notifications.nodes",
                    "default": 0
                  },
                  "gte": 50
                },
                {
                  "value": {
                    "subtract": [
                      {
                        "path": "gql.data.notificationsUnreadCount",
                        "default": 0
                      },
                      "unread_all"
                    ]
                  },
                  "gt": 0
                }
              ]
            },
            "then": {
              "subtract": [
                "gql.data.notificationsUnreadCount",
                "unread_all"
              ]
            },
            "else": {
              "add": [
                0,
                0
              ]
            }
          }
        },
        {
          "name": "inbox_empty",
          "value": {
            "if": {
              "path": "older_unread",
              "gt": 0
            },
            "then": {
              "template": "{older_unread} unread, older than the 50 newest notifications."
            },
            "else": {
              "template": "No unread notifications."
            }
          }
        },
        {
          "name": "done_count",
          "value": {
            "count": "gql.data.done.nodes",
            "default": 0
          }
        },
        {
          "name": "done_words",
          "value": {
            "if": {
              "path": "gql.data.done.pageInfo.hasNextPage",
              "equals": true
            },
            "then": {
              "template": "At least {done_count} in 7 days"
            },
            "else": {
              "template": "{done_count} in 7 days"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "mine",
      "type": "tabs",
      "same_height": true,
      "tabs": [
        {
          "label": "Doing",
          "cards": [
            {
              "id": "doing",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "linear",
                "empty": {
                  "text": "Nothing in progress.",
                  "when": {
                    "value": {
                      "count": "doing_rows",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "doing_rows",
                    "sort": [
                      "order",
                      "position"
                    ],
                    "fields": {
                      "text": "title",
                      "url": "url",
                      "detail": "detail",
                      "key": "key",
                      "icon": {
                        "if": {
                          "path": "icon",
                          "not": ""
                        },
                        "then": "icon"
                      },
                      "color": {
                        "if": {
                          "path": "color",
                          "not": ""
                        },
                        "then": "color"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Todo",
          "cards": [
            {
              "id": "todo",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "linear",
                "empty": {
                  "text": "Nothing to do next.",
                  "when": {
                    "value": {
                      "count": "todo_rows",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "todo_rows",
                    "sort": [
                      "order",
                      "position"
                    ],
                    "fields": {
                      "text": "title",
                      "url": "url",
                      "detail": "detail",
                      "key": "key",
                      "icon": {
                        "if": {
                          "path": "icon",
                          "not": ""
                        },
                        "then": "icon"
                      },
                      "color": {
                        "if": {
                          "path": "color",
                          "not": ""
                        },
                        "then": "color"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Backlog",
          "cards": [
            {
              "id": "backlog",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "linear",
                "empty": {
                  "text": "The backlog is empty.",
                  "when": {
                    "value": {
                      "count": "backlog_rows",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "backlog_rows",
                    "sort": [
                      "order",
                      "position"
                    ],
                    "fields": {
                      "text": "title",
                      "url": "url",
                      "detail": "detail",
                      "key": "key",
                      "icon": {
                        "if": {
                          "path": "icon",
                          "not": ""
                        },
                        "then": "icon"
                      },
                      "color": {
                        "if": {
                          "path": "color",
                          "not": ""
                        },
                        "then": "color"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "Some lists are shortened here. Open [Linear](https://linear.app) for the rest.",
      "source": {
        "ref": "linear",
        "when": {
          "any": [
            {
              "value": {
                "path": "gql.data.doing.pageInfo.hasNextPage",
                "default": false
              },
              "equals": true
            },
            {
              "value": {
                "path": "gql.data.todo.pageInfo.hasNextPage",
                "default": false
              },
              "equals": true
            },
            {
              "value": {
                "path": "gql.data.backlog.pageInfo.hasNextPage",
                "default": false
              },
              "equals": true
            },
            {
              "value": {
                "path": "gql.data.viewer.teams.pageInfo.hasNextPage",
                "default": false
              },
              "equals": true
            },
            {
              "value": {
                "count": "cycles",
                "default": 0
              },
              "gt": 20
            },
            {
              "path": "older_unread",
              "gt": 0
            }
          ]
        }
      }
    },
    {
      "id": "cycles",
      "type": "pacing",
      "label": "Completed this cycle",
      "items": [],
      "source": {
        "ref": "linear",
        "when": {
          "value": {
            "count": "cycles",
            "default": 0
          },
          "gt": 0
        },
        "map": {
          "items": {
            "each": "cycles",
            "limit": 20,
            "fields": {
              "label": {
                "template": "{key} · Cycle {number}"
              },
              "value": "done",
              "target": "scope",
              "unit": "unit",
              "detail": {
                "template": "Ends {ends|day}"
              },
              "url": {
                "template": "legato://page/linear-cycle?team={key}"
              }
            }
          }
        }
      }
    },
    {
      "id": "inbox",
      "type": "list",
      "label": "Inbox",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "linear",
        "empty": {
          "text": "{inbox_empty}",
          "when": {
            "value": {
              "count": "unread",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "unread",
            "sort": {
              "by": "at",
              "order": "desc"
            },
            "fields": {
              "text": "title",
              "url": "url",
              "detail": {
                "template": "{at|ago}"
              }
            }
          }
        }
      }
    },
    {
      "id": "completed",
      "type": "heading",
      "text": "Completed",
      "collapsed": true,
      "cards": [
        "done"
      ],
      "source": {
        "ref": "linear",
        "map": {
          "detail": "done_words"
        }
      }
    },
    {
      "id": "done",
      "type": "list",
      "label": "Recently completed",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "linear",
        "empty": {
          "text": "Nothing completed in 7 days.",
          "when": {
            "path": "done_count",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "gql.data.done.nodes",
            "sort": {
              "by": "completedAt",
              "order": "desc"
            },
            "fields": {
              "text": "title",
              "url": "url",
              "detail": {
                "template": "{completedAt|ago}"
              }
            }
          }
        }
      }
    }
  ]
}
