Skip to content

Commit

Permalink
refactor: Remove duplicate code and improve organization in EntryPage…
Browse files Browse the repository at this point in the history
… component
  • Loading branch information
bramses committed Sep 10, 2024
1 parent 5ed7c10 commit 81f3448
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 66 deletions.
133 changes: 67 additions & 66 deletions src/components/EntryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,72 +314,6 @@ const EntryPage = () => {
'Loading...'
)}

{(hasAliases || temporaryAliases.length > 0) && (
<h2 className="my-4 text-4xl font-extrabold">Comments</h2>
)}

{temporaryAliases.length > 0 && (
<div>
{temporaryAliases.map((alias) => (
<div key={alias} className="mb-4 flex flex-col items-start">
<div className="flex items-center">
<div className="mr-2 flex size-6 shrink-0 items-center justify-center rounded-full bg-gray-300 text-xs font-bold text-white">
{firstLastName.firstName && firstLastName.lastName ? (
<>
{firstLastName.firstName[0]}
{firstLastName.lastName[0]}
</>
) : (
'YCB'
)}
</div>
<button
className="text-black hover:underline"
type="button"
onClick={() => toDashboard(alias)}
>
{alias}
</button>
</div>
<span className="text-sm text-gray-500">
Added to yCb: {new Date().toLocaleString()}
</span>
</div>
))}
</div>
)}

{hasAliases && (
<div>
{data?.metadata?.aliasData?.map((alias: any) => (
<div key={alias.aliasId} className="mb-4 flex flex-col items-start">
<div className="flex items-center">
<div className="mr-2 flex size-6 shrink-0 items-center justify-center rounded-full bg-gray-300 text-xs font-bold text-white">
{firstLastName.firstName && firstLastName.lastName ? (
<>
{firstLastName.firstName[0]}
{firstLastName.lastName[0]}
</>
) : (
'YCB'
)}
</div>
<button
className="text-black hover:underline"
type="button"
onClick={() => toDashboard(alias.aliasData)}
>
{alias.aliasData}
</button>
</div>
<span className="text-sm text-gray-500">
Added to yCb: {alias.aliasCreatedAt}
</span>
</div>
))}
</div>
)}

<h2 className="my-4 text-4xl font-extrabold">Add Comment</h2>
<div className="">
<textarea
Expand Down Expand Up @@ -509,6 +443,73 @@ const EntryPage = () => {
</button>
)}
</div>

{(hasAliases || temporaryAliases.length > 0) && (
<h2 className="my-4 text-4xl font-extrabold">Comments</h2>
)}

{temporaryAliases.length > 0 && (
<div>
{temporaryAliases.map((alias) => (
<div key={alias} className="mb-4 flex flex-col items-start">
<div className="flex items-center">
<div className="mr-2 flex size-6 shrink-0 items-center justify-center rounded-full bg-gray-300 text-xs font-bold text-white">
{firstLastName.firstName && firstLastName.lastName ? (
<>
{firstLastName.firstName[0]}
{firstLastName.lastName[0]}
</>
) : (
'YCB'
)}
</div>
<button
className="text-black hover:underline"
type="button"
onClick={() => toDashboard(alias)}
>
{alias}
</button>
</div>
<span className="text-sm text-gray-500">
Added to yCb: {new Date().toLocaleString()}
</span>
</div>
))}
</div>
)}

{hasAliases && (
<div>
{data?.metadata?.aliasData?.map((alias: any) => (
<div key={alias.aliasId} className="mb-4 flex flex-col items-start">
<div className="flex items-center">
<div className="mr-2 flex size-6 shrink-0 items-center justify-center rounded-full bg-gray-300 text-xs font-bold text-white">
{firstLastName.firstName && firstLastName.lastName ? (
<>
{firstLastName.firstName[0]}
{firstLastName.lastName[0]}
</>
) : (
'YCB'
)}
</div>
<button
className="text-black hover:underline"
type="button"
onClick={() => toDashboard(alias.aliasData)}
>
{alias.aliasData}
</button>
</div>
<span className="text-sm text-gray-500">
Added to yCb: {alias.aliasCreatedAt}
</span>
</div>
))}
</div>
)}

{showAliasError && (
<div className="text-red-500">Error adding alias. Try again.</div>
)}
Expand Down
46 changes: 46 additions & 0 deletions src/helpers/favicon_cahce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const CACHE_KEY = 'faviconCache';
const CACHE_EXPIRATION = 10 * 60 * 1000 * 60; // 10 hours in milliseconds

export const getFaviconCache = () => {
if (typeof window === 'undefined' || typeof localStorage === 'undefined') {
return { favicon: {} }; // Check for client-side and localStorage
}
const cache = localStorage.getItem(CACHE_KEY);
if (cache) {
const parsedCache = JSON.parse(cache);
if (Date.now() - parsedCache.timestamp < CACHE_EXPIRATION) {
return parsedCache.data;
}
}
return { favicon: {} };
};

export const setFaviconCache = (data: any) => {
if (typeof window === 'undefined' || typeof localStorage === 'undefined')
return; // Check for client-side and localStorage
const cache = {
timestamp: Date.now(),
data,
};
localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
};

export const appendFaviconToCache = (query: string, url: any) => {
const cache = getFaviconCache();

cache.favicon[query] = url;

setFaviconCache(cache);
};

export const invalidateFaviconFromCache = (query: string) => {
const cache = getFaviconCache();

delete cache.favicon[query];

setFaviconCache(cache);
};

export const clearFaviconCache = () => {
localStorage.removeItem(CACHE_KEY);
};

0 comments on commit 81f3448

Please sign in to comment.