Skip to content

Commit

Permalink
add duplication option
Browse files Browse the repository at this point in the history
  • Loading branch information
Focke Adrian committed May 15, 2024
1 parent 35c2d15 commit 304bd97
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 31 deletions.
11 changes: 3 additions & 8 deletions app/files/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import Files from '@/components/Files';
import client from '@/tina/__generated__/client';
import type { FileLinkInfo } from '@/types/index';

export default async function Page() {
const result = await client.queries.fileConnection();
const files = result.data.fileConnection.edges?.map((file) => {
return {
link: file?.node?._sys.filename,
name: file?.node?.name,
};
}) as FileLinkInfo[];
const files = result.data.fileConnection.edges?.map((file) => file?.node);

return <Files files={files} />;
// TODO
return <Files files={files as any} />;
}
43 changes: 37 additions & 6 deletions components/Card/ContextCard.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import client from '@/tina/__generated__/client';
import type { File } from '@/tina/__generated__/types';
import * as ContextMenu from '@radix-ui/react-context-menu';
import { FileIcon } from '@radix-ui/react-icons';
import { AccessibleIcon, Card, Flex, Text } from '@radix-ui/themes';
import { useRouter } from 'next/navigation';

type ContextCardProps = {
text: string;
href: string;
file: File;
};

/** Card component with context menu wrapped around which displays file actions */
export default ({ text, href }: ContextCardProps) => {
export default ({ file }: ContextCardProps) => {
const router = useRouter();

console.log('File: ', file);

return (
<ContextMenu.Root>
<ContextMenu.Trigger
Expand All @@ -23,7 +27,7 @@ export default ({ text, href }: ContextCardProps) => {
</AccessibleIcon>
<Text className='select-none text-tina-blue'>File</Text>
</Flex>
<Text className='text-black'>{text}</Text>
<Text className='text-black'>{file.name}</Text>
</Card>
</ContextMenu.Trigger>
<ContextMenu.Portal>
Expand All @@ -36,10 +40,37 @@ export default ({ text, href }: ContextCardProps) => {
<ContextMenu.Item className='text-gray-500 w-full hover:bg-blue-500 px-1 py-1 rounded-md hover:text-white hover:cursor-pointer'>
Open
</ContextMenu.Item>
<ContextMenu.Item className='text-gray-500 w-full hover:bg-blue-500 px-1 py-1 rounded-md hover:text-white hover:cursor-pointer'>
<ContextMenu.Item
onClick={async () =>
await client.queries
.deleteFile({
relativePath: file._sys.basename,
})
.then(() => router.refresh())
}
className='text-gray-500 w-full hover:bg-blue-500 px-1 py-1 rounded-md hover:text-white hover:cursor-pointer'
>
Delete
</ContextMenu.Item>
<ContextMenu.Item className='text-gray-500 w-full hover:bg-blue-500 px-1 py-1 rounded-md hover:text-white hover:cursor-pointer'>
<ContextMenu.Item
onClick={async () =>
await client.queries
.duplicateFile({
relativePath: '/aaa1.mdx',
params: {
file: {
name: `${file.name}`,
entity:
'content/entities/audits/ÖBB_PV 27001 z 2024.json',
skeleton: file.skeleton,
language: file.language,
},
},
})
.then(() => router.refresh())
}
className='text-gray-500 w-full hover:bg-blue-500 px-1 py-1 rounded-md hover:text-white hover:cursor-pointer'
>
Duplicate
</ContextMenu.Item>
</ContextMenu.Content>
Expand Down
19 changes: 8 additions & 11 deletions components/Files.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
'use client';
import type { FileLinkInfo } from '@/types/index';
import type { File } from '@/tina/__generated__/types';
import { fileInEditMode } from '@/utils/path';
import { Grid, Link } from '@radix-ui/themes';
import { uniqueUuid } from 'docx';
import ContextCard from './Card/ContextCard';
import NewFileCard from './Card/NewFileCard';

type FilesProps = {
files: FileLinkInfo[];
files: File[];
};

export default ({ files }: FilesProps) => {
return (
<Grid columns='3' gap='3'>
<NewFileCard />

{files.map((item, i) => (
{files.map((file) => (
<Link
className={item.name === 'test' ? 'hidden' : ''}
title={`Go to file ${item.name}`}
className={file.name === 'test' ? 'hidden' : ''}
title={`Go to file ${file.name}`}
style={{ textDecoration: 'none' }}
data-testid={`${item.name}`}
href={fileInEditMode(item.link)}
data-testid={`${file.name}`}
href={fileInEditMode(file._sys.filename)}
key={uniqueUuid()}
>
<ContextCard
text={item.name ?? item.link}
href={fileInEditMode(item.link)}
/>
<ContextCard file={file} />
</Link>
))}
</Grid>
Expand Down
1 change: 1 addition & 0 deletions tina/picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Checkbox from '@radix-ui/react-checkbox';
import { CheckIcon } from '@radix-ui/react-icons';
import { Card, Text } from '@radix-ui/themes';
import { uniqueUuid } from 'docx';
import React from 'react';
import { wrapFieldsWithMeta } from 'tinacms';
import { useTinaQuery } from '../../app/api/tina/hook';
import pickerMapper from './pickerMapper';
Expand Down
12 changes: 11 additions & 1 deletion tina/queries/main.gql
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ mutation deleteFile($relativePath: String!) {
deleteDocument(collection: "file", relativePath: $relativePath) {
__typename
}
}
}

mutation duplicateFile($relativePath: String!, $params: DocumentMutation!) {
createDocument(
collection: "file"
relativePath: $relativePath
params: $params
) {
__typename
}
}
5 changes: 0 additions & 5 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import type { IPatch } from 'docx';
export type Skeleton = `/uploads/skeletons/${string}.docx`;
export type Placeholders = `{{field_${string}}}`[];

export type FileLinkInfo = {
link: string;
name: string;
};

export type DocxStringConversionResult = {
value: string;
messages: {
Expand Down

0 comments on commit 304bd97

Please sign in to comment.