diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 49f57d69..f66369f7 100755 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -596,10 +596,15 @@ }, "gitea": { "notifications": "Notifications", - "Issue": "Issues", - "Pull": "Pull Requests", - "Commit": "Commits", - "Repository": "Repositories" + "issue": "Issues", + "pull": "Pull Requests", + "commit": "Commits", + "repository": "Repositories", + "followers": "Followers", + "following": "Following", + "repos": "Repositories", + "forks": "Stars", + "stars": "Forks" }, "audiobookshelf": { "podcasts": "Podcasts", diff --git a/src/widgets/gitea/component.jsx b/src/widgets/gitea/component.jsx index 881ef043..85e17a1e 100644 --- a/src/widgets/gitea/component.jsx +++ b/src/widgets/gitea/component.jsx @@ -5,41 +5,73 @@ import Container from "components/services/widget/container"; import useWidgetAPI from "utils/proxy/use-widget-api"; +function hasField(fields, fieldTypes) { + return fields.some(field => fieldTypes.includes(field)); +} - -export default function Component ({ service }) { +export default function Component({ service }) { const { t } = useTranslation(); + let data = {}; const { widget } = service; + const fields = widget.fields ?? ["repos", "followers", "notifications"]; - const { data: notifications, error: giteaError } = useWidgetAPI(widget, "allNotifications"); - const notificationTypes = (notifications ?? []).reduce((acc, notification) => { - acc[notification.subject.type].push(notification); - return acc; - } - , { - "Issue": [], - "Pull": [], - "Commit": [], - "Repository": [] - } - ); + // Different fields require different API calls + const notificationFields = ["notifications", "issue", "pull", "commit", "repository"]; + const userFields = ["followers", "following"]; + const repoFields = ["repos", "stars", "forks"]; + + if (hasField(fields, userFields)) { + // eslint-disable-next-line react-hooks/rules-of-hooks + const { data: user, error: userError } = useWidgetAPI(widget, "user"); + if (userError) { + return ; + } + data = { ...data, followers: user?.followers_count, following: user?.following_count }; - if (giteaError) { - return ; } - if (!notifications) return ( - - - - ); + if (hasField(fields, repoFields)) { + // eslint-disable-next-line react-hooks/rules-of-hooks + const { data: repos, error: repoError } = useWidgetAPI(widget, "repos"); + if (repoError) { + return ; + } + const repoStats = (repos ?? []).reduce((acc, repo) => { + acc.repos += 1; + acc.stars += repo.stars_count; + acc.forks += repo.forks_count; + return acc; + }, { repos: 0, stars: 0, forks: 0 }); + + data = { ...data, ...repoStats }; + } + + if (hasField(fields, notificationFields)) { + + // eslint-disable-next-line react-hooks/rules-of-hooks + const { data: notifications, error: notificationError } = useWidgetAPI(widget, "notifications"); + + if (notificationError) { + return ; + } + + const notificationTypes = (notifications ?? []).reduce((acc, notification) => { + acc[notification.subject.type.toLowerCase()] += 1; + acc.notifications += 1; + return acc; + }, { + "notifications": 0, "issue": 0, "pull": 0, "commit": 0, "repository": 0 + } + ); + + data = { ...data, ...notificationTypes }; + } return ( - {Object.keys(notificationTypes).map((type) => - + {fields.map((field) => + )} ); diff --git a/src/widgets/gitea/widget.js b/src/widgets/gitea/widget.js index cf967c27..52ff143e 100644 --- a/src/widgets/gitea/widget.js +++ b/src/widgets/gitea/widget.js @@ -1,16 +1,19 @@ import genericProxyHandler from "utils/proxy/handlers/generic"; const widget = { - api: "{url}/api/v1/{endpoint}?access_token={key}", + api: "{url}/api/{endpoint}?access_token={key}", proxyHandler: genericProxyHandler, mappings: { - "allNotifications": { - endpoint: "notifications", + "notifications": { + endpoint: "v1/notifications", // required scope: notification }, - "newNotifications": { - endpoint: "notifications/new", + "user": { + endpoint: "v1/user", }, + "repos": { + endpoint: "v1/user/repos", // required scope: repo + } }, };