-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
315 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { type PropsWithChildren } from 'react'; | ||
|
||
type ErrorCardProps = PropsWithChildren<{ | ||
error?: Error; | ||
}> | ||
|
||
export const ErrorCard = ({ children, error }: ErrorCardProps) => { | ||
if (!error) { | ||
return <p>oops!</p>; | ||
} | ||
|
||
return ( | ||
<> | ||
<p>oops!</p> | ||
<p>name: <code>{error.name}</code></p> | ||
<p>message: <code>{error.message}</code></p> | ||
{/* TODO: add error.cause? */} | ||
{children && <>{children}</>} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { type APIGuildMember } from 'discord-api-types/v10'; | ||
import React from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
import { ErrorCard } from './ErrorCard'; | ||
import { LoadingContent } from './LoadingContent'; | ||
import { Welcome } from './Welcome'; | ||
import { useRequest } from '../hooks/use-request'; | ||
|
||
/** | ||
* TODO: | ||
* @param guildUser TODO: | ||
* @returns TODO: | ||
*/ | ||
const getWelcomeMessage = (guildUser: APIGuildMember) => { | ||
const welcome = 'welcome to the trash compactor'; | ||
|
||
if (guildUser?.user?.username) { | ||
return `${welcome}, ${guildUser.user.username}`; | ||
} | ||
|
||
return `${welcome}, <unknown>`; | ||
}; | ||
|
||
/** | ||
* The home page | ||
*/ | ||
export const Home = () => { | ||
const useAuthorizedGuildMemberData = useRequest<APIGuildMember>('/api/v1/authorized'); | ||
const { data: guildUser, error, isLoading } = useAuthorizedGuildMemberData(); | ||
|
||
if (isLoading) { | ||
return (<LoadingContent />); | ||
} | ||
|
||
if (guildUser) { | ||
return ( | ||
<> | ||
<ul className="navigation-list"> | ||
<li> | ||
<Link to="/new">new</Link> | ||
</li> | ||
<li> | ||
<Link to="/worlds">worlds</Link> | ||
</li> | ||
</ul> | ||
<Welcome message={getWelcomeMessage(guildUser)} /> | ||
</> | ||
); | ||
} | ||
|
||
if (error) { | ||
return <ErrorCard error={error} />; | ||
} | ||
|
||
// TODO: FIXME: this _should_ never happen based on the states returnable from useAuthorizedGuildMemberData() | ||
// OR is this potentially an "authenticated but not authorized" handler? | ||
// return <ErrorCard error={Error('Loading finished without receiving data or throwing an error')} />; | ||
return <></>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { LoadingContent } from './LoadingContent'; | ||
import { useHasTimePassed } from '../hooks/use-has-time-passed'; | ||
|
||
jest.mock('../hooks/use-has-time-passed'); | ||
|
||
describe('LoadingContent', () => { | ||
beforeEach(() => { | ||
(useHasTimePassed as jest.Mock).mockReset(); | ||
}); | ||
|
||
// TODO: FIXME: renders nothing before time has passed | ||
it('renders', () => { | ||
(useHasTimePassed as jest.Mock).mockReturnValue(false); | ||
expect(render(<LoadingContent />)).toBeTruthy(); | ||
}); | ||
|
||
// TODO: FIXME: renders loading message after time has passed | ||
it('renders 2', () => { | ||
(useHasTimePassed as jest.Mock).mockReturnValue(true); | ||
expect(render(<LoadingContent />)).toBeTruthy(); | ||
expect(screen.getByText('l o a d i n g . . .')).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { type PropsWithChildren } from 'react'; | ||
|
||
import { useHasTimePassed } from '../hooks/use-has-time-passed'; | ||
|
||
/** | ||
* Default loading message | ||
*/ | ||
const GenericLoadingMessage = () => ( | ||
<> | ||
<code>l o a d i n g . . .</code> | ||
</> | ||
); | ||
|
||
type LoadingContentProps = PropsWithChildren<{ | ||
duration?: number; | ||
}> | ||
|
||
/** | ||
* A component that renders a loading message | ||
*/ | ||
export const LoadingContent = ({ children, duration = 600 }: LoadingContentProps) => { | ||
// Wait a moment, content _might_ load quickly | ||
// Showing a loading message instantly can contribute to worse perceived performance (Citation needed) | ||
const showLoading = useHasTimePassed(duration); | ||
|
||
if (showLoading) { | ||
return children ? | ||
children : | ||
<GenericLoadingMessage />; | ||
} | ||
|
||
return <></>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
export const NewWorld = () => { | ||
return ( | ||
<> | ||
<ul className="navigation-list"> | ||
<li> | ||
<Link to="/">back</Link> | ||
</li> | ||
</ul> | ||
<form> | ||
<label htmlFor="version">version</label> | ||
<select name="version"> | ||
<option>1.20.1</option> | ||
</select> | ||
<label htmlFor="name">name</label> | ||
<input name="name" /> | ||
<button>create</button> | ||
</form> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
export const WorldDetail = () => { | ||
const { worldId } = useParams(); | ||
return ( | ||
<> | ||
<h2>{worldId}</h2> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.