Skip to content

Commit

Permalink
refactor: dashboard (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihebi authored Aug 3, 2023
1 parent 794c0f8 commit c8f37f8
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 467 deletions.
51 changes: 15 additions & 36 deletions api/src/resolver_repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ async function ensurePodEditAccess({ id, userId }) {
}
}

async function myRepos(_, __, { userId }) {
async function getDashboardRepos(_, __, { userId }) {
if (!userId) throw Error("Unauthenticated");
const repos = await prisma.repo.findMany({
where: {
owner: {
id: userId,
},
OR: [
{
owner: {
id: userId,
},
},
{
collaborators: {
some: { id: userId },
},
},
],
},
include: {
UserRepoData: {
Expand All @@ -58,46 +67,17 @@ async function myRepos(_, __, { userId }) {
stargazers: true,
},
});
// Sort by last access time.
repos.sort((a, b) => {
if (a.UserRepoData.length > 0) {
if (b.UserRepoData.length > 0) {
return (
b.UserRepoData[0].accessedAt.valueOf() -
a.UserRepoData[0].accessedAt.valueOf()
);
}
return -1;
}
return a.updatedAt.valueOf() - b.updatedAt.valueOf();
});
// Re-use updatedAt field (this is actually the lastviewed field).
return repos.map((repo) => {
return {
...repo,
updatedAt:
accessedAt:
repo.UserRepoData.length > 0
? repo.UserRepoData[0].accessedAt
: repo.updatedAt,
};
});
}

async function myCollabRepos(_, __, { userId }) {
if (!userId) throw Error("Unauthenticated");
const repos = await prisma.repo.findMany({
where: {
collaborators: {
some: { id: userId },
},
},
include: {
stargazers: true,
},
});
return repos;
}

async function updateUserRepoData({ userId, repoId }) {
// FIXME I should probably rename this from query to mutation?
//
Expand Down Expand Up @@ -609,9 +589,8 @@ async function copyRepo(_, { repoId }, { userId }) {

export default {
Query: {
myRepos,
repo,
myCollabRepos,
getDashboardRepos,
getVisibility,
},
Mutation: {
Expand Down
4 changes: 2 additions & 2 deletions api/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const typeDefs = gql`
public: Boolean
createdAt: String
updatedAt: String
accessedAt: String
}
type Edge {
Expand Down Expand Up @@ -105,11 +106,10 @@ export const typeDefs = gql`
repos: [Repo]
repo(id: String!): Repo
pod(id: ID!): Pod
myRepos: [Repo]
getDashboardRepos: [Repo]
activeSessions: [String]
getVisibility(repoId: String!): Visibility
listAllRuntimes: [RuntimeInfo]
myCollabRepos: [Repo]
infoRuntime(sessionId: String!): RuntimeInfo
}
Expand Down
Loading

0 comments on commit c8f37f8

Please sign in to comment.