Skip to content

Commit

Permalink
feat: wire up the new project page (#1067)
Browse files Browse the repository at this point in the history
* Reflected in Plasmic v48.0.0, this does the main data queries
  server-side (cached), and then passes it in as props to the page.
* Adds a bunch of new GraphQL queries that we use for the project page
* Adds a `queryWrapper` to cache new queries easily
* Added some minor logging to the auth route for later
  • Loading branch information
ryscheng authored Mar 15, 2024
1 parent 736f3f7 commit aae8a0b
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 60 deletions.
11 changes: 7 additions & 4 deletions apps/frontend/app/api/auth/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function GET(request: NextRequest) {

// If no token provided, then return anonymous role
if (!auth) {
console.log(`/api/auth: No token => anon`);
return NextResponse.json(makeAnonRole());
}

Expand All @@ -54,9 +55,9 @@ export async function GET(request: NextRequest) {
// Try JWT decoding
try {
const decoded = jwtDecode(token);
console.log("JWT token:", decoded);
console.log("JWT token: ", decoded);
} catch (e) {
console.warn("JWT error: ", e);
console.warn("JWT decoding error: ", e);
}

// Get the user
Expand All @@ -66,12 +67,13 @@ export async function GET(request: NextRequest) {
.eq(API_KEY_COLUMN, token);

if (keyError || !keyData) {
console.warn("Error retrieving API keys", keyError);
console.warn(`/api/auth: Error retrieving API keys => anon`, keyError);
return NextResponse.json(makeAnonRole());
}

const activeKeys = keyData.filter((x) => !x.deleted_at);
if (activeKeys.length < 1) {
console.log(`/api/auth: API key not valid => anon`);
return NextResponse.json(makeAnonRole());
}

Expand All @@ -86,12 +88,13 @@ export async function GET(request: NextRequest) {

if (collectiveError || !collectiveData) {
console.warn(
"Error retrieving data collective membership",
`/api/auth: Valid key, error retrieving data collective membership => user`,
collectiveError,
);
return NextResponse.json(makeUserRole(userId));
} else if (collectiveData.length < 1) {
// Not a member
console.log(`/api/auth: Valid key, not data collective member => user`);
return NextResponse.json(makeUserRole(userId));
}

Expand Down
42 changes: 40 additions & 2 deletions apps/frontend/app/project/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { cache } from "react";
import { PlasmicComponent } from "@plasmicapp/loader-nextjs";
import { PLASMIC } from "../../../plasmic-init";
import { PlasmicClientRootProvider } from "../../../plasmic-init-client";
import { cachedGetProjectsBySlugs } from "../../../lib/graphql/cached-queries";
import {
cachedGetArtifactsByIds,
cachedGetArtifactIdsByProjectIds,
cachedGetProjectsBySlugs,
cachedGetCollectionsByIds,
cachedGetCollectionIdsByProjectIds,
cachedGetCodeMetricsByProjectIds,
cachedGetOnchainMetricsByProjectIds,
cachedGetAllEventTypes,
} from "../../../lib/graphql/cached-queries";
import { logger } from "../../../lib/logger";
import { catchallPathToString } from "../../../lib/paths";
import { STATIC_EXPORT } from "../../../lib/config";
Expand Down Expand Up @@ -53,8 +62,32 @@ export default async function ProjectPage(props: ProjectPageProps) {
notFound();
}
const project = projectArray[0];
//console.log("project", project);

const { event_types: eventTypes } = await cachedGetAllEventTypes();
const { code_metrics_by_project: codeMetrics } =
await cachedGetCodeMetricsByProjectIds({
project_ids: [project.project_id],
});
const { onchain_metrics_by_project: onchainMetrics } =
await cachedGetOnchainMetricsByProjectIds({
project_ids: [project.project_id],
});
const { artifacts_by_project: artifactIds } =
await cachedGetArtifactIdsByProjectIds({
project_ids: [project.project_id],
});
const { projects_by_collection: collectionIds } =
await cachedGetCollectionIdsByProjectIds({
project_ids: [project.project_id],
});
const { artifacts } = await cachedGetArtifactsByIds({
artifact_ids: artifactIds.map((x: any) => x.artifact_id),
});
const { collections } = await cachedGetCollectionsByIds({
collection_ids: collectionIds.map((x: any) => x.collection_id),
});

//console.log(project);
const plasmicData = await cachedFetchComponent(PLASMIC_COMPONENT);
const compMeta = plasmicData.entryCompMetas[0];

Expand All @@ -68,6 +101,11 @@ export default async function ProjectPage(props: ProjectPageProps) {
component={compMeta.displayName}
componentProps={{
metadata: project,
codeMetrics,
onchainMetrics,
eventTypes,
artifacts,
collections,
}}
/>
</PlasmicClientRootProvider>
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/components/dataprovider/event-data-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from "lodash";
import React from "react";
import { assertNever, ensure, uncheckedCast } from "../../lib/common";
import {
GET_ARTIFACT_BY_IDS,
GET_ARTIFACTS_BY_IDS,
GET_PROJECTS_BY_IDS,
GET_COLLECTIONS_BY_IDS,
GET_EVENTS_DAILY_TO_ARTIFACT,
Expand Down Expand Up @@ -359,7 +359,7 @@ function ArtifactEventDataProvider(props: EventDataProviderProps) {
data: artifactData,
error: artifactError,
loading: artifactLoading,
} = useQuery(GET_ARTIFACT_BY_IDS, {
} = useQuery(GET_ARTIFACTS_BY_IDS, {
variables: {
artifact_ids: props.ids,
},
Expand Down
127 changes: 77 additions & 50 deletions apps/frontend/lib/graphql/cached-queries.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,114 @@
import { cache } from "react";
import { getApolloClient } from "../clients/apollo";
import { QueryOptions } from "@apollo/client";
import {
GET_ALL_ARTIFACTS,
GET_ARTIFACTS_BY_IDS,
GET_ARTIFACT_BY_NAME,
GET_ALL_PROJECTS,
GET_ARTIFACT_IDS_BY_PROJECT_IDS,
GET_PROJECTS_BY_SLUGS,
GET_COLLECTIONS_BY_IDS,
GET_COLLECTION_IDS_BY_PROJECT_IDS,
GET_CODE_METRICS_BY_PROJECT,
GET_ONCHAIN_METRICS_BY_PROJECT,
GET_ALL_EVENT_TYPES,
} from "./queries";
import { logger } from "../logger";

// Revalidate the data at most every hour
export const revalidate = false; // 3600 = 1 hour

// Cached getters
const cachedGetAllArtifacts = cache(async () => {
const { data, error } = await getApolloClient().query({
query: GET_ALL_ARTIFACTS,
});
const queryWrapper = async (opts: QueryOptions) => {
const { data, error } = await getApolloClient().query(opts);
if (error) {
logger.error(error);
throw error;
}
return data;
});
};

// Cached getters
const cachedGetAllEventTypes = cache(async () =>
queryWrapper({
query: GET_ALL_EVENT_TYPES,
}),
);

const cachedGetProjectsBySlugs = cache(
async (variables: { project_slugs: string[] }) =>
queryWrapper({
query: GET_PROJECTS_BY_SLUGS,
variables,
}),
);

const cachedGetArtifactsByIds = cache(
async (variables: { artifact_ids: string[] }) =>
queryWrapper({
query: GET_ARTIFACTS_BY_IDS,
variables,
}),
);

const cachedGetArtifactByName = cache(
async (variables: {
artifact_namespace: string;
artifact_type: string;
artifact_name: string;
}) => {
const { data, error } = await getApolloClient().query({
}) =>
queryWrapper({
query: GET_ARTIFACT_BY_NAME,
variables,
});
if (error) {
logger.error(error);
throw error;
}
return data;
},
}),
);

const cachedGetAllProjects = cache(async () => {
const { data, error } = await getApolloClient().query({
query: GET_ALL_PROJECTS,
});
if (error) {
logger.error(error);
throw error;
}
return data;
});
const cachedGetArtifactIdsByProjectIds = cache(
async (variables: { project_ids: string[] }) =>
queryWrapper({
query: GET_ARTIFACT_IDS_BY_PROJECT_IDS,
variables,
}),
);

const cachedGetProjectsBySlugs = cache(
async (variables: { project_slugs: string[] }) => {
const { data, error } = await getApolloClient().query({
query: GET_PROJECTS_BY_SLUGS,
const cachedGetCollectionsByIds = cache(
async (variables: { collection_ids: string[] }) =>
queryWrapper({
query: GET_COLLECTIONS_BY_IDS,
variables,
});
if (error) {
logger.error(error);
throw error;
}
return data;
},
}),
);

const cachedGetAllEventTypes = cache(async () => {
const { data, error } = await getApolloClient().query({
query: GET_ALL_EVENT_TYPES,
});
if (error) {
logger.error(error);
throw error;
}
return data;
});
const cachedGetCollectionIdsByProjectIds = cache(
async (variables: { project_ids: string[] }) =>
queryWrapper({
query: GET_COLLECTION_IDS_BY_PROJECT_IDS,
variables,
}),
);

const cachedGetCodeMetricsByProjectIds = cache(
async (variables: { project_ids: string[] }) =>
queryWrapper({
query: GET_CODE_METRICS_BY_PROJECT,
variables,
}),
);

const cachedGetOnchainMetricsByProjectIds = cache(
async (variables: { project_ids: string[] }) =>
queryWrapper({
query: GET_ONCHAIN_METRICS_BY_PROJECT,
variables,
}),
);

export {
cachedGetAllArtifacts,
cachedGetArtifactsByIds,
cachedGetArtifactByName,
cachedGetAllProjects,
cachedGetArtifactIdsByProjectIds,
cachedGetProjectsBySlugs,
cachedGetCollectionsByIds,
cachedGetCollectionIdsByProjectIds,
cachedGetCodeMetricsByProjectIds,
cachedGetOnchainMetricsByProjectIds,
cachedGetAllEventTypes,
};
Loading

0 comments on commit aae8a0b

Please sign in to comment.