Skip to content

Commit

Permalink
Removed warnings and added new store
Browse files Browse the repository at this point in the history
  • Loading branch information
evoggy committed Aug 24, 2024
1 parent e5c30d5 commit 2a85144
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 140 deletions.
44 changes: 0 additions & 44 deletions src/App copy.tsx

This file was deleted.

27 changes: 12 additions & 15 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { useState } from 'react';
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import reactLogo from "./assets/react.svg";
import { invoke } from "@tauri-apps/api/core";
import "./App.css";

import { ToastContainer, toast } from 'react-toastify';
Expand All @@ -21,20 +17,19 @@ import ConnectionModal from "./components/connection_modal";
import ConsoleTab from "./components/console_tab";
import BackEnd from './backend';

function App() {
const [greetMsg, setGreetMsg] = useState("");
const [name, setName] = useState("");

async function greet() {
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
setGreetMsg(await invoke("greet", { name }));
}
import createFastContext from "./global_context";

console.log("Rendering main app")
export const { Provider, useStore } = createFastContext({
console: ""
});

return (
<div>

function App() {

return (
<Provider>
<div>
{/* Routes nest inside one another. Nested route paths build upon
parent route paths, and nested route elements render inside
parent route elements. See the note about <Outlet> below. */}
Expand All @@ -51,7 +46,9 @@ function App() {
</Route>
</Routes>
<BackEnd/>
</div>
</div>
</Provider>

);
}

Expand Down
28 changes: 8 additions & 20 deletions src/backend.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
import { useEffect, useContext } from 'react';
import { useEffect } from 'react';
import { listen } from "@tauri-apps/api/event";
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import Table from 'react-bootstrap/Table';

import { toast } from 'react-toastify';

import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';

import { store } from './store.js';

import { ConsoleEvent } from './interface/console_event';

import {useStore} from "./App";

interface ConsoleEventMessage {
payload: ConsoleEvent;
}

function BackEnd() {
const { state, dispatch } = useContext(store);
const [_, setStore] = useStore((store) => store["console"]);

const changeColor = () => {
dispatch({ type: "CHANGE_COLOR", payload: "blue" });
};
let currentConsoleText = "";

useEffect(() => {
console.log("Backend loaded");

const unListen = listen("console", (e: ConsoleEventMessage) => {
//setConsoleText(consoleText + e.payload.message + "\n");
console.log("Got console stuff off backend")
dispatch({ type: "CONSOLE", payload: e.payload.message });

currentConsoleText += e.payload.message + "\n";
setStore({ ["console"]: currentConsoleText });
});

return () => {
Expand Down
14 changes: 0 additions & 14 deletions src/components/connection_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,10 @@ import Modal from 'react-bootstrap/Modal';
import Table from 'react-bootstrap/Table';

import { toast } from 'react-toastify';

import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';

import 'bootstrap/dist/css/bootstrap.min.css';

import { invoke } from "@tauri-apps/api/core";

import { ScanResponse } from '../interface/scan_response';


// interface ProgressEventPayload {
// matches_per_bs: Stats;
// hits_per_sensor: Stats;
// sync: Stats;
// }

interface ProgressEventProps {
payload: ScanResponse;
}
Expand Down
27 changes: 7 additions & 20 deletions src/components/console_tab.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
import { useContext, useState } from 'react';
import { listen } from "@tauri-apps/api/event";
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import Table from 'react-bootstrap/Table';

import { toast } from 'react-toastify';

import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';

import { store } from './../store.js';

import { ConsoleEvent } from '../interface/console_event';

interface ConsoleEventMessage {
payload: ConsoleEvent;
}
import {useStore} from "./../App";

function ConsoleTab() {
const [consoleText, setConsoleText] = useState("");

const { state, dispatch } = useContext(store);
const [crazyflieConsole] = useStore((store) => store["console"]);

return (
<div>
<pre>{state.console}</pre>
<Row>
<Col>
<pre>{crazyflieConsole}</pre>
</Col>
</Row>
</div>
);
}
Expand Down
73 changes: 73 additions & 0 deletions src/global_context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Source https://github.com/jherr/fast-react-context/tree/main/fast-context-generic

import React, {
useRef,
createContext,
useContext,
useCallback,
useSyncExternalStore,
} from "react";

export default function createGlobalContext<Store>(initialState: Store) {
function useStoreData(): {
get: () => Store;
set: (value: Partial<Store>) => void;
subscribe: (callback: () => void) => () => void;
} {
const store = useRef(initialState);

const get = useCallback(() => store.current, []);

const subscribers = useRef(new Set<() => void>());

const set = useCallback((value: Partial<Store>) => {
store.current = { ...store.current, ...value };
subscribers.current.forEach((callback) => {console.log("Calling callback"); callback()});
}, []);

const subscribe = useCallback((callback: () => void) => {
subscribers.current.add(callback);
return () => subscribers.current.delete(callback);
}, []);

return {
get,
set,
subscribe,
};
}

type UseStoreDataReturnType = ReturnType<typeof useStoreData>;

const StoreContext = createContext<UseStoreDataReturnType | null>(null);

function Provider({ children }: { children: React.ReactNode }) {
return (
<StoreContext.Provider value={useStoreData()}>
{children}
</StoreContext.Provider>
);
}

function useStore<SelectorOutput>(
selector: (store: Store) => SelectorOutput
): [SelectorOutput, (value: Partial<Store>) => void] {
const store = useContext(StoreContext);
if (!store) {
throw new Error("Store not found");
}

const state = useSyncExternalStore(
store.subscribe,
() => selector(store.get()),
() => selector(initialState),
);

return [state, store.set];
}

return {
Provider,
useStore,
};
}
3 changes: 0 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import ReactDOM from "react-dom/client";
import App from "./App";
import "./styles.css";
import { BrowserRouter } from "react-router-dom";
import { StateProvider } from "./store";

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<BrowserRouter>
<StateProvider>
<App />
</StateProvider>
</BrowserRouter>
</React.StrictMode>
);
24 changes: 0 additions & 24 deletions src/store.tsx

This file was deleted.

0 comments on commit 2a85144

Please sign in to comment.