From 1d0b6e91603e3346459a2cda24974b3a4ac90f38 Mon Sep 17 00:00:00 2001 From: Malik Zulqurnain <51641047+mzparacha@users.noreply.github.com> Date: Thu, 8 Feb 2024 21:11:50 +0500 Subject: [PATCH] Fix snapshots validation and links array component (#6613) * Fixed snapshots link validation * Fixed webhooks disabled button --------- Co-authored-by: Jake Naviasky --- .../views/components/LinksArray/types.ts | 2 +- .../components/LinksArray/useLinksArray.tsx | 4 ---- .../Integrations/Snapshots/Snapshots.tsx | 21 ++++++++++++++-- .../Integrations/Snapshots/validation.ts | 24 ++++++++++++------- .../Integrations/Webhooks/Webhooks.tsx | 10 +++++++- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/packages/commonwealth/client/scripts/views/components/LinksArray/types.ts b/packages/commonwealth/client/scripts/views/components/LinksArray/types.ts index 37cd05b135d..b2a7cbecd10 100644 --- a/packages/commonwealth/client/scripts/views/components/LinksArray/types.ts +++ b/packages/commonwealth/client/scripts/views/components/LinksArray/types.ts @@ -42,5 +42,5 @@ export type LinksArrayProps = { export type LinksArrayHookProps = { initialLinks: Link[]; - linkValidation?: z.ZodEffects | z.ZodString; + linkValidation?: z.ZodTypeAny; }; diff --git a/packages/commonwealth/client/scripts/views/components/LinksArray/useLinksArray.tsx b/packages/commonwealth/client/scripts/views/components/LinksArray/useLinksArray.tsx index 91f6e04d8ad..d5cc1a1d60f 100644 --- a/packages/commonwealth/client/scripts/views/components/LinksArray/useLinksArray.tsx +++ b/packages/commonwealth/client/scripts/views/components/LinksArray/useLinksArray.tsx @@ -41,10 +41,6 @@ const useLinksArray = ({ }; const onLinkUpdatedAtIndex = (updatedLink: Link, index: number) => { - const splitLink = updatedLink.value.split('/'); - const sanitizedLink = splitLink[splitLink.length - 1]; - updatedLink.value = sanitizedLink; - const updatedLinks = [...links]; updatedLinks[index] = { ...updatedLink, diff --git a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/Snapshots.tsx b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/Snapshots.tsx index d432939d55a..91dc55ea379 100644 --- a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/Snapshots.tsx +++ b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/Snapshots.tsx @@ -1,6 +1,7 @@ import { notifySuccess } from 'controllers/app/notifications'; import React, { useState } from 'react'; import app from 'state'; +import _ from 'underscore'; import { LinksArray, useLinksArray } from 'views/components/LinksArray'; import { CWText } from 'views/components/component_kit/cw_text'; import { CWButton } from 'views/components/component_kit/new_designs/cw_button'; @@ -33,7 +34,18 @@ const Snapshots = () => { if (!areLinksValid()) return; try { - const newSnapshots = [...new Set(snapshots.map((x) => x.value))]; + // get unique snapshot names from links (if any value in array was link) + const newSnapshots = [ + ...new Set( + snapshots + .map((x) => x.value) + .map((link) => { + const splitLink = link.split('/'); + const sanitizedLink = splitLink[splitLink.length - 1]; + return sanitizedLink; + }), + ), + ]; await community.updateChainData({ snapshot: newSnapshots, }); @@ -80,7 +92,12 @@ const Snapshots = () => { x.value.trim())].sort((a, b) => + a.localeCompare(b), + ), + [...(community.snapshot || [])].sort((a, b) => a.localeCompare(b)), + )} onClick={onSaveChanges} /> ) : ( diff --git a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/validation.ts b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/validation.ts index bedce80988d..7f1b089eeac 100644 --- a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/validation.ts +++ b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Snapshots/validation.ts @@ -1,13 +1,19 @@ import z from 'zod'; -const snapshotValidationSchema = z.string().refine( - (space) => { - const extension = space.slice(space.length - 4); - return extension === '.eth' || extension === '.xyz'; - }, - { - message: 'Snapshot name must be in the form of *.eth or *.xyz', - }, -); +const snapshotNameSchema = z.string().regex(/^[a-zA-Z0-9-.]+\.((xyz)|(eth))$/, { + message: 'Snapshot must be valid, and end in *.eth or *.xyz', +}); +const snapshotLinkSchema = z + .string() + .regex( + /^https:\/\/(\w+\.)?snapshot\.org\/#\/[a-zA-Z0-9-.]+\.((xyz)|(eth))$/, + { + message: 'Snapshot link be valid, and end in *.eth or *.xyz', + }, + ); +const snapshotValidationSchema = z.union([ + snapshotNameSchema, + snapshotLinkSchema, +]); export { snapshotValidationSchema }; diff --git a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Webhooks/Webhooks.tsx b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Webhooks/Webhooks.tsx index 3556af3e19f..a36c09f11fd 100644 --- a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Webhooks/Webhooks.tsx +++ b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Integrations/Webhooks/Webhooks.tsx @@ -12,6 +12,7 @@ import { useEditWebhookMutation, useFetchWebhooksQuery, } from 'state/api/webhooks'; +import _ from 'underscore'; import { LinksArray, useLinksArray } from 'views/components/LinksArray'; import { CWText } from 'views/components/component_kit/cw_text'; import { CWModal } from 'views/components/component_kit/new_designs/CWModal'; @@ -197,7 +198,14 @@ const Webhooks = () => { x.value.trim())].sort((a, b) => + a.localeCompare(b), + ), + [...(existingWebhooks || []).map((x) => x.url.trim())].sort( + (a, b) => a.localeCompare(b), + ), + )} onClick={onSaveChanges} /> ) : (