Skip to content

Commit

Permalink
feat(snippet): update the resolver to retrieve snippet content
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel committed Nov 1, 2023
1 parent 18db8b6 commit 644eb68
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 29 deletions.
5 changes: 3 additions & 2 deletions apps/core/src/resources/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { updateSnippet } from './snippets/mutations/update-snippet';
import { findSnippet } from './snippets/queries/find-snippet';
import { mySnippets } from './snippets/queries/my-snippets';
import { publicSnippets } from './snippets/queries/public-snippets';
import { shortContentResolver } from './snippets/queries/resolvers/short-content';
import { dateScalar } from './types/date';
import { loginUser } from './users/mutations/login-user';
import { logoutUser } from './users/mutations/logout-user';
Expand Down Expand Up @@ -58,10 +57,12 @@ const resolvers: Resolvers = {
publicSnippets,
},
Snippet: {
contentHighlighted: (snippet) => {
return snippet.contentHtml;
},
folder: (snippet, _args, context) => {
return context.db.folder.findById(snippet.folderId);
},
shortContent: shortContentResolver,
user: (snippet, _args, context) => {
return context.db.user.findById(snippet.userId);
},
Expand Down
3 changes: 1 addition & 2 deletions apps/core/src/resources/schema.graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export default gql`
id: ID!
name: String!
content: String!
contentHighlighted: String
shortContent: String!
contentHighlighted: String!
language: String!
lineHighlight: String
size: Int!
Expand Down
6 changes: 2 additions & 4 deletions apps/core/src/types/graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,14 @@ export type SignupUserResult = {
export type Snippet = {
__typename?: 'Snippet';
content: Scalars['String'];
contentHighlighted?: Maybe<Scalars['String']>;
contentHighlighted: Scalars['String'];
createdAt: Scalars['Date'];
description?: Maybe<Scalars['String']>;
folder: Folder;
id: Scalars['ID'];
language: Scalars['String'];
lineHighlight?: Maybe<Scalars['String']>;
name: Scalars['String'];
shortContent: Scalars['String'];
size: Scalars['Int'];
theme: Scalars['String'];
updatedAt: Scalars['Date'];
Expand Down Expand Up @@ -589,15 +588,14 @@ export type SnippetResolvers<
ParentType extends ResolversParentTypes['Snippet'] = ResolversParentTypes['Snippet'],
> = {
content?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
contentHighlighted?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
contentHighlighted?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
createdAt?: Resolver<ResolversTypes['Date'], ParentType, ContextType>;
description?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
folder?: Resolver<ResolversTypes['Folder'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
language?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
lineHighlight?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
shortContent?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
size?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
theme?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
updatedAt?: Resolver<ResolversTypes['Date'], ParentType, ContextType>;
Expand Down
15 changes: 3 additions & 12 deletions apps/web/src/components/snippets/public-snippet.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { Highlighter, Link, PublicSnippetResult, UserAvatar } from '@sharingan/front';
import { useEditor } from '@sharingan/front/src/components/directory/snippets/form/editor/hooks/use-editor';
import { Link, PublicSnippetResult, UserAvatar } from '@sharingan/front';

type Props = {
highlighter?: Highlighter;
snippet: PublicSnippetResult['items'][number];
};

export const PublicSnippet = ({ highlighter, snippet }: Props) => {
const { highlightSnippet } = useEditor();
export const PublicSnippet = ({ snippet }: Props) => {
const { user } = snippet;

const htmlCode = highlightSnippet({
code: snippet.content,
highlighter,
language: snippet.language,
lineHighlight: snippet.lineHighLight,
theme: snippet.theme,
});
const htmlCode = snippet.content;

return (
<div className="rounded-md border border-gray-300 shadow-sm rounded p-3 bg-white">
Expand Down
6 changes: 2 additions & 4 deletions apps/web/src/containers/private/browse.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PublicSnippetResult, useCodeHighlighter } from '@sharingan/front';
import { PublicSnippetResult } from '@sharingan/front';
import { NextSeo } from 'next-seo';

import Layout from '@/components/layout/private/layout';
Expand All @@ -9,8 +9,6 @@ type Props = {
};

const Browse = ({ data }: Props) => {
const { highlighter } = useCodeHighlighter();

console.log(data);

const snippets = data.items;
Expand All @@ -29,7 +27,7 @@ const Browse = ({ data }: Props) => {
<div className="px-4 py-8 sm:px-0">
<div className="space-y-3 min-h-96">
{snippets.map((snippet) => (
<PublicSnippet highlighter={highlighter} key={snippet.id} snippet={snippet} />
<PublicSnippet key={snippet.id} snippet={snippet} />
))}
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions packages/front/src/graphql/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export type SignupUserResult = {
export type Snippet = {
__typename?: 'Snippet';
content: Scalars['String'];
contentHighlighted?: Maybe<Scalars['String']>;
contentHighlighted: Scalars['String'];
createdAt: Scalars['Date'];
description?: Maybe<Scalars['String']>;
folder: Folder;
Expand Down Expand Up @@ -360,7 +360,7 @@ export type FindSnippetQuery = {
lineHighlight?: string | null;
visibility: SnippetVisibility;
content: string;
contentHighlighted?: string | null;
contentHighlighted: string;
theme: string;
createdAt: any;
updatedAt: any;
Expand All @@ -387,7 +387,7 @@ export type PublicSnippetsQuery = {
description?: string | null;
language: string;
lineHighlight?: string | null;
shortContent: string;
contentHighlighted: string;
theme: string;
createdAt: any;
user: { __typename?: 'User'; id: string; username?: string | null; name: string; pictureUrl?: string | null };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const findPublicSnippetsQuery = gql`
description
language
lineHighlight
shortContent
contentHighlighted
theme
createdAt
user {
Expand Down
2 changes: 1 addition & 1 deletion packages/front/src/services/snippets/public-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const formatPublicSnippetsResult = (data?: PublicSnippetsQuery): PublicSn
hasMore,
itemPerPage,
items: items.map((snippet) => ({
content: snippet.shortContent,
content: snippet.contentHighlighted,
createdAt: snippet.createdAt,
description: snippet.description ?? null,
id: snippet.id,
Expand Down

0 comments on commit 644eb68

Please sign in to comment.