Skip to content

Commit

Permalink
fix regex issues, media carousel
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanlesich committed Aug 6, 2024
1 parent 86102f6 commit 4e57dae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/components/forms/MediaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const MediaForm = ({ form }: { form: UseFormReturnType<any> }) => {
placeholder="https://image-hosting/id.png"
{...form.getInputProps(`showcaseLinks.${i}.url`)}
error={link.mediaType === MediaType.Unknown ? 'Invalid link' : null}
onBlur={(e) => {
onChange={(e) => {
form.getInputProps(`showcaseLinks.${link.id}`).onBlur(e);
handleLinkChange(link.id, e.currentTarget.value);
}}
Expand Down
3 changes: 0 additions & 3 deletions src/components/grant/MilestonesDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Box,
Button,
Group,
NumberInput,
Stack,
Text,
TextInput,
Expand Down Expand Up @@ -293,8 +292,6 @@ export const MilestonesDrawer = ({
label={`Percentage`}
name={`milestone-perc-${index + 1}`}
type="number"
min={1}
step={1}
placeholder="30"
w={'48%'}
value={formData[`milestone-perc-${index + 1}`]}
Expand Down
34 changes: 27 additions & 7 deletions src/utils/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export type ShowcaseLink = {
mediaType: MediaType;
};

const youtubeRegex =
/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
const youtubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/.*$/i;
const extractYoutubeIdRegex =
/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;

const vimeoRegex =
/(?:https?:\/\/)?(?:www\.)?vimeo\.com(?:\/manage\/videos)?\/(\d+)\/([a-zA-Z0-9]+)(?:\?.*)?$/;
Expand All @@ -40,14 +41,15 @@ export const detectMediaTypeFromUrl = (url: string): MediaType => {
};

const transformYoutubeUrl = (url: string) => {
const match = url.match(youtubeRegex);
const match = url.match(extractYoutubeIdRegex);
console.log('match', match);

if (match && match[2].length === 11) {
const videoId = match[2];
if (match && match[1]?.length === 11) {
const videoId = match[1];
return `https://www.youtube.com/embed/${videoId}?rel=0&modestBranding=1&showinfo=0&controls=0&title=0`;
}

return null;
return url;
};

const transformVimeoUrl = (url: string) => {
Expand All @@ -60,14 +62,16 @@ const transformVimeoUrl = (url: string) => {
return `https://player.vimeo.com/video/${videoId}?h=${h_id}&autopause=0&player_id=0&byline=0&title=0&transparent=1&autoplay=1&dnt=1&controls=0&background=1`;
}

return null;
return url;
};

export const parseShowcaseLink = (
link: string
): { url: string | null; mediaType: MediaType } => {
const mediaType = detectMediaTypeFromUrl(link);

console.log('mediaType', mediaType);

if (mediaType === MediaType.Youtube) {
return {
url: transformYoutubeUrl(link),
Expand All @@ -87,3 +91,19 @@ export const parseShowcaseLink = (
mediaType,
};
};

const testPNG = youtubeRegex.test(
'https://presskit.manada.dev/images/cloudlines/0000screenshot.png'
);

const youtubeURLTest = youtubeRegex.test(
'https://www.youtube.com/watch?v=Sg-G1E1UwAY'
);

const youtubeShareTest = youtubeRegex.test(
'https://youtu.be/Sg-G1E1UwAY?feature=shared'
);

console.log('test PNG', testPNG);
console.log('youtubeTest', youtubeURLTest);
console.log('youtubeShareTest', youtubeShareTest);

0 comments on commit 4e57dae

Please sign in to comment.