Skip to content

Commit

Permalink
Merge main into sweep/add-sweep-config
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Aug 11, 2023
2 parents 709b72f + d508e46 commit d336d7e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,9 @@ app.get(
}
);

app.get("/api/sseUpdates", requireLogin, async (req, res) => {
app.get("/api/sseUpdates/:clientSessionId", requireLogin, async (req, res) => {
const userid = getUserId(req);
SE.connectClient(userid, req, res);
SE.connectClient(userid, req.params.clientSessionId, req, res);
});

app.get(
Expand Down
6 changes: 4 additions & 2 deletions src/Library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default function Library({ mobile = false }) {

// TODO handle encryption before enabling
// This doesn't handle multiple tabs in the same browser.
// useSSEUpdates(setSettings);
useSSEUpdates(setSettings);

async function saveAllBooks() {
setLoading(true);
Expand Down Expand Up @@ -428,7 +428,9 @@ export default function Library({ mobile = false }) {
await Promise.all([fetchBooks(), fetchSettings()]);
//await Promise.all([fetchBooks(), fetchSettings(), fetchBookTitles()]);
};
setCookie("clientid", nanoid(), 1);
sessionStorage.setItem("clientSessionId", nanoid());

//setCookie("clientid", nanoid(), 1);
func();
}, []);

Expand Down
4 changes: 2 additions & 2 deletions src/LibraryDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default function LibraryDesktop() {
className="h-full w-full absolute top-0 left-0 bg-editor dark:bg-dmeditor z-0"
id="editor"
>
<div className="h-full w-full">
{/* <div className="h-full w-full">
{!bookid && !currentChapter && (
<LibErrorBoundary component="home">
<EditorPlaceholder loaded={state.booksLoaded}>
Expand All @@ -220,7 +220,7 @@ export default function LibraryDesktop() {
</EditorPlaceholder>
</LibErrorBoundary>
)}
</div>
</div> */}

<div className="h-full w-full">
{/* Has to be chapterid and not currentChapter! Otherwise book editor loads and kicks off a save. This bug is now fixed but leaving comment */}
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleSearchSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ListItem from "./components/ListItem";
import LibraryContext from "./LibraryContext";
import { useColors } from "./lib/hooks";
import { RootState } from "./store";
import { getChapterText, getStringContext } from "./utils";
import { getChapterText, getStringContext, useLocalStorage } from "./utils";

function SearchResult({ book, chapter, index }) {
return (
Expand All @@ -35,7 +35,7 @@ export default function SimpleSearchSidebar() {
const navigate = useNavigate();
const { settings } = useContext(LibraryContext) as LibraryContextType;
const colors = useColors();
const [searchTerm, setSearchTerm] = React.useState("");
const [searchTerm, setSearchTerm] = useLocalStorage("searchTerm", "");
const searchRef = React.useRef(null);

useEffect(() => {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/fetchData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ export async function postWithCsrf(url: string, body: any) {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ ...body, csrfToken: getCsrfToken() }),
body: JSON.stringify({
...body,
csrfToken: getCsrfToken(),
clientSessionId: sessionStorage.getItem("clientSessionId"),
}),
});
return res;
}
Expand Down
9 changes: 5 additions & 4 deletions src/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export function lightColors() {

export function useSSEUpdates(setSettings) {
// TODO this solution doesn't handle multiple tabs in the same browser
const clientid = getCookie("clientid");
const clientSessionId = sessionStorage.getItem("clientSessionId");
console.log({ clientSessionId });
const dispatch = useDispatch();

function listen(eventName, eventSource, func) {
Expand All @@ -226,8 +227,8 @@ export function useSSEUpdates(setSettings) {
}

return useEffect(() => {
if (clientid) {
const eventSourceUrl = `/api/sseUpdates`;
if (clientSessionId) {
const eventSourceUrl = `/api/sseUpdates/${clientSessionId}`;
const eventSource = new EventSource(eventSourceUrl, {
withCredentials: true,
});
Expand Down Expand Up @@ -267,7 +268,7 @@ export function useSSEUpdates(setSettings) {
eventSource.close();
};
}
}, [clientid]);
}, [clientSessionId]);
}

export function useRecording() {
Expand Down
6 changes: 3 additions & 3 deletions src/storage/storageEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export function getLastEdited(userid) {
// 3. Edits the created at timestamp to send back to the client and in the last edited cache on the server. And
// 4. Updates other clients if updateData is given.
export async function save(req, res, updateData, saveFunc) {
const clientidOfWriter = req.cookies.clientid;
const clientidOfWriter = req.body.clientSessionId;
console.log({ clientidOfWriter }, "in save");
const userid = getUserId(req);
const lastHeardFromServer = Date.now();
if (updateData) {
Expand Down Expand Up @@ -73,7 +74,7 @@ export function updateClients(userid, clientidOfWriter, eventName, _data) {
}
}

export function connectClient(userid, req, res) {
export function connectClient(userid, clientid, req, res) {
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Access-Control-Allow-Origin", "*");
Expand All @@ -83,7 +84,6 @@ export function connectClient(userid, req, res) {
// flush the headers to establish SSE with client
// needs to be done if using compression
res.flushHeaders();
const clientid = req.cookies.clientid;
const newConnection = { clientid, res };
console.log("New connection", { userid, clientid });
if (clientsToUpdate[userid]) {
Expand Down

0 comments on commit d336d7e

Please sign in to comment.