diff --git a/api/api.go b/api/api.go index 47ca130bb..97d24ae76 100644 --- a/api/api.go +++ b/api/api.go @@ -533,6 +533,13 @@ func (api *api) ListApps(limit uint64, offset uint64, filters ListAppsFilters, o orderBy = "last_used_at IS NULL, " + orderBy } + if orderBy == "last_settled_transaction" { + query = query.Select("apps.*, MAX(transactions.settled_at) as last_transaction_at"). + Joins("LEFT JOIN transactions ON transactions.app_id = apps.id AND transactions.state = ?", constants.TRANSACTION_STATE_SETTLED). + Group("apps.id") + orderBy = "last_transaction_at IS NULL, last_transaction_at DESC, apps.last_used_at" + } + query = query.Order(orderBy + " DESC") if limit == 0 { diff --git a/frontend/src/components/home/widgets/LatestUsedAppsWidget.tsx b/frontend/src/components/home/widgets/LatestUsedAppsWidget.tsx index 079fea3db..f732ce116 100644 --- a/frontend/src/components/home/widgets/LatestUsedAppsWidget.tsx +++ b/frontend/src/components/home/widgets/LatestUsedAppsWidget.tsx @@ -13,7 +13,7 @@ import { ALBY_ACCOUNT_APP_NAME } from "src/constants"; import { useApps } from "src/hooks/useApps"; export function LatestUsedAppsWidget() { - const { data: appsData } = useApps(3, undefined, undefined, "last_used_at"); + const { data: appsData } = useApps(3, undefined, undefined, "last_settled_transaction"); const apps = appsData?.apps; const usedApps = apps?.filter((x) => x.lastUsedAt); @@ -32,28 +32,22 @@ export function LatestUsedAppsWidget() { - {usedApps - .sort( - (a, b) => - new Date(b.lastUsedAt ?? 0).getTime() - - new Date(a.lastUsedAt ?? 0).getTime() - ) - .map((app) => ( - -
- -

- {app.name === ALBY_ACCOUNT_APP_NAME - ? "Alby Account" - : app.name} -

-

- {app.lastUsedAt ? dayjs(app.lastUsedAt).fromNow() : "never"} -

- -
- - ))} + {usedApps.map((app) => ( + +
+ +

+ {app.name === ALBY_ACCOUNT_APP_NAME + ? "Alby Account" + : app.name} +

+

+ {app.lastUsedAt ? dayjs(app.lastUsedAt).fromNow() : "never"} +

+ +
+ + ))}
); diff --git a/frontend/src/hooks/useApps.ts b/frontend/src/hooks/useApps.ts index 4c551b749..f8cc05aad 100644 --- a/frontend/src/hooks/useApps.ts +++ b/frontend/src/hooks/useApps.ts @@ -15,7 +15,7 @@ export function useApps( unused?: boolean; subWallets?: boolean; }, - orderBy?: "last_used_at" | "created_at", + orderBy?: "last_used_at" | "created_at" | "last_settled_transaction", isEnabled = true ) { const offset = (page - 1) * limit; diff --git a/nip47/event_handler.go b/nip47/event_handler.go index c765d74da..948bc6f2b 100644 --- a/nip47/event_handler.go +++ b/nip47/event_handler.go @@ -85,7 +85,7 @@ func (svc *nip47Service) HandleEvent(ctx context.Context, pool nostrmodels.Simpl err = svc.db.Model(&app).Update("last_used_at", &now).Error if err != nil { logger.Logger.WithFields(logrus.Fields{ - "it": app.ID, + "app_id": app.ID, }).WithError(err).Error("Failed to update app last used time") }