Skip to content

refactor: continued renaming of accounts to auth #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface IProps {
export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
const {
settings,
auth: accounts,
auth,
removeNotificationFromState,
markNotificationRead,
markNotificationDone,
Expand All @@ -46,15 +46,15 @@ export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
} = useContext(AppContext);

const openNotification = useCallback(() => {
openInBrowser(notification, accounts);
openInBrowser(notification, auth);

if (settings.markAsDoneOnOpen) {
markNotificationDone(notification.id, hostname);
} else {
// no need to mark as read, github does it by default when opening it
removeNotificationFromState(settings, notification.id, hostname);
}
}, [notifications, notification, accounts, settings]); // notifications required here to prevent weird state issues
}, [notifications, notification, auth, settings]); // notifications required here to prevent weird state issues

const unsubscribeFromThread = (event: MouseEvent<HTMLElement>) => {
// Don't trigger onClick of parent element.
Expand Down
70 changes: 35 additions & 35 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ interface AppContextState {
export const AppContext = createContext<Partial<AppContextState>>({});

export const AppProvider = ({ children }: { children: ReactNode }) => {
const [accounts, setAccounts] = useState<AuthState>(defaultAuth);
const [auth, setAuth] = useState<AuthState>(defaultAuth);
const [settings, setSettings] = useState<SettingsState>(defaultSettings);
const {
fetchNotifications,
Expand All @@ -104,17 +104,17 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {

// biome-ignore lint/correctness/useExhaustiveDependencies: We only want fetchNotifications to be called for certain account or setting changes.
useEffect(() => {
fetchNotifications(accounts, settings);
fetchNotifications(auth, settings);
}, [
settings.participating,
settings.showBots,
settings.detailedNotifications,
accounts.token,
accounts.enterpriseAccounts.length,
auth.token,
auth.enterpriseAccounts.length,
]);

useInterval(() => {
fetchNotifications(accounts, settings);
fetchNotifications(auth, settings);
}, Constants.FETCH_INTERVAL);

// biome-ignore lint/correctness/useExhaustiveDependencies: We need to update tray title when settings or notifications changes.
Expand All @@ -136,58 +136,58 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {

const newSettings = { ...settings, [name]: value };
setSettings(newSettings);
saveState({ auth: accounts, settings: newSettings });
saveState({ auth, settings: newSettings });
},
[accounts, settings],
[auth, settings],
);

const isLoggedIn = useMemo(() => {
return !!accounts.token || accounts.enterpriseAccounts.length > 0;
}, [accounts]);
return !!auth.token || auth.enterpriseAccounts.length > 0;
}, [auth]);

const login = useCallback(async () => {
const { authCode } = await authGitHub();
const { token } = await getToken(authCode);
const hostname = Constants.DEFAULT_AUTH_OPTIONS.hostname;
const user = await getUserData(token, hostname);
const updatedAccounts = addAccount(accounts, token, hostname, user);
setAccounts(updatedAccounts);
saveState({ auth: updatedAccounts, settings });
}, [accounts, settings]);
const updatedAuth = addAccount(auth, token, hostname, user);
setAuth(updatedAuth);
saveState({ auth: updatedAuth, settings });
}, [auth, settings]);

const loginEnterprise = useCallback(
async (data: AuthOptions) => {
const { authOptions, authCode } = await authGitHub(data);
const { token, hostname } = await getToken(authCode, authOptions);
const updatedAccounts = addAccount(accounts, token, hostname);
setAccounts(updatedAccounts);
saveState({ auth: updatedAccounts, settings });
const updatedAuth = addAccount(auth, token, hostname);
setAuth(updatedAuth);
saveState({ auth: updatedAuth, settings });
},
[accounts, settings],
[auth, settings],
);

const validateToken = useCallback(
async ({ token, hostname }: AuthTokenOptions) => {
await headNotifications(hostname, token);

const user = await getUserData(token, hostname);
const updatedAccounts = addAccount(accounts, token, hostname, user);
setAccounts(updatedAccounts);
const updatedAccounts = addAccount(auth, token, hostname, user);
setAuth(updatedAccounts);
saveState({ auth: updatedAccounts, settings });
},
[accounts, settings],
[auth, settings],
);

const logout = useCallback(() => {
setAccounts(defaultAuth);
setAuth(defaultAuth);
clearState();
}, []);

const restoreSettings = useCallback(() => {
const existing = loadState();

if (existing.auth) {
setAccounts({ ...defaultAuth, ...existing.auth });
setAuth({ ...defaultAuth, ...existing.auth });
}

if (existing.settings) {
Expand All @@ -197,44 +197,44 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
}, []);

const fetchNotificationsWithAccounts = useCallback(
async () => await fetchNotifications(accounts, settings),
[accounts, settings, notifications],
async () => await fetchNotifications(auth, settings),
[auth, settings, notifications],
);

const markNotificationReadWithAccounts = useCallback(
async (id: string, hostname: string) =>
await markNotificationRead(accounts, settings, id, hostname),
[accounts, notifications],
await markNotificationRead(auth, settings, id, hostname),
[auth, notifications],
);

const markNotificationDoneWithAccounts = useCallback(
async (id: string, hostname: string) =>
await markNotificationDone(accounts, settings, id, hostname),
[accounts, notifications],
await markNotificationDone(auth, settings, id, hostname),
[auth, notifications],
);

const unsubscribeNotificationWithAccounts = useCallback(
async (id: string, hostname: string) =>
await unsubscribeNotification(accounts, settings, id, hostname),
[accounts, notifications],
await unsubscribeNotification(auth, settings, id, hostname),
[auth, notifications],
);

const markRepoNotificationsWithAccounts = useCallback(
async (repoSlug: string, hostname: string) =>
await markRepoNotifications(accounts, settings, repoSlug, hostname),
[accounts, notifications],
await markRepoNotifications(auth, settings, repoSlug, hostname),
[auth, notifications],
);

const markRepoNotificationsDoneWithAccounts = useCallback(
async (repoSlug: string, hostname: string) =>
await markRepoNotificationsDone(accounts, settings, repoSlug, hostname),
[accounts, notifications],
await markRepoNotificationsDone(auth, settings, repoSlug, hostname),
[auth, notifications],
);

return (
<AppContext.Provider
value={{
auth: accounts,
auth,
isLoggedIn,
login,
loginEnterprise,
Expand Down
11 changes: 3 additions & 8 deletions src/routes/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ import { isLinux, isMacOS } from '../utils/platform';
import { setTheme } from '../utils/theme';

export const SettingsRoute: FC = () => {
const {
auth: accounts,
settings,
updateSetting,
logout,
} = useContext(AppContext);
const { auth, settings, updateSetting, logout } = useContext(AppContext);
const navigate = useNavigate();

const [appVersion, setAppVersion] = useState<string | null>(null);
Expand Down Expand Up @@ -301,7 +296,7 @@ export const SettingsRoute: FC = () => {
className={footerButtonClass}
title="Login with Personal Access Token"
onClick={loginWithPersonalAccessToken}
hidden={isPersonalAccessTokenLoggedIn(accounts)}
hidden={isPersonalAccessTokenLoggedIn(auth)}
>
<KeyIcon size={18} aria-label="Login with Personal Access Token" />
<PlusIcon size={10} className="ml-1 mb-2" />
Expand All @@ -312,7 +307,7 @@ export const SettingsRoute: FC = () => {
className={footerButtonClass}
title="Login with OAuth App"
onClick={loginWithOAuthApp}
hidden={isOAuthAppLoggedIn(accounts)}
hidden={isOAuthAppLoggedIn(auth)}
>
<PersonIcon size={20} aria-label="Login with OAuth App" />
<PlusIcon size={10} className="mb-2" />
Expand Down