Skip to content

refactor: replace accounts with auth for AuthState #1154

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 29, 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
4 changes: 2 additions & 2 deletions src/context/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ jest.mock('../hooks/useNotifications');

const customRender = (
ui,
accounts: AuthState = mockAuth,
auth: AuthState = mockAuth,
settings: SettingsState = mockSettings,
) => {
return render(
<AppContext.Provider value={{ auth: accounts, settings }}>
<AppContext.Provider value={{ auth, settings }}>
<AppProvider>{ui}</AppProvider>
</AppContext.Provider>,
);
Expand Down
8 changes: 4 additions & 4 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { getNotificationCount } from '../utils/notifications';
import { clearState, loadState, saveState } from '../utils/storage';
import { setTheme } from '../utils/theme';

const defaultAccounts: AuthState = {
const defaultAuth: AuthState = {
token: null,
enterpriseAccounts: [],
user: null,
Expand Down 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>(defaultAccounts);
const [accounts, setAccounts] = useState<AuthState>(defaultAuth);
const [settings, setSettings] = useState<SettingsState>(defaultSettings);
const {
fetchNotifications,
Expand Down Expand Up @@ -179,15 +179,15 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
);

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

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

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

if (existing.settings) {
Expand Down
20 changes: 10 additions & 10 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('hooks/useNotifications.ts', () => {

describe('enterprise', () => {
it('should fetch notifications with success - enterprise only', async () => {
const accounts: AuthState = {
const auth: AuthState = {
...mockAuth,
token: null,
};
Expand All @@ -119,7 +119,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(accounts, {
result.current.fetchNotifications(auth, {
...mockSettings,
detailedNotifications: false,
});
Expand All @@ -136,7 +136,7 @@ describe('hooks/useNotifications.ts', () => {
});

it('should fetch notifications with failure - enterprise only', async () => {
const accounts: AuthState = {
const auth: AuthState = {
...mockAuth,
token: null,
};
Expand All @@ -156,7 +156,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(accounts, mockSettings);
result.current.fetchNotifications(auth, mockSettings);
});

await waitFor(() => {
Expand All @@ -167,7 +167,7 @@ describe('hooks/useNotifications.ts', () => {

describe('github.com', () => {
it('should fetch notifications with success - github.com only', async () => {
const accounts: AuthState = {
const auth: AuthState = {
...mockAuth,
enterpriseAccounts: [],
user: mockUser,
Expand All @@ -185,7 +185,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(accounts, {
result.current.fetchNotifications(auth, {
...mockSettings,
detailedNotifications: false,
});
Expand All @@ -200,7 +200,7 @@ describe('hooks/useNotifications.ts', () => {
});

it('should fetch notifications with failures - github.com only', async () => {
const accounts: AuthState = {
const auth: AuthState = {
...mockAuth,
enterpriseAccounts: [],
};
Expand All @@ -220,7 +220,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(accounts, mockSettings);
result.current.fetchNotifications(auth, mockSettings);
});

await waitFor(() => {
Expand All @@ -232,7 +232,7 @@ describe('hooks/useNotifications.ts', () => {

describe('with detailed notifications', () => {
it('should fetch notifications with success', async () => {
const accounts: AuthState = {
const auth: AuthState = {
...mockAuth,
enterpriseAccounts: [],
user: mockUser,
Expand Down Expand Up @@ -393,7 +393,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(accounts, {
result.current.fetchNotifications(auth, {
...mockSettings,
detailedNotifications: true,
});
Expand Down
43 changes: 20 additions & 23 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,35 @@ interface NotificationsState {
hostname: string,
) => void;
fetchNotifications: (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
) => Promise<void>;
markNotificationRead: (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
id: string,
hostname: string,
) => Promise<void>;
markNotificationDone: (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
id: string,
hostname: string,
) => Promise<void>;
unsubscribeNotification: (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
id: string,
hostname: string,
) => Promise<void>;
markRepoNotifications: (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
repoSlug: string,
hostname: string,
) => Promise<void>;
markRepoNotificationsDone: (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
repoSlug: string,
hostname: string,
Expand All @@ -76,21 +76,18 @@ export const useNotifications = (): NotificationsState => {
);

const fetchNotifications = useCallback(
async (accounts: AuthState, settings: SettingsState) => {
async (auth: AuthState, settings: SettingsState) => {
setStatus('loading');

try {
const fetchedNotifications = await getAllNotifications(
accounts,
settings,
);
const fetchedNotifications = await getAllNotifications(auth, settings);

setNotifications(fetchedNotifications);
triggerNativeNotifications(
notifications,
fetchedNotifications,
settings,
accounts,
auth,
);
setStatus('success');
} catch (err) {
Expand All @@ -103,14 +100,14 @@ export const useNotifications = (): NotificationsState => {

const markNotificationRead = useCallback(
async (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
id: string,
hostname: string,
) => {
setStatus('loading');

const token = getTokenForHost(hostname, accounts);
const token = getTokenForHost(hostname, auth);

try {
await markNotificationThreadAsRead(id, hostname, token);
Expand All @@ -134,14 +131,14 @@ export const useNotifications = (): NotificationsState => {

const markNotificationDone = useCallback(
async (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
id: string,
hostname: string,
) => {
setStatus('loading');

const token = getTokenForHost(hostname, accounts);
const token = getTokenForHost(hostname, auth);

try {
await markNotificationThreadAsDone(id, hostname, token);
Expand All @@ -165,18 +162,18 @@ export const useNotifications = (): NotificationsState => {

const unsubscribeNotification = useCallback(
async (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
id: string,
hostname: string,
) => {
setStatus('loading');

const token = getTokenForHost(hostname, accounts);
const token = getTokenForHost(hostname, auth);

try {
await ignoreNotificationThreadSubscription(id, hostname, token);
await markNotificationRead(accounts, settings, id, hostname);
await markNotificationRead(auth, settings, id, hostname);
setStatus('success');
} catch (err) {
setStatus('success');
Expand All @@ -187,14 +184,14 @@ export const useNotifications = (): NotificationsState => {

const markRepoNotifications = useCallback(
async (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
repoSlug: string,
hostname: string,
) => {
setStatus('loading');

const token = getTokenForHost(hostname, accounts);
const token = getTokenForHost(hostname, auth);

try {
await markRepositoryNotificationsAsRead(repoSlug, hostname, token);
Expand All @@ -216,7 +213,7 @@ export const useNotifications = (): NotificationsState => {

const markRepoNotificationsDone = useCallback(
async (
accounts: AuthState,
auth: AuthState,
settings: SettingsState,
repoSlug: string,
hostname: string,
Expand All @@ -238,7 +235,7 @@ export const useNotifications = (): NotificationsState => {
await Promise.all(
notificationsToRemove.map((notification) =>
markNotificationDone(
accounts,
auth,
settings,
notification.id,
notifications[accountIndex].hostname,
Expand Down
16 changes: 8 additions & 8 deletions src/routes/LoginWithOAuthApp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jest.mock('react-router-dom', () => ({
describe('routes/LoginWithOAuthApp.tsx', () => {
const openExternalMock = jest.spyOn(shell, 'openExternal');

const mockAccounts: AuthState = {
const mockAuth: AuthState = {
enterpriseAccounts: [],
user: null,
};
Expand All @@ -29,7 +29,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {

it('renders correctly', () => {
const tree = render(
<AppContext.Provider value={{ auth: mockAccounts }}>
<AppContext.Provider value={{ auth: mockAuth }}>
<MemoryRouter>
<LoginWithOAuthApp />
</MemoryRouter>
Expand All @@ -41,7 +41,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {

it('let us go back', () => {
render(
<AppContext.Provider value={{ auth: mockAccounts }}>
<AppContext.Provider value={{ auth: mockAuth }}>
<MemoryRouter>
<LoginWithOAuthApp />
</MemoryRouter>
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
describe("'Create new OAuth App' button", () => {
it('should be disabled if no hostname configured', async () => {
render(
<AppContext.Provider value={{ auth: mockAccounts }}>
<AppContext.Provider value={{ auth: mockAuth }}>
<MemoryRouter>
<LoginWithOAuthApp />
</MemoryRouter>
Expand All @@ -94,7 +94,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {

it('should open in browser if hostname configured', async () => {
render(
<AppContext.Provider value={{ auth: mockAccounts }}>
<AppContext.Provider value={{ auth: mockAuth }}>
<MemoryRouter>
<LoginWithOAuthApp />
</MemoryRouter>
Expand All @@ -113,7 +113,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {

it('should receive a logged-in enterprise account', () => {
const { rerender } = render(
<AppContext.Provider value={{ auth: mockAccounts }}>
<AppContext.Provider value={{ auth: mockAuth }}>
<MemoryRouter>
<LoginWithOAuthApp />
</MemoryRouter>
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {

it('should render the form with errors', () => {
render(
<AppContext.Provider value={{ auth: mockAccounts }}>
<AppContext.Provider value={{ auth: mockAuth }}>
<MemoryRouter>
<LoginWithOAuthApp />
</MemoryRouter>
Expand All @@ -168,7 +168,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {

it('should open help docs in the browser', async () => {
render(
<AppContext.Provider value={{ auth: mockAccounts }}>
<AppContext.Provider value={{ auth: mockAuth }}>
<MemoryRouter>
<LoginWithOAuthApp />
</MemoryRouter>
Expand Down
Loading