clean AF handling of function type with typescript and passing down to context provider types and shit

This commit is contained in:
talksik
2022-05-07 11:10:38 -05:00
parent f0120b19cf
commit 8d875ef785
6 changed files with 50 additions and 17 deletions
@@ -20,6 +20,7 @@ export default function ProtectedRoute({
isLoading: serverLoading, isLoading: serverLoading,
isSuccess: isServerHealthy, isSuccess: isServerHealthy,
error: serverStatusError, error: serverStatusError,
isFetching: serverFetching,
} = useServerCheck(); } = useServerCheck();
const { isLoading, isError, isFetching, isSuccess, refetch } = useAuthCheck( const { isLoading, isError, isFetching, isSuccess, refetch } = useAuthCheck(
@@ -59,6 +60,7 @@ export default function ProtectedRoute({
refetch(); refetch();
}, [jwtToken]); }, [jwtToken]);
// first time loading
if (serverLoading) if (serverLoading)
return ( return (
<span className="text-md text-gray-400 flex items-center justify-center flex-1 text-center p-5 h-screen"> <span className="text-md text-gray-400 flex items-center justify-center flex-1 text-center p-5 h-screen">
+5 -1
View File
@@ -57,7 +57,11 @@ export function useUserLines() {
// todo: base/source of truth for getting all of the lines for the user // todo: base/source of truth for getting all of the lines for the user
// merge with sockets + audio clip data + master data + convomember data // merge with sockets + audio clip data + master data + convomember data
return useQuery("USER_LINES", ApiCalls.getUserLines); return useQuery("USER_LINES", ApiCalls.getUserLines, {
refetchOnWindowFocus: false,
refetchIntervalInBackground: false,
staleTime: Infinity,
});
} }
// =========== MUTATIONS // =========== MUTATIONS
@@ -174,8 +174,9 @@ function useSocketHandler(linesData: MasterLineData[]) {
* and doing initial connections/tune ins ?could trigger this later? * and doing initial connections/tune ins ?could trigger this later?
*/ */
useEffect(() => { useEffect(() => {
console.log(linesData); if (linesData?.length > 0) {
if (linesData) { console.log("going to make initial connections and create lines map");
setLinesMap((prevMappings) => { setLinesMap((prevMappings) => {
// go through the lines from the persistent store // go through the lines from the persistent store
@@ -282,17 +283,14 @@ function useSocketHandler(linesData: MasterLineData[]) {
[$ws] [$ws]
); );
// TODO: move all of these emitters to just having child views doing the work here
return { return {
linesMap, linesMap,
emitters: $ws handleConnectToLine,
? { handleTuneToLine,
handleConnectToLine, handleStartBroadcast,
handleTuneToLine, handleStopBroadcast,
handleStartBroadcast, handleFetchMoreAudioBlocks,
handleStopBroadcast,
handleFetchMoreAudioBlocks,
}
: {},
}; };
} }
@@ -303,11 +301,17 @@ interface ILineDataContext {
// represents the modified and up to date lines data with availability from session // represents the modified and up to date lines data with availability from session
linesMap: LineIdToMasterLine; linesMap: LineIdToMasterLine;
relevantUsers: User[]; relevantUsers: User[];
// in case some components need this like in the peer handling
$ws: Socket;
} }
const LineDataContext = React.createContext<ILineDataContext>({ const LineDataContext = React.createContext<
Partial<ILineDataContext & ReturnType<typeof useSocketHandler>>
>({
linesMap: {}, linesMap: {},
relevantUsers: [], relevantUsers: [],
$ws: undefined,
}); });
export function LineDataProvider({ children }) { export function LineDataProvider({ children }) {
@@ -318,11 +322,19 @@ export function LineDataProvider({ children }) {
// ?just do simple synchronous axios/fetch in useEffect and manage isLoading ourselves? // ?just do simple synchronous axios/fetch in useEffect and manage isLoading ourselves?
const { data: basicUserLinesData } = useUserLines(); const { data: basicUserLinesData } = useUserLines();
const { linesMap } = useSocketHandler(basicUserLinesData?.data?.masterLines); const { linesMap, ...handlers } = useSocketHandler(
basicUserLinesData?.data?.masterLines
);
const value: ILineDataContext = { if (!$ws) {
return <span>attempting to connect you for the here and now...</span>;
}
const value: ILineDataContext & ReturnType<typeof useSocketHandler> = {
linesMap, // TODO send the updated map instead of this array linesMap, // TODO send the updated map instead of this array
relevantUsers: [], relevantUsers: [],
$ws,
...handlers,
}; };
return ( return (
@@ -16,6 +16,8 @@ export default function LineDetailsTerminal({
}: { }: {
selectedLine: MasterLineData; selectedLine: MasterLineData;
}) { }) {
const { handleTuneToLine } = useLineDataProvider();
const { data: userDetails } = useGetUserDetails(); const { data: userDetails } = useGetUserDetails();
const isUserToggleTuned = useMemo( const isUserToggleTuned = useMemo(
() => selectedLine?.currentUserMember?.state === LineMemberState.TUNED, () => selectedLine?.currentUserMember?.state === LineMemberState.TUNED,
@@ -59,6 +61,10 @@ export default function LineDetailsTerminal({
className="absolute bottom-0 p-4 shadow-2xl className="absolute bottom-0 p-4 shadow-2xl
flex flex-row items-center gap-2 justify-end w-full" flex flex-row items-center gap-2 justify-end w-full"
> >
<span className="mr-auto text-teal-800">{`${
selectedLine?.tunedInMemberIds?.length ?? 0
} on the line`}</span>
<button <button
className={`p-3 flex justify-center items-center hover:bg-gray-300 className={`p-3 flex justify-center items-center hover:bg-gray-300
transition-all hover:scale-105`} transition-all hover:scale-105`}
@@ -77,6 +83,9 @@ export default function LineDetailsTerminal({
hover:scale-105 transition-all animate-pulse ${ hover:scale-105 transition-all animate-pulse ${
isUserToggleTuned ? "bg-gray-800 text-white" : "text-black" isUserToggleTuned ? "bg-gray-800 text-white" : "text-black"
}`} }`}
onClick={() =>
handleTuneToLine(selectedLine.lineDetails._id.toString(), true)
}
> >
<FiActivity className="text-lg" /> <FiActivity className="text-lg" />
</button> </button>
@@ -26,6 +26,12 @@ export default function NirvanaTerminal() {
const { isLoading: isLoadingInitialLines } = useUserLines(); const { isLoading: isLoadingInitialLines } = useUserLines();
const { linesMap } = useLineDataProvider(); const { linesMap } = useLineDataProvider();
useEffect(() => {
console.log("change/update in lines map");
console.log(linesMap);
}, [linesMap]);
const allLines: MasterLineData[] = useMemo(() => { const allLines: MasterLineData[] = useMemo(() => {
const masterLines: MasterLineData[] = Object.values(linesMap); const masterLines: MasterLineData[] = Object.values(linesMap);
+2 -2
View File
@@ -28,12 +28,12 @@ body {
} }
/* TODO: temporary...remove? later? */ /* TODO: temporary...remove? later? */
body { /* body {
-webkit-user-select: none; -webkit-user-select: none;
-webkit-app-region: drag; -webkit-app-region: drag;
cursor: pointer; cursor: pointer;
} } */
.titlebar-button { .titlebar-button {
-webkit-app-region: no-drag; -webkit-app-region: no-drag;