|
| 1 | +import React from 'react'; |
| 2 | +import { Element, Text } from '@codesandbox/components'; |
| 3 | +import { useQuery } from '@apollo/react-hooks'; |
| 4 | + |
| 5 | +import isSameWeek from 'date-fns/isSameWeek'; |
| 6 | +import { |
| 7 | + DeletedSandboxesQueryVariables, |
| 8 | + DeletedSandboxesQuery, |
| 9 | +} from 'app/graphql/types'; |
| 10 | +import { SandboxCard } from '../../../Components/SandboxCard'; |
| 11 | +import { DELETED_SANDBOXES_CONTENT_QUERY } from '../../../queries'; |
| 12 | + |
| 13 | +export const Deleted = () => { |
| 14 | + const { data, error, loading } = useQuery< |
| 15 | + DeletedSandboxesQuery, |
| 16 | + DeletedSandboxesQueryVariables |
| 17 | + >(DELETED_SANDBOXES_CONTENT_QUERY, { |
| 18 | + fetchPolicy: 'cache-and-network', |
| 19 | + }); |
| 20 | + |
| 21 | + if (error) { |
| 22 | + return <Text>Error</Text>; |
| 23 | + } |
| 24 | + |
| 25 | + if (loading) { |
| 26 | + return <Text>loading</Text>; |
| 27 | + } |
| 28 | + |
| 29 | + const sandboxes = data && data.me && data.me.sandboxes; |
| 30 | + const orderedSandboxes = sandboxes.reduce( |
| 31 | + (accumulator, currentValue) => { |
| 32 | + if (isSameWeek(new Date(currentValue.removedAt), new Date())) { |
| 33 | + accumulator.week.push(currentValue); |
| 34 | + |
| 35 | + return accumulator; |
| 36 | + } |
| 37 | + |
| 38 | + accumulator.older.push(currentValue); |
| 39 | + |
| 40 | + return accumulator; |
| 41 | + }, |
| 42 | + { |
| 43 | + week: [], |
| 44 | + older: [], |
| 45 | + } |
| 46 | + ); |
| 47 | + |
| 48 | + return ( |
| 49 | + <Element> |
| 50 | + <Text marginBottom={2} block> |
| 51 | + Recently Deleted |
| 52 | + </Text> |
| 53 | + <Text variant="muted" block marginBottom={11}> |
| 54 | + Sandboxes, Templates and Folders are permanently deleted after 30 days{' '} |
| 55 | + </Text> |
| 56 | + {orderedSandboxes.week.length && ( |
| 57 | + <Element marginBottom={14}> |
| 58 | + <Text marginBottom={6} block> |
| 59 | + Archived this week |
| 60 | + </Text> |
| 61 | + {orderedSandboxes.week.map(sandbox => ( |
| 62 | + <SandboxCard sandbox={sandbox} key={sandbox.id} /> |
| 63 | + ))} |
| 64 | + </Element> |
| 65 | + )} |
| 66 | + {orderedSandboxes.older.length && ( |
| 67 | + <> |
| 68 | + <Text marginBottom={6} block> |
| 69 | + Archived Earlier |
| 70 | + </Text> |
| 71 | + {orderedSandboxes.older.map(sandbox => ( |
| 72 | + <SandboxCard sandbox={sandbox} key={sandbox.id} /> |
| 73 | + ))} |
| 74 | + </> |
| 75 | + )} |
| 76 | + </Element> |
| 77 | + ); |
| 78 | +}; |
0 commit comments