mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-15 00:59:09 +00:00
* feat(web): add server-setup page for when bot isn't in the server picked * chore: move contexts into their own folder * feat(web): add recent guilds context, and app shell helper context * feat(web): show recent guilds in masthead * feat(web): functionally add recents to servers list * fix(web): correct styling for servers listing recents/all headers * fix(web): correct some type issues with appShellProps * fix(web): don't show ServerListing recents when recents is empty
This commit is contained in:
parent
3a36d7a85d
commit
99952aa19f
22 changed files with 302 additions and 66 deletions
41
packages/web/src/contexts/app-shell/AppShellContext.tsx
Normal file
41
packages/web/src/contexts/app-shell/AppShellContext.tsx
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { AppShellProps } from '@roleypoly/design-system/organisms/app-shell';
|
||||
import * as React from 'react';
|
||||
import { useRecentGuilds } from '../recent-guilds/RecentGuildsContext';
|
||||
import { useSessionContext } from '../session/SessionContext';
|
||||
|
||||
type AppShellPropsT =
|
||||
| {
|
||||
user: Required<AppShellProps['user']>;
|
||||
guilds: Required<AppShellProps['guilds']>;
|
||||
recentGuilds: Required<AppShellProps['recentGuilds']>;
|
||||
}
|
||||
| {
|
||||
user: undefined;
|
||||
guilds: undefined;
|
||||
recentGuilds: [];
|
||||
};
|
||||
|
||||
export const AppShellPropsContext = React.createContext<AppShellPropsT>({
|
||||
user: undefined,
|
||||
guilds: undefined,
|
||||
recentGuilds: [],
|
||||
});
|
||||
|
||||
export const useAppShellProps = () => React.useContext(AppShellPropsContext);
|
||||
|
||||
export const AppShellPropsProvider = (props: { children: React.ReactNode }) => {
|
||||
const { session } = useSessionContext();
|
||||
const { recentGuilds } = useRecentGuilds();
|
||||
|
||||
const appShellProps: AppShellPropsT = {
|
||||
user: session?.user,
|
||||
guilds: session?.guilds,
|
||||
recentGuilds,
|
||||
};
|
||||
|
||||
return (
|
||||
<AppShellPropsContext.Provider value={appShellProps}>
|
||||
{props.children}
|
||||
</AppShellPropsContext.Provider>
|
||||
);
|
||||
};
|
|
@ -0,0 +1,59 @@
|
|||
import * as React from 'react';
|
||||
|
||||
type RecentGuildsT = {
|
||||
recentGuilds: string[];
|
||||
pushRecentGuild: (id: string) => void;
|
||||
};
|
||||
|
||||
export const RecentGuilds = React.createContext<RecentGuildsT>({
|
||||
recentGuilds: [],
|
||||
pushRecentGuild: () => {},
|
||||
});
|
||||
|
||||
export const useRecentGuilds = () => React.useContext(RecentGuilds);
|
||||
|
||||
const saveState = (state: string[]) => {
|
||||
localStorage.setItem('rp_recent_guilds', JSON.stringify(state));
|
||||
};
|
||||
|
||||
const pullState = (): string[] => {
|
||||
const rawState = localStorage.getItem('rp_recent_guilds');
|
||||
if (!rawState) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(rawState);
|
||||
} catch (e) {
|
||||
console.warn('RecentGuilds failed to re-hydrate saved state', e);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const RecentGuildsProvider = (props: { children: React.ReactNode }) => {
|
||||
const [recentGuilds, setRecentGuilds] = React.useState<string[]>(pullState());
|
||||
|
||||
const recentGuildsData: RecentGuildsT = {
|
||||
recentGuilds,
|
||||
pushRecentGuild: (id: string) => {
|
||||
const nextState = [
|
||||
id,
|
||||
...recentGuilds.slice(0, 19).filter((guild) => guild !== id),
|
||||
];
|
||||
|
||||
if (recentGuilds[0] !== id) {
|
||||
setRecentGuilds(nextState);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
React.useEffect(() => {
|
||||
saveState(recentGuilds);
|
||||
}, [recentGuilds]);
|
||||
|
||||
return (
|
||||
<RecentGuilds.Provider value={recentGuildsData}>
|
||||
{props.children}
|
||||
</RecentGuilds.Provider>
|
||||
);
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
import { SessionData } from '@roleypoly/types';
|
||||
import * as React from 'react';
|
||||
import { useApiContext } from '../api-context/ApiContext';
|
||||
import { useApiContext } from '../api/ApiContext';
|
||||
|
||||
type SessionContextT = {
|
||||
session?: Omit<Partial<SessionData>, 'tokens'>;
|
|
@ -1,16 +1,33 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import { ApiContextProvider } from './api-context/ApiContext';
|
||||
import { AppRouter } from './app-router/AppRouter';
|
||||
import { SessionContextProvider } from './session-context/SessionContext';
|
||||
import { ApiContextProvider } from './contexts/api/ApiContext';
|
||||
import { AppShellPropsProvider } from './contexts/app-shell/AppShellContext';
|
||||
import { RecentGuildsProvider } from './contexts/recent-guilds/RecentGuildsContext';
|
||||
import { SessionContextProvider } from './contexts/session/SessionContext';
|
||||
|
||||
const ProviderProvider = (props: {
|
||||
providerChain: typeof ApiContextProvider[];
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
return props.providerChain.reduceRight(
|
||||
(acc, Provider) => <Provider>{acc}</Provider>,
|
||||
<>{props.children}</>
|
||||
);
|
||||
};
|
||||
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<ApiContextProvider>
|
||||
<SessionContextProvider>
|
||||
<AppRouter />
|
||||
</SessionContextProvider>
|
||||
</ApiContextProvider>
|
||||
<ProviderProvider
|
||||
providerChain={[
|
||||
ApiContextProvider,
|
||||
SessionContextProvider,
|
||||
RecentGuildsProvider,
|
||||
AppShellPropsProvider,
|
||||
]}
|
||||
>
|
||||
<AppRouter />
|
||||
</ProviderProvider>
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { AuthLogin } from '@roleypoly/design-system/templates/auth-login';
|
||||
import { GuildSlug } from '@roleypoly/types';
|
||||
import React from 'react';
|
||||
import { useApiContext } from '../../api-context/ApiContext';
|
||||
import { useApiContext } from '../../contexts/api/ApiContext';
|
||||
|
||||
const Login = () => {
|
||||
const { apiUrl, fetch } = useApiContext();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { useApiContext } from '../../api-context/ApiContext';
|
||||
import { useSessionContext } from '../../session-context/SessionContext';
|
||||
import { useApiContext } from '../../contexts/api/ApiContext';
|
||||
import { useSessionContext } from '../../contexts/session/SessionContext';
|
||||
|
||||
const SessionDebug = () => {
|
||||
const session = useSessionContext();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { navigate } from '@reach/router';
|
||||
import * as React from 'react';
|
||||
import { useApiContext } from '../../api-context/ApiContext';
|
||||
import { useApiContext } from '../../contexts/api/ApiContext';
|
||||
|
||||
const SetApi = () => {
|
||||
const apiContext = useApiContext();
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
import { Redirect } from '@reach/router';
|
||||
import { LandingTemplate } from '@roleypoly/design-system/templates/landing';
|
||||
import * as React from 'react';
|
||||
import { useSessionContext } from '../session-context/SessionContext';
|
||||
import { useAppShellProps } from '../contexts/app-shell/AppShellContext';
|
||||
import { useSessionContext } from '../contexts/session/SessionContext';
|
||||
|
||||
const Landing = () => {
|
||||
const { isAuthenticated } = useSessionContext();
|
||||
const appShellProps = useAppShellProps();
|
||||
|
||||
if (isAuthenticated) {
|
||||
return <Redirect to="/servers" />;
|
||||
}
|
||||
|
||||
return <LandingTemplate />;
|
||||
return <LandingTemplate {...appShellProps} />;
|
||||
};
|
||||
|
||||
export default Landing;
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
import { Redirect } from '@reach/router';
|
||||
import { RolePickerTemplate } from '@roleypoly/design-system/templates/role-picker';
|
||||
import { ServerSetupTemplate } from '@roleypoly/design-system/templates/server-setup';
|
||||
import { PresentableGuild, RoleUpdate, UserGuildPermissions } from '@roleypoly/types';
|
||||
import * as React from 'react';
|
||||
import { useSessionContext } from '../session-context/SessionContext';
|
||||
import { useAppShellProps } from '../contexts/app-shell/AppShellContext';
|
||||
import { useRecentGuilds } from '../contexts/recent-guilds/RecentGuildsContext';
|
||||
import { useSessionContext } from '../contexts/session/SessionContext';
|
||||
import { makeRoleTransactions } from '../utils/roleTransactions';
|
||||
|
||||
type PickerProps = {
|
||||
|
@ -11,8 +14,12 @@ type PickerProps = {
|
|||
|
||||
const Picker = (props: PickerProps) => {
|
||||
const { session, authedFetch, isAuthenticated } = useSessionContext();
|
||||
const { pushRecentGuild } = useRecentGuilds();
|
||||
const appShellProps = useAppShellProps();
|
||||
|
||||
const [pickerData, setPickerData] = React.useState<PresentableGuild | null>(null);
|
||||
const [pickerData, setPickerData] = React.useState<PresentableGuild | null | false>(
|
||||
null
|
||||
);
|
||||
const [pending, setPending] = React.useState(false);
|
||||
|
||||
React.useEffect(() => {
|
||||
|
@ -20,11 +27,20 @@ const Picker = (props: PickerProps) => {
|
|||
const response = await authedFetch(`/get-picker-data/${props.serverID}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (response.status !== 200) {
|
||||
setPickerData(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setPickerData(data);
|
||||
};
|
||||
|
||||
fetchPickerData();
|
||||
}, [props.serverID, authedFetch]);
|
||||
}, [props.serverID, authedFetch, pushRecentGuild]);
|
||||
|
||||
React.useCallback((serverID) => pushRecentGuild(serverID), [pushRecentGuild])(
|
||||
props.serverID
|
||||
);
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Redirect to={`/auth/login?r=${props.serverID}`} replace />;
|
||||
|
@ -34,6 +50,25 @@ const Picker = (props: PickerProps) => {
|
|||
return <div>Loading...</div>;
|
||||
}
|
||||
|
||||
if (pickerData === false) {
|
||||
if (session && session.user && session.guilds) {
|
||||
const guildSlug = session.guilds.find((guild) => guild.id === props.serverID);
|
||||
if (!guildSlug) {
|
||||
throw new Error('placeholder: guild not found in user slugs, 404');
|
||||
}
|
||||
|
||||
return (
|
||||
<ServerSetupTemplate
|
||||
activeGuildId={props.serverID}
|
||||
guildSlug={guildSlug}
|
||||
{...appShellProps}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error('placeholder: session state is odd, 404');
|
||||
}
|
||||
|
||||
const onSubmit = async (submittedRoles: string[]) => {
|
||||
if (pending === true) {
|
||||
return;
|
||||
|
@ -62,8 +97,7 @@ const Picker = (props: PickerProps) => {
|
|||
return (
|
||||
<RolePickerTemplate
|
||||
activeGuildId={props.serverID}
|
||||
user={session?.user}
|
||||
guilds={session?.guilds || []}
|
||||
{...appShellProps}
|
||||
guild={pickerData.guild}
|
||||
guildData={pickerData.data}
|
||||
member={pickerData.member}
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
import { Redirect } from '@reach/router';
|
||||
import { ServersTemplate } from '@roleypoly/design-system/templates/servers';
|
||||
import * as React from 'react';
|
||||
import { useSessionContext } from '../session-context/SessionContext';
|
||||
import { useAppShellProps } from '../contexts/app-shell/AppShellContext';
|
||||
import { useSessionContext } from '../contexts/session/SessionContext';
|
||||
|
||||
const ServersPage = () => {
|
||||
const { isAuthenticated, session } = useSessionContext();
|
||||
const appShellProps = useAppShellProps();
|
||||
if (!isAuthenticated || !session) {
|
||||
return <Redirect to="/" />;
|
||||
}
|
||||
|
||||
return <ServersTemplate guilds={session.guilds || []} user={session.user} />;
|
||||
return <ServersTemplate {...appShellProps} />;
|
||||
};
|
||||
|
||||
export default ServersPage;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue