Skip to content

Commit

Permalink
fix: move addToURL to client side code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dun-sin committed Oct 6, 2024
1 parent bc917cf commit c5a71dd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/components/DiscoverTracks/SubmitButtion.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use client';

import {
addToUrl,
extractPlaylistId,
getAllTracks,
getEveryAlbum,
Expand All @@ -25,6 +24,7 @@ import { useLoading } from '@/app/context/loadingContext';
import { useOptions } from '@/app/context/optionsContext';
import { useType } from '@/app/context/DiscoverTracks/typeContext';
import { toast } from 'react-toastify';
import { addToUrl } from '@/app/lib/clientUtils';

const API_KEY = process.env.NEXT_PUBLIC_API_KEY;
const genAI = new GoogleGenerativeAI(API_KEY as string);
Expand Down
3 changes: 2 additions & 1 deletion app/components/ResultLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import React, { useEffect } from 'react';

import OpenOnSpotify from './OpenOnSpotify';
import { addPlaylistFullLinkFromID, getFromUrl } from '../lib/utils';
import { addPlaylistFullLinkFromID } from '../lib/utils';
import { useGeneralState } from '@/app/context/generalStateContext';
import { useLoading } from '@/app/context/loadingContext';
import { getFromUrl } from '../lib/clientUtils';

const ResultLink = () => {
const { loading } = useLoading();
Expand Down
25 changes: 25 additions & 0 deletions app/lib/clientUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use client';

export const addToUrl = (key: string, value: string) => {
try {
const searchParams = new URLSearchParams(window.location.search);
searchParams.set(key, value);
const newUrl = `${window.location.pathname}?${searchParams.toString()}`;
window.history.pushState({}, '', newUrl);
} catch (error) {
console.log('Error adding to url', error);
}
};

export const copyToClipboard = async (textToCopy: string) => {
if ('clipboard' in navigator) {
return await navigator.clipboard.writeText(textToCopy);
} else {
return document.execCommand('copy', true, textToCopy);
}
};

export const getFromUrl = (key: string) => {
const searchParams = new URLSearchParams(window.location.search);
return searchParams.get(key);
};
21 changes: 0 additions & 21 deletions app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,8 @@ export const convertToSubArray = (albums: string[]) => {
return subArrays;
};

export const copyToClipboard = async (textToCopy: string) => {
if ('clipboard' in navigator) {
return await navigator.clipboard.writeText(textToCopy);
} else {
return document.execCommand('copy', true, textToCopy);
}
};

export const addToUrl = (key: string, value: string) => {
try {
const searchParams = new URLSearchParams(window.location.search);
searchParams.set(key, value);
const newUrl = `${window.location.pathname}?${searchParams.toString()}`;
window.history.pushState({}, '', newUrl);
} catch (error) {
console.log('Error adding to url', error);
}
};

export const getFromUrl = (key: string) => {
const searchParams = new URLSearchParams(window.location.search);
return searchParams.get(key);
};

export const formatDate = (date: Date) => {
const day = String(date.getDate()).padStart(2, '0');
Expand Down

0 comments on commit c5a71dd

Please sign in to comment.