From aa8abad17ddaa7e39214afec0116959d318bdfb1 Mon Sep 17 00:00:00 2001
From: Adrian <107351903+6lr61@users.noreply.github.com>
Date: Fri, 13 Sep 2024 16:44:28 +0200
Subject: [PATCH] refactor: don't allow null for context
throw if the provider is missing and the context doesn't have a value
give it that kasper je ne sais quoi
---
src/App.tsx | 14 +++------
src/components/BadgeList.tsx | 2 +-
src/components/LoginButton.tsx | 14 ++++-----
src/components/MentionSegment.tsx | 6 ++--
.../{AuthStateContext.ts => AuthContext.ts} | 8 ++++-
...AuthStateProvider.tsx => AuthProvider.tsx} | 10 ++-----
src/contexts/badges/TwitchBadgeContext.ts | 11 +++----
src/contexts/badges/TwitchBadgeProvider.tsx | 29 ++++++++++---------
src/contexts/event-sub/EventSubContext.ts | 8 ++++-
src/hooks/useEventSub.ts | 16 ++--------
src/hooks/useGetProfile.ts | 5 ++--
src/hooks/useTwitchChat.ts | 16 ++--------
src/main.tsx | 6 ++--
src/utils/api/event-sub/subscribe.ts | 2 +-
src/utils/api/event-sub/unsubscribe.ts | 2 +-
src/utils/event-sub/EventSub.ts | 2 +-
16 files changed, 66 insertions(+), 85 deletions(-)
rename src/contexts/auth-state/{AuthStateContext.ts => AuthContext.ts} (73%)
rename src/contexts/auth-state/{AuthStateProvider.tsx => AuthProvider.tsx} (92%)
diff --git a/src/App.tsx b/src/App.tsx
index f34c74e..bd1cb28 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,11 +1,11 @@
import { useContext, useMemo } from "react";
-import { AuthStateContext } from "./contexts/auth-state/AuthStateContext";
+import { AuthContext } from "./contexts/auth-state/AuthContext";
import LoginButton from "./components/LoginButton";
import Chat from "./components/Chat";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
export default function App() {
- const authContext = useContext(AuthStateContext);
+ const { authState } = useContext(AuthContext);
const queryClient = useMemo(
() =>
new QueryClient({
@@ -14,19 +14,13 @@ export default function App() {
[]
);
- if (!authContext) {
- return
Missing AuthStateContext provider?
;
- }
-
return (
<>
- {authContext.authState && (
+ {authState && (
-
- Hello: {authContext.authState.user.login}
-
+ Hello: {authState.user.login}
Chat Messages:
diff --git a/src/components/BadgeList.tsx b/src/components/BadgeList.tsx
index 1fc0b57..e917032 100644
--- a/src/components/BadgeList.tsx
+++ b/src/components/BadgeList.tsx
@@ -20,7 +20,7 @@ export default function BadgeList({
}: Props): React.ReactElement | undefined {
const twitchBadges = useContext(TwitchBadgeConext);
- if (!twitchBadges || badges.length === 0) {
+ if (badges.length === 0) {
return;
}
diff --git a/src/components/LoginButton.tsx b/src/components/LoginButton.tsx
index 48a0259..50315be 100644
--- a/src/components/LoginButton.tsx
+++ b/src/components/LoginButton.tsx
@@ -1,22 +1,18 @@
import { useContext } from "react";
-import { AuthStateContext } from "../contexts/auth-state/AuthStateContext";
+import { AuthContext } from "../contexts/auth-state/AuthContext";
export default function LoginButton(): React.ReactElement {
- const context = useContext(AuthStateContext);
+ const { authState, login, signOut } = useContext(AuthContext);
- if (!context) {
- throw new Error("LoginButton: Missing AuthStateContext");
- }
-
- if (context.authState) {
+ if (authState) {
return (
-