Skip to content

Commit db9d66e

Browse files
authored
Fix anonymous metadata query (#468)
* refactor: create an apps folder that includes all services * feat: add UI package with atomic components and utilities * feat: initialize landing page with Next.js, Tailwind CSS, and essential configurations - Add ESLint and Prettier configurations for code quality. - Create Next.js configuration for custom webpack settings. - Set up Tailwind CSS with a comprehensive theme and utility classes. - Implement landing page layout with header, footer, and main sections. - Include components for user interaction, such as sign-in options and upload functionality. - Establish a responsive design with a focus on user experience and accessibility. - Add README for project setup and usage instructions. * feat: integrate @auto-drive/ui package and refactor components - Add @auto-drive/ui as a dependency for shared components and utilities. - Update Tailwind configuration to include UI package paths for content. - Refactor various components to utilize the new UI package for buttons and routes. - Remove deprecated components and files, streamlining the codebase. - Adjust navigation logic in the app to enhance user experience. * chore: add @auto-drive/ui as a workspace dependency in yarn.lock * chore: add UI build step to Makefile and include it in common targets * fix: some old imports * chore: add landing and UI workspace commands to package.json * chore: update Makefile to include 'ui' in the all target * chore: update test and build commands in package.json for auth and backend apps * feat: implement automatic login and authentication redirection * fix: update redirection path in page component from explorer to drive/global * refactor: remove unused imports in SideNavBar component * refactor: replace useAutomaticLogin hook with AutomaticLoginWrapper component in AppLayout * fix: anonymous fetch metadata query by head_cid * feat: implements an error page if some error happen in server side
1 parent 060b453 commit db9d66e

File tree

8 files changed

+249
-17
lines changed

8 files changed

+249
-17
lines changed

apps/frontend/gql/graphql.ts

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,7 +3577,14 @@ export type GetMetadataByHeadCidQueryVariables = Exact<{
35773577
}>;
35783578

35793579

3580-
export type GetMetadataByHeadCidQuery = { __typename?: 'query_root', metadata: Array<{ __typename?: 'metadata', head_cid: string, tags?: Array<string> | null, metadata?: any | null, created_at?: any | null, maximumBlockDepth: Array<{ __typename?: 'nodes', block_published_on?: number | null, tx_published_on?: string | null }>, minimumBlockDepth: Array<{ __typename?: 'nodes', block_published_on?: number | null, tx_published_on?: string | null }>, publishedNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, archivedNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, totalNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, object_ownership: Array<{ __typename?: 'object_ownership', oauth_user_id: string, oauth_provider: string, is_admin?: boolean | null }> }> };
3580+
export type GetMetadataByHeadCidQuery = { __typename?: 'query_root', metadata: Array<{ __typename?: 'metadata', head_cid: string, tags?: Array<string> | null, metadata?: any | null, created_at?: any | null, maximumBlockDepth: Array<{ __typename?: 'nodes', block_published_on?: number | null, tx_published_on?: string | null }>, minimumBlockDepth: Array<{ __typename?: 'nodes', block_published_on?: number | null, tx_published_on?: string | null }>, publishedNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, archivedNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, totalNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null } }> };
3581+
3582+
export type GetMetadataByHeadCidWithOwnershipQueryVariables = Exact<{
3583+
headCid: Scalars['String']['input'];
3584+
}>;
3585+
3586+
3587+
export type GetMetadataByHeadCidWithOwnershipQuery = { __typename?: 'query_root', metadata: Array<{ __typename?: 'metadata', head_cid: string, tags?: Array<string> | null, metadata?: any | null, created_at?: any | null, maximumBlockDepth: Array<{ __typename?: 'nodes', block_published_on?: number | null, tx_published_on?: string | null }>, minimumBlockDepth: Array<{ __typename?: 'nodes', block_published_on?: number | null, tx_published_on?: string | null }>, publishedNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, archivedNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, totalNodes: { __typename?: 'nodes_aggregate', aggregate?: { __typename?: 'nodes_aggregate_fields', count: number } | null }, object_ownership: Array<{ __typename?: 'object_ownership', oauth_user_id: string, oauth_provider: string, is_admin?: boolean | null }> }> };
35813588

35823589
export type SearchGlobalMetadataByCidOrNameQueryVariables = Exact<{
35833590
search: Scalars['String']['input'];
@@ -4134,11 +4141,6 @@ export const GetMetadataByHeadCidDocument = gql`
41344141
count
41354142
}
41364143
}
4137-
object_ownership {
4138-
oauth_user_id
4139-
oauth_provider
4140-
is_admin
4141-
}
41424144
}
41434145
}
41444146
`;
@@ -4175,6 +4177,85 @@ export type GetMetadataByHeadCidQueryHookResult = ReturnType<typeof useGetMetada
41754177
export type GetMetadataByHeadCidLazyQueryHookResult = ReturnType<typeof useGetMetadataByHeadCidLazyQuery>;
41764178
export type GetMetadataByHeadCidSuspenseQueryHookResult = ReturnType<typeof useGetMetadataByHeadCidSuspenseQuery>;
41774179
export type GetMetadataByHeadCidQueryResult = Apollo.QueryResult<GetMetadataByHeadCidQuery, GetMetadataByHeadCidQueryVariables>;
4180+
export const GetMetadataByHeadCidWithOwnershipDocument = gql`
4181+
query GetMetadataByHeadCIDWithOwnership($headCid: String!) {
4182+
metadata(
4183+
where: {_or: [{head_cid: {_ilike: $headCid}}, {name: {_ilike: $headCid}}]}
4184+
) {
4185+
head_cid
4186+
tags
4187+
metadata
4188+
created_at
4189+
maximumBlockDepth: nodes(
4190+
order_by: {block_published_on: desc_nulls_last}
4191+
limit: 1
4192+
) {
4193+
block_published_on
4194+
tx_published_on
4195+
}
4196+
minimumBlockDepth: nodes(
4197+
order_by: {block_published_on: desc_nulls_last}
4198+
limit: 1
4199+
) {
4200+
block_published_on
4201+
tx_published_on
4202+
}
4203+
publishedNodes: nodes_aggregate(where: {block_published_on: {_is_null: false}}) {
4204+
aggregate {
4205+
count
4206+
}
4207+
}
4208+
archivedNodes: nodes_aggregate(where: {piece_offset: {_is_null: false}}) {
4209+
aggregate {
4210+
count
4211+
}
4212+
}
4213+
totalNodes: nodes_aggregate {
4214+
aggregate {
4215+
count
4216+
}
4217+
}
4218+
object_ownership {
4219+
oauth_user_id
4220+
oauth_provider
4221+
is_admin
4222+
}
4223+
}
4224+
}
4225+
`;
4226+
4227+
/**
4228+
* __useGetMetadataByHeadCidWithOwnershipQuery__
4229+
*
4230+
* To run a query within a React component, call `useGetMetadataByHeadCidWithOwnershipQuery` and pass it any options that fit your needs.
4231+
* When your component renders, `useGetMetadataByHeadCidWithOwnershipQuery` returns an object from Apollo Client that contains loading, error, and data properties
4232+
* you can use to render your UI.
4233+
*
4234+
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
4235+
*
4236+
* @example
4237+
* const { data, loading, error } = useGetMetadataByHeadCidWithOwnershipQuery({
4238+
* variables: {
4239+
* headCid: // value for 'headCid'
4240+
* },
4241+
* });
4242+
*/
4243+
export function useGetMetadataByHeadCidWithOwnershipQuery(baseOptions: Apollo.QueryHookOptions<GetMetadataByHeadCidWithOwnershipQuery, GetMetadataByHeadCidWithOwnershipQueryVariables> & ({ variables: GetMetadataByHeadCidWithOwnershipQueryVariables; skip?: boolean; } | { skip: boolean; }) ) {
4244+
const options = {...defaultOptions, ...baseOptions}
4245+
return Apollo.useQuery<GetMetadataByHeadCidWithOwnershipQuery, GetMetadataByHeadCidWithOwnershipQueryVariables>(GetMetadataByHeadCidWithOwnershipDocument, options);
4246+
}
4247+
export function useGetMetadataByHeadCidWithOwnershipLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<GetMetadataByHeadCidWithOwnershipQuery, GetMetadataByHeadCidWithOwnershipQueryVariables>) {
4248+
const options = {...defaultOptions, ...baseOptions}
4249+
return Apollo.useLazyQuery<GetMetadataByHeadCidWithOwnershipQuery, GetMetadataByHeadCidWithOwnershipQueryVariables>(GetMetadataByHeadCidWithOwnershipDocument, options);
4250+
}
4251+
export function useGetMetadataByHeadCidWithOwnershipSuspenseQuery(baseOptions?: Apollo.SkipToken | Apollo.SuspenseQueryHookOptions<GetMetadataByHeadCidWithOwnershipQuery, GetMetadataByHeadCidWithOwnershipQueryVariables>) {
4252+
const options = baseOptions === Apollo.skipToken ? baseOptions : {...defaultOptions, ...baseOptions}
4253+
return Apollo.useSuspenseQuery<GetMetadataByHeadCidWithOwnershipQuery, GetMetadataByHeadCidWithOwnershipQueryVariables>(GetMetadataByHeadCidWithOwnershipDocument, options);
4254+
}
4255+
export type GetMetadataByHeadCidWithOwnershipQueryHookResult = ReturnType<typeof useGetMetadataByHeadCidWithOwnershipQuery>;
4256+
export type GetMetadataByHeadCidWithOwnershipLazyQueryHookResult = ReturnType<typeof useGetMetadataByHeadCidWithOwnershipLazyQuery>;
4257+
export type GetMetadataByHeadCidWithOwnershipSuspenseQueryHookResult = ReturnType<typeof useGetMetadataByHeadCidWithOwnershipSuspenseQuery>;
4258+
export type GetMetadataByHeadCidWithOwnershipQueryResult = Apollo.QueryResult<GetMetadataByHeadCidWithOwnershipQuery, GetMetadataByHeadCidWithOwnershipQueryVariables>;
41784259
export const SearchGlobalMetadataByCidOrNameDocument = gql`
41794260
query SearchGlobalMetadataByCIDOrName($search: String!, $limit: Int!) {
41804261
metadata(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use client';
2+
3+
import React from 'react';
4+
import { useRouter } from 'next/navigation';
5+
import { defaultNetworkId, ROUTES } from '@auto-drive/ui';
6+
7+
type ErrorPageProps = {
8+
error: Error & { digest?: string };
9+
};
10+
11+
export default function ErrorPage({ error }: ErrorPageProps) {
12+
const router = useRouter();
13+
14+
return (
15+
<div className='flex min-h-[calc(100vh-10rem)] items-center justify-center p-8'>
16+
<div className='w-full max-w-xl text-center'>
17+
<h1 className='text-4xl font-bold'>Something went wrong</h1>
18+
<p className='mt-4 text-base text-muted-foreground md:text-lg'>
19+
An unexpected error occurred while loading this page. If the problem
20+
persists, please contact support.
21+
</p>
22+
{error?.message ? (
23+
<p className='mt-5 rounded bg-muted p-4 text-left text-sm leading-relaxed'>
24+
{error.message}
25+
{error?.digest ? (
26+
<span className='mt-2 block opacity-70'>
27+
Error ID: {error.digest}
28+
</span>
29+
) : null}
30+
</p>
31+
) : null}
32+
<div className='mt-8 flex items-center justify-center gap-4'>
33+
<button
34+
type='button'
35+
onClick={() => window.location.reload()}
36+
className='rounded-md bg-primary px-5 py-3 text-base font-medium text-primary-foreground hover:opacity-90'
37+
>
38+
Try again
39+
</button>
40+
<button
41+
type='button'
42+
onClick={() => router.replace(ROUTES.globalFeed(defaultNetworkId))}
43+
className='rounded-md border px-5 py-3 text-base hover:bg-muted'
44+
>
45+
Go to Drive
46+
</button>
47+
</div>
48+
</div>
49+
</div>
50+
);
51+
}
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
import {
22
GetMetadataByHeadCidDocument,
33
GetMetadataByHeadCidQuery,
4+
GetMetadataByHeadCidWithOwnershipDocument,
5+
GetMetadataByHeadCidWithOwnershipQuery,
46
} from 'gql/graphql';
57
import { ObjectDetails } from '@/components/organisms/ObjectDetails';
6-
import { mapObjectInformationFromQueryResult } from 'services/gql/utils';
8+
import {
9+
mapObjectInformationFromQueryResult,
10+
mapObjectInformationFromQueryResultWithOwnership,
11+
} from 'services/gql/utils';
712
import { NetworkId } from '@auto-drive/ui';
813
import { createGQLClientByNetwork } from 'services/gql';
14+
import { getServerSession } from 'next-auth';
915

1016
export const dynamic = 'force-dynamic';
1117

18+
const getMetadata = async (cid: string, chain: NetworkId) => {
19+
const gqlClient = createGQLClientByNetwork(chain);
20+
const session = await getServerSession();
21+
22+
if (session) {
23+
const { data } =
24+
await gqlClient.query<GetMetadataByHeadCidWithOwnershipQuery>({
25+
query: GetMetadataByHeadCidWithOwnershipDocument,
26+
variables: { headCid: cid },
27+
});
28+
29+
return mapObjectInformationFromQueryResultWithOwnership(data);
30+
} else {
31+
const { data } = await gqlClient.query<GetMetadataByHeadCidQuery>({
32+
query: GetMetadataByHeadCidDocument,
33+
variables: { headCid: cid },
34+
});
35+
36+
return mapObjectInformationFromQueryResult(data);
37+
}
38+
};
39+
1240
export default async function Page({
1341
params: { cid, chain },
1442
}: {
1543
params: { cid: string; chain: NetworkId };
1644
}) {
17-
const gqlClient = createGQLClientByNetwork(chain);
18-
19-
const { data } = await gqlClient.query<GetMetadataByHeadCidQuery>({
20-
query: GetMetadataByHeadCidDocument,
21-
variables: { headCid: cid },
22-
});
23-
24-
const metadata = mapObjectInformationFromQueryResult(data);
45+
const metadata = await getMetadata(cid, chain);
2546

2647
return <ObjectDetails object={metadata} />;
2748
}

apps/frontend/src/services/gql/common/query.graphql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,50 @@
11
query GetMetadataByHeadCID($headCid: String!) {
2+
metadata(
3+
where: {
4+
_or: [{ head_cid: { _ilike: $headCid } }, { name: { _ilike: $headCid } }]
5+
}
6+
) {
7+
head_cid
8+
tags
9+
metadata
10+
created_at
11+
maximumBlockDepth: nodes(
12+
order_by: { block_published_on: desc_nulls_last }
13+
limit: 1
14+
) {
15+
block_published_on
16+
tx_published_on
17+
}
18+
minimumBlockDepth: nodes(
19+
order_by: { block_published_on: desc_nulls_last }
20+
limit: 1
21+
) {
22+
block_published_on
23+
tx_published_on
24+
}
25+
publishedNodes: nodes_aggregate(
26+
where: { block_published_on: { _is_null: false } }
27+
) {
28+
aggregate {
29+
count
30+
}
31+
}
32+
archivedNodes: nodes_aggregate(
33+
where: { piece_offset: { _is_null: false } }
34+
) {
35+
aggregate {
36+
count
37+
}
38+
}
39+
totalNodes: nodes_aggregate {
40+
aggregate {
41+
count
42+
}
43+
}
44+
}
45+
}
46+
47+
query GetMetadataByHeadCIDWithOwnership($headCid: String!) {
248
metadata(
349
where: {
450
_or: [{ head_cid: { _ilike: $headCid } }, { name: { _ilike: $headCid } }]

apps/frontend/src/services/gql/utils.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { GetMetadataByHeadCidQuery } from 'gql/graphql';
1+
import {
2+
GetMetadataByHeadCidQuery,
3+
GetMetadataByHeadCidWithOwnershipQuery,
4+
} from 'gql/graphql';
25
import { ObjectInformation, OwnerRole, objectStatus } from '@auto-drive/models';
36

47
export const mapObjectInformationFromQueryResult = (
@@ -26,7 +29,37 @@ export const mapObjectInformationFromQueryResult = (
2629
status: objectStatus(uploadState),
2730
uploadState,
2831
tags: metadata.tags ?? [],
29-
owners: metadata.object_ownership.map((owner) => ({
32+
owners: [],
33+
publishedObjectId: null,
34+
};
35+
};
36+
37+
export const mapObjectInformationFromQueryResultWithOwnership = (
38+
result: GetMetadataByHeadCidWithOwnershipQuery,
39+
): ObjectInformation => {
40+
const metadata = result.metadata.at(0);
41+
if (!metadata) {
42+
throw new Error('Metadata not found');
43+
}
44+
45+
const uploadState = {
46+
uploadedNodes: metadata.publishedNodes?.aggregate?.count ?? 0,
47+
totalNodes: metadata.totalNodes?.aggregate?.count ?? 0,
48+
archivedNodes: metadata.archivedNodes?.aggregate?.count ?? 0,
49+
minimumBlockDepth:
50+
metadata.minimumBlockDepth?.[0]?.block_published_on ?? null,
51+
maximumBlockDepth:
52+
metadata.maximumBlockDepth?.[0]?.block_published_on ?? null,
53+
};
54+
55+
return {
56+
cid: metadata.head_cid,
57+
metadata: metadata.metadata,
58+
createdAt: metadata.created_at,
59+
status: objectStatus(uploadState),
60+
uploadState,
61+
tags: metadata.tags ?? [],
62+
owners: metadata.object_ownership?.map((owner) => ({
3063
role: owner.is_admin ? OwnerRole.ADMIN : OwnerRole.VIEWER,
3164
oauthProvider: owner.oauth_provider ?? '',
3265
oauthUserId: owner.oauth_user_id ?? '',
89.5 KB
Binary file not shown.
936 KB
Binary file not shown.
109 KB
Binary file not shown.

0 commit comments

Comments
 (0)