Skip to content

Commit

Permalink
support multiple buffer keys
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiasAurel committed Apr 30, 2022
1 parent 59397bb commit e02adc9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
13 changes: 9 additions & 4 deletions pages/api/buffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ const buffers = deta.Base("buffers");
type SaveNote = {
buffer: string;
key: string;
keys: string[]
};

export default async function fetchNotes(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "POST") {
const { key }: SaveNote = req.body;
const { keys }: SaveNote = req.body;
const fetchedBuffers = [];

try {
const fetchedBuffers = await (await buffers.fetch({ owner: key })).items;

res.json({ fetchedBuffers });
for (let i = 0; i < keys.length; i++) {
const fetchedBuffer = await (await buffers.fetch({ owner: keys[i] })).items;
fetchedBuffers.push(fetchedBuffer);
}
// console.log(fetchedBuffers);
res.json(fetchedBuffers);
} catch (err) {
res.json({
status: "Failed",
Expand Down
41 changes: 31 additions & 10 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";

import {
Note,
Tag,
Button,
Textarea,
Expand Down Expand Up @@ -36,7 +35,7 @@ const App: React.FC = (): JSX.Element => {
const { copy } = useClipboard();

function saveBuffer() {
const hashedKey = hashKey(secret);
const hashedKey = hashKey(secret.split(",")[0].trim());
toast.promise(
makeRequest(`/api/save`, { buffer, key: hashedKey }).then((_) =>
setBuffer("")
Expand All @@ -53,11 +52,18 @@ const App: React.FC = (): JSX.Element => {

async function refreshBuffers() {
const secret = localStorage.getItem("secret");
const hashedKey = hashKey(secret);
const newBuffers = await makeRequest("/api/buffers", { key: hashedKey });
const hashedKeys = secret.split(",")
.map(key => key.trim())
.map(key => hashKey(key));
const newBuffers = await makeRequest("/api/buffers", { keys: hashedKeys });
const clientBuffers: Array<string> = [];
newBuffers?.fetchedBuffers.forEach((buffer) =>
clientBuffers.push(buffer.buffer)
// console.log(newBuffers);
newBuffers?.forEach(fetchedBuffer => {
console.log(fetchedBuffer);
fetchedBuffer.forEach(buffer => {
clientBuffers.push(buffer.buffer);
});
}
);

/* console.log("clientBuffers", clientBuffers);
Expand All @@ -76,13 +82,21 @@ const App: React.FC = (): JSX.Element => {
}
function getBuffers() {
const secret = localStorage.getItem("secret");
const hashedKey = hashKey(secret);
const hashedKeys = secret.split(",")
.map(key => key.trim())
.map(key => hashKey(key));
// console.log(hashedKeys);

toast.promise(
makeRequest("/api/buffers", { key: hashedKey }).then((result: any) => {
makeRequest("/api/buffers", { key: hashedKeys }).then((result: any) => {
// console.log("result", result);
const clientBuffers: Array<string> = [];
result?.fetchedBuffers.map((buffer: BufferType) =>
clientBuffers.push(buffer.buffer)
console.log(result);
result?.forEach(fetchedBuffer => {
fetchedBuffer.forEach((buffer: BufferType) => {
clientBuffers.push(buffer.buffer)
});
}
);
// console.log("clientBuffers", clientBuffers);
setBuffers(clientBuffers);
Expand Down Expand Up @@ -391,6 +405,13 @@ const App: React.FC = (): JSX.Element => {
settings.
</li>
<li>Create a new buffer by clicking the plus icon</li>
<li>
New: You can now add more than one secret key.
Each key has to be separated by commas or else it won't work.
Example : 34nd2, fwfge3
The first key will be used as the main key to save your buffer text.
For the other keys, only the content of their buffer will be fetched and they may not be used to save anything.
</li>
</ul>
</div>
</Modal.Content>
Expand Down

1 comment on commit e02adc9

@vercel
Copy link

@vercel vercel bot commented on e02adc9 Apr 30, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.