Skip to content

Commit

Permalink
chore: Update environment variables and API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Aug 14, 2024
1 parent bdc39be commit 14079be
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ NEXT_PUBLIC_SENTRY_DSN=
# Logging ingestion
# LOGTAIL_SOURCE_TOKEN=
######## [END] SENSITIVE DATA

DATABASE_URL='/var/data/db1.db'
CLOUD_URL="https://api-gateway-electron.onrender.com"

35 changes: 35 additions & 0 deletions src/app/[locale]/(auth)/api/fetch/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NextResponse } from 'next/server';

import { logger } from '@/libs/Logger';

// import env variables

export const POST = async (request: Request) => {
const { id } = await request.json();
const { CLOUD_URL, DATABASE_URL } = process.env;

const resp = await fetch(`${CLOUD_URL}/fetch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: id.toString(),
dbPath: DATABASE_URL,
}),
});
logger.info('resp:', resp);
const data = await resp.json();

try {
logger.info(`A new fetch has been created ${JSON.stringify(data)}`);

return NextResponse.json({
data,
});
} catch (error) {
logger.error(error, 'An error occurred while creating a search');

return NextResponse.json({}, { status: 500 });
}
};
9 changes: 6 additions & 3 deletions src/app/[locale]/(auth)/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { NextResponse } from 'next/server';

import { logger } from '@/libs/Logger';

// import env variables

export const POST = async (request: Request) => {
const { query, dbPath } = await request.json();
const { query } = await request.json();
const { CLOUD_URL, DATABASE_URL } = process.env;

const resp = await fetch('https://api-gateway-electron.onrender.com/search', {
const resp = await fetch(`${CLOUD_URL}/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
dbPath,
dbPath: DATABASE_URL,
}),
});

Expand Down
10 changes: 5 additions & 5 deletions src/components/Entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const Entries = ({ searchResults, onDelve }: EntriesProps) => {
<div>
{searchResults.map((entry: any) => (
<Entry
key={entry.id}
data={
'_display' in entry.metadata ? entry.metadata._display : entry.data
}
key={'aliasData' in entry ? Math.random() * 1000 : entry.id} // Replace 'Math.random() * 1000' with a more specific key if possible
data={entry.data}
aliases={'aliasData' in entry ? entry.aliasData : []}
selectedIndex={'selectedIndex' in entry ? entry.selectedIndex : -1}
title={entry.metadata.title}
author={entry.metadata.author}
createdAt={entry.createdAt}
Expand Down Expand Up @@ -61,7 +61,7 @@ const Entries = ({ searchResults, onDelve }: EntriesProps) => {
: ''
}
onDelve={onDelve}
hasAliases={'_display' in entry.metadata}
hasAliases={'aliasData' in entry}
/>
))}
</div>
Expand Down
77 changes: 47 additions & 30 deletions src/components/Entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const Entry = ({
imageUrl = '',
hasImage = false,
onDelve = (_: string) => {},
aliases = [],
selectedIndex = -1,
}) => {
const [hasAliases] = useState(initialHasAliases);
// const [hasCollections] = useState(initialHasCollections);
Expand Down Expand Up @@ -72,37 +74,52 @@ const Entry = ({
<h2 className="mb-2 text-lg font-semibold text-gray-900 dark:text-white">
Aliases:
</h2>
{/* {
"id": "2449",
"data": "me at a hot ones interview",
"metadata": {
"title": "bingus ✩ on X: \"https://t.co/G8jSTYplMv\" / X",
"author": "https://x.com/aliaoftheblade/status/1822602088427487358",
"alias_ids": [
2450,
2451,
2517
],
"s_id": 2449
},
"createdAt": "2024-08-12T01:56:14.569Z",
"updatedAt": "2024-08-12T01:56:14.569Z",
"aliasData": [
"dune meme",
"hot ones meme",
"hot ones dune"
],
"selectedIndex": 1
}
map over aliasData and display each alias in a list and put a (*) next to the selected index
and turn each into a link that searches for that alias
*/}
<ul className="max-w-md list-inside list-disc space-y-1 text-gray-500 dark:text-gray-400">
<li>
<button
onClick={() => {}}
className="font-medium text-blue-600 underline hover:text-blue-700 hover:no-underline dark:text-blue-500 dark:hover:text-blue-600"
type="button"
>
atrioc
</button>
</li>
<li>
<span>
<button
onClick={() => {}}
className="font-medium text-blue-600 underline hover:text-blue-700 hover:no-underline dark:text-blue-500 dark:hover:text-blue-600"
type="button"
>
japan
</button>
(*)
</span>
</li>
<li>
<button
onClick={() => {}}
className="font-medium text-blue-600 underline hover:text-blue-700 hover:no-underline dark:text-blue-500 dark:hover:text-blue-600"
type="button"
>
the collapsing us stock market 2024
</button>
</li>
{aliases.map((alias, index) => (
<li key={alias}>
{index === selectedIndex ? (
<strong className="font-semibold text-gray-900 dark:text-white">
{alias} *
</strong>
) : (
<button
type="button"
className="font-normal text-blue-600 hover:underline"
onClick={() => {
// search using alias
onDelve(alias);
}}
>
{alias}
</button>
)}
</li>
))}
</ul>

<hr className="mx-auto my-4 h-1 w-48 rounded border-0 bg-gray-100 md:my-10 dark:bg-gray-700" />
Expand Down
120 changes: 115 additions & 5 deletions src/components/SearchBox.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
/* eslint-disable no-console */

'use client';

import { useState } from 'react';

import Entries from './Entries';

const SearchBox = () => {
const [searchResults, setSearchResults] = useState([]);
const [searchResults, setSearchResults] = useState<any[]>([]);
const [textAreaValue, setTextAreaValue] = useState('');

const fetchByID = async (id: string) => {
try {
const response = await fetch('/api/fetch', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id,
}),
});
const data = await response.json();

return data;
} catch (error) {
console.error('Error fetching entry by ID:', error);
return {};
}
};

const fetchSearchResults = async (query: string) => {
try {
const response = await fetch('/api/search', {
Expand All @@ -17,9 +39,6 @@ const SearchBox = () => {
},
body: JSON.stringify({
query,
dbPath:
// '/Users/bram/Dropbox/PARA/Projects/api-gateway-local-build/api-gateway-electron/yourcommonbase.db',
'/var/data/db1.db',
}),
});
console.log('response:', response);
Expand All @@ -36,7 +55,98 @@ const SearchBox = () => {
}
});

setSearchResults(updatedData);
// for each entry, fetch the if metadata has a parent ID fetch the parent entry and its aliases and then fetch them
/*
ex:
{
"title": "Your Commonbase Dashboard",
"author": "",
"_display": "right after chq talk kind of depressed was looking fwd to this and a bit empty now its over in terms of next goal. also afraid of medicine that i promised myself id start taking",
"parent_id": 2575
}
fetchByID(2575).then((parentEntry) => {
"title": "Your Commonbase Dashboard",
"author": "",
"alias_ids": [
2580
]
}).then((parentEntry) => {
let aliasData = [];
for (let i = 0; i < parentEntry.alias_ids.length; i++) {
fetchByID(parentEntry.alias_ids[i]);
aliasData.push(aliasData.data);
}
updatedMetadataWAliases = {
...parentEntry,
aliasData: [...],
selectedIndex: (index of the alias entry that was in the first step)
*/

const updatedDataWithAliases = await Promise.all(
updatedData.map(async (entry: any) => {
if (entry.metadata.parent_id) {
try {
let selectedIndex = -1;

// Fetch parent entry by parent_id
const parentEntryRes = await fetchByID(entry.metadata.parent_id);
const parentEntry = parentEntryRes.data;

// Parse parent metadata
const parentMetadataJSON = JSON.parse(parentEntry.metadata);
parentEntry.metadata = parentMetadataJSON;

// Find the index of the current entry in the parent's alias_ids
if (
parentMetadataJSON.alias_ids &&
parentMetadataJSON.alias_ids.includes(Number(entry.id))
) {
selectedIndex = parentMetadataJSON.alias_ids.indexOf(
Number(entry.id),
);
}

// Fetch all alias entries by alias_ids
const aliasData = await Promise.all(
parentMetadataJSON.alias_ids.map(async (aliasId: string) => {
try {
const aliasEntryRes = await fetchByID(aliasId);
const aliasEntry = aliasEntryRes.data;
return aliasEntry.data;
} catch (aliasFetchError) {
console.error(
`Error fetching alias entry with ID ${aliasId}:`,
aliasFetchError,
);
throw aliasFetchError;
}
}),
);

// Return the combined entry with parent and alias data
return {
...parentEntry,
aliasData,
similarity: entry.similarity,
selectedIndex,
};
} catch (parentFetchError) {
console.error(
`Error fetching parent entry with ID ${entry.metadata.parent_id}:`,
parentFetchError,
);
throw parentFetchError;
}
}

return entry;
}),
);

console.log('Setting search results:', updatedDataWithAliases);
setSearchResults(updatedDataWithAliases);
} catch (error) {
console.error('Error fetching search results:', error);
}
Expand Down
2 changes: 2 additions & 0 deletions src/libs/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const Env = createEnv({
CLERK_SECRET_KEY: z.string().min(1),
DATABASE_URL: z.string().optional(),
LOGTAIL_SOURCE_TOKEN: z.string().optional(),
CLOUD_URL: z.string().optional(),
},
client: {
NEXT_PUBLIC_APP_URL: z.string().optional(),
Expand All @@ -26,5 +27,6 @@ export const Env = createEnv({
process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
NEXT_PUBLIC_CLERK_SIGN_IN_URL: process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL,
NODE_ENV: process.env.NODE_ENV,
CLOUD_URL: process.env.CLOUD_URL,
},
});

0 comments on commit 14079be

Please sign in to comment.