Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL encoded collection data for grant rounds #1786

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ node_modules
.DS_Store
/lerna-debug.log
.turbo
.vercel
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"api-build": "turbo run build --filter=api",
"api-test": "turbo run test --filter=api",
"api-start": "pnpm --filter api run start",
"api-gen": "pnpm --filter api run prisma:generate",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API folder is not used anymore so this might be just a leftover from git.

"lint-staged": "lint-staged",
"lint:builder": "pnpm --filter=builder run lint"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/grant-explorer/package.json
Expand Up @@ -63,12 +63,15 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.0.1",
"@testing-library/user-event": "^14.1.1",
"@types/crypto-js": "^4.1.1",
"@types/uuid": "^9.0.1",
"@wagmi/core": "0.5.4",
"allo-indexer-client": "github:gitcoinco/allo-indexer-client#48490969985125e89bf1d65725ef9e510c97256a",
"babel-loader": "^8.3.0",
"buffer": "^6.0.3",
"common": "workspace:*",
"crypto-browserify": "^3.12.0",
"crypto-js": "^4.1.1",
"date-fns": "^2.29.3",
"eslint": ">=8.1.0 <9.0.0",
"ethers": "^5.6.5",
Expand Down Expand Up @@ -100,7 +103,9 @@
"swr": "^2.0.0",
"typescript": "^4.6.0",
"url": "^0.11.0",
"usehooks-ts": "^2.9.1",
"util": "^0.12.4",
"uuid": "^9.0.0",
"wagmi": "0.6.8",
"web-vitals": "^2.1.0",
"webpack": ">=2"
Expand All @@ -123,6 +128,7 @@
"@types/react-gtm-module": "^2.0.1",
"@types/testing-library__jest-dom": "^5.14.5",
"autoprefixer": "^10.4.7",
"craco-esbuild": "0.5.2",
vacekj marked this conversation as resolved.
Show resolved Hide resolved
"eslint-config-gitcoin": "workspace:*",
"jest-localstorage-mock": "^2.4.22",
"postcss": "^8.4.14",
Expand Down
76 changes: 76 additions & 0 deletions packages/grant-explorer/public/empty-state.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/grant-explorer/src/assets/icons/bookmark.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/grant-explorer/src/assets/icons/check.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/grant-explorer/src/assets/icons/minus.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/grant-explorer/src/assets/icons/plus.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/grant-explorer/src/assets/icons/star.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions packages/grant-explorer/src/features/api/collections.ts
@@ -0,0 +1,90 @@
import { v4 as uuid } from 'uuid'
import CryptoJS from 'crypto-js'

export type Collection = {
title: string
id: string
enc?: string
owner: `0x${string}`
round: string
projects: number[]
}

type CreateCollectionParams = {
title: string
owner: `0x${string}`
round: string
projects: number[]
}

const secretKey = 'grantProjects'

const parseCollections = (): string[] => {
return JSON.parse(localStorage.getItem(`collections`) || '[]')
}

export const encodeCollection = (collection: string) => {
return CryptoJS.AES.encrypt(collection, secretKey).toString()
}

export const createCollection = async ({ title, owner, round, projects }: CreateCollectionParams): Promise<void> => {
const id = `collections_${round}_${uuid()}`

localStorage.setItem(
id,
JSON.stringify({
id,
title,
owner,
round,
projects,
})
)

localStorage.setItem('collections', JSON.stringify(Array.from(new Set(parseCollections()).add(id))))

document.dispatchEvent(new Event('collectionsUpdated'))
}

export const getCollectionById = async (id: string): Promise<Collection> => {
// decrypt data
console.log(id)
const collection = JSON.parse(CryptoJS.AES.decrypt(id, secretKey).toString(CryptoJS.enc.Utf8))
console.log(collection)
// load collection data from id

return collection as Collection
}

export const getAllCollectionsForRound = (round: string): string[] => {
return parseCollections().filter((i) => {
const [, roundId] = i.split('_')
return roundId === round
})
}

export const getAllCollections = async (round: string): Promise<Collection[]> => {
return parseCollections()
.filter((i) => {
const [, roundId] = i.split('_')
return roundId === round
})
.map((collectionKey) => JSON.parse(localStorage.getItem(collectionKey) || '[]'))
.reverse()
}

export const getMyCollections = async (round: string): Promise<Collection[]> => {
return getAllCollections(round)
}

export type AddProjectData = {
collection: string
project: number
}

export const addProjectToCollection = async ({ collection, project }: AddProjectData) => {
const collectionData = JSON.parse(localStorage.getItem(collection) || '[]')
collectionData.projects = Array.from(new Set(collectionData.projects).add(project))

localStorage.setItem(collection, JSON.stringify(collectionData))
}