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

fix(feed): Parse MusicAudio and Video posts metadata on Map Feed #1400

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
6 changes: 4 additions & 2 deletions packages/components/socialFeed/Map/Map.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ export const Map: FC<MapProps> = ({
const markers: MarkerPopup[] = useMemo(() => {
if (!posts) return [];
const results: MarkerPopup[] = [];
posts.forEach((post, index) => {
posts.forEach((post) => {
const metadata = zodTryParseJSON(
ZodSocialFeedPostMetadata,
ZodSocialFeedPostMetadata.partial().merge(
ZodSocialFeedPostMetadata.pick({ title: true }),
Copy link
Collaborator

@WaDadidou WaDadidou Nov 20, 2024

Choose a reason for hiding this comment

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

You don't need to modify the Zod type to try parse.
Yes, ZodSocialFeedPostMetadata shape can be wrong, but we have made other shapes for other posts categories. So we need to handle them.

Reformulation: The issue is due to the fact we have 4 differents types for the fetched posts.
Here we only try to parse ZodSocialFeedPostMetadata. So, the tracks and videos posts validation fails.

The result here has no success: https://github.com/TERITORI/teritori-dapp/blob/bugfix/video-audio-map/packages/utils/sanitize.ts#L22
If you console.log the result, you will see what's wrong to validate ZodSocialFeedPostMetadata

To avoid that, because the map gathers all posts categories, we must handle these Zod types. Here is my suggestion:

const videoPostMetadata = zodTryParseJSON(
  ZodSocialFeedVideoMetadata,
  post.metadata,
);
const trackMetadata = zodTryParseJSON(
  ZodSocialFeedTrackMetadata,
  post.metadata,
);
const articleMetadata = zodTryParseJSON(
  ZodSocialFeedArticleMetadata,
  post.metadata,
);
const postMetadata = zodTryParseJSON(
  ZodSocialFeedPostMetadata,
  post.metadata,
);
const metadataToUse = videoPostMetadata || trackMetadata || articleMetadata || postMetadata;

We need a refacto about it. Either unify all types in one, either add an utilitary function to handle (try parse) these differents zod types

Copy link
Collaborator

@n0izn0iz n0izn0iz Nov 21, 2024

Choose a reason for hiding this comment

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

actually no there is the zodSocialFeedCommonMetadata schema when you need to only parse the title

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have made a fix for this discussion in this commit: 3a16113

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

actually no there is the zodSocialFeedCommonMetadata schema when you need to only parse the title

In this case, there are other things that i need to parse also based on the category type, so I have created a util function to handle those cases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

you can switch on the post type to do the proper parsing in each case, the whole point of using zod is to have type safety, using a z.ZodTypeAny entirely defeat this purpose

Copy link
Collaborator Author

@sujal-into sujal-into Nov 21, 2024

Choose a reason for hiding this comment

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

I have used switch and removed the ZodTypeAny : ece2bac

),
post.metadata,
);
if (!metadata?.location) return;
Expand Down
Loading