Skip to content
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

Add support for viewing snapshot history #75

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
24 changes: 20 additions & 4 deletions packages/greenbox-web/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Stack,
Text,
Tooltip,
Link,
} from "@chakra-ui/react";
import { RawSnapshotData } from "greenbox-data";
import { useCallback } from "react";
Expand All @@ -27,6 +28,10 @@ type Props = {
loading?: boolean;
error?: boolean;
errorMessage?: string;
snapshots: {
current: number | null;
total: number | null;
};
};

const forceRefreshInfo = `
Expand All @@ -41,13 +46,17 @@ export default function Header({
loading,
error,
errorMessage,
snapshots,
}: Props) {
const dispatch = useAppDispatch();

const forceUpdate = useCallback(() => {
dispatch(fetchAll(true));
}, [dispatch]);

const prevSnapshotLink = snapshots.current && snapshots.current !== 1 && `/?u=${meta?.id}&h=${snapshots.current - 1}`
const nextSnapshotLink = snapshots.current && snapshots.total && snapshots.current !== snapshots.total && `/?u=${meta?.id}&h=${snapshots.current + 1}`

return (
<Stack as="section" alignItems="stretch" py={2}>
<HStack alignItems="center" flex={1} maxWidth="100%" wrap="wrap">
Expand All @@ -58,7 +67,7 @@ export default function Header({
<Box flex={1} />
<Box textAlign="right">
{meta ? (
<MetaInfo direct={direct} meta={meta} />
<MetaInfo direct={direct} meta={meta} snapshots={snapshots} />
) : loading ? (
<Spinner />
) : error ? (
Expand All @@ -70,9 +79,16 @@ export default function Header({
</Box>
</HStack>
<Stack>
<Text>
To get the data from your account, first install the script by running
</Text>
<HStack>
<Text>
To get the data from your account, first install the script by running
</Text>
<Box flex={1} />
<Stack>
{prevSnapshotLink && <Link href={prevSnapshotLink}>Previous snapshot</Link>}
{nextSnapshotLink && <Link href={nextSnapshotLink}>Next snapshot</Link>}
</Stack>
</HStack>
<Code p={2} borderRadius={5}>
git checkout loathers/greenbox release
</Code>
Expand Down
11 changes: 9 additions & 2 deletions packages/greenbox-web/src/components/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ import Header from "./Header";
export default function MainPage() {
const [directValue] = useQueryParam("d", StringParam);
const [playerId] = useQueryParam("u", NumberParam);
const [snapshotNumber] = useQueryParam("h", NumberParam);
const favouritePlayerId = useAppSelector((state) => state.favouritePlayerId);

const dispatch = useAppDispatch();

useEffect(() => {
const id = playerId || favouritePlayerId;
if (id) dispatch(fetchPlayerData(id));
}, [playerId, dispatch, favouritePlayerId]);
if (id) dispatch(fetchPlayerData({
playerId: id,
snapshotNumber: snapshotNumber ?? undefined,
}));
}, [playerId, dispatch, favouritePlayerId, snapshotNumber]);

useEffect(() => {
if (!directValue) return;
Expand All @@ -45,6 +49,8 @@ export default function MainPage() {
const loading = useAppSelector((state) => state.loading);
const error = useAppSelector((state) => state.error);
const errorMessage = useAppSelector((state) => state.errorMessage);
const snapshotCurrent = useAppSelector((state) => state.snapshotNumber);
const snapshotsTotal = useAppSelector((state) => state.snapshotsTotal);

const id = "clash-toast";

Expand Down Expand Up @@ -82,6 +88,7 @@ export default function MainPage() {
loading={loading.playerData}
error={error.playerData}
errorMessage={errorMessage.playerData}
snapshots={{current: snapshotCurrent, total: snapshotsTotal}}
/>
<Tabs isLazy variant="enclosed">
<TabList>
Expand Down
10 changes: 9 additions & 1 deletion packages/greenbox-web/src/components/MetaInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import { HStack, Text } from "@chakra-ui/react";
import { formatDistance, intlFormat } from "date-fns";
import { RawSnapshotData, VERSION } from "greenbox-data";
import { useEffect, useMemo, useState } from "react";
import { useAppSelector } from "../hooks";

import { FavouriteButton } from "./FavouriteButton";
import VersionWarning from "./VersionWarning";

type Props = {
meta: RawSnapshotData["meta"];
direct?: boolean;
snapshots: {
current: number | null;
total: number | null;
};
};

export default function MetaInfo({ meta, direct }: Props) {
export default function MetaInfo({ meta, direct, snapshots }: Props) {
const [now, setNow] = useState(new Date());
const date = useMemo(() => new Date(meta.timestamp), [meta.timestamp]);
const humanDate = useMemo(
Expand Down Expand Up @@ -42,6 +47,9 @@ export default function MetaInfo({ meta, direct }: Props) {
<Text title={`r${meta.revision}`}>{`${
direct ? "Private s" : "S"
}napshot`}</Text>{" "}
<Text>
{snapshots.current} / {snapshots.total}
</Text>{" "}
<Text>by</Text>{" "}
<Text fontWeight="bold" title={`Player #${meta.id}`}>
{meta.name}
Expand Down
29 changes: 24 additions & 5 deletions packages/greenbox-web/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export interface GreenboxState {
loading: Partial<{ [K in keyof GreenboxState]: boolean }>;
error: Partial<{ [K in keyof GreenboxState]: boolean }>;
errorMessage: Partial<{ [K in keyof GreenboxState]: string }>;
snapshotNumber: number | null;
snapshotsTotal: number | null;
}

const initialState: GreenboxState = {
Expand Down Expand Up @@ -106,6 +108,8 @@ const initialState: GreenboxState = {
},
error: { wikiClashes: false },
errorMessage: {},
snapshotNumber: null,
snapshotsTotal: null,
};

export const fetchClasses = createAsyncThunk(
Expand Down Expand Up @@ -161,17 +165,27 @@ export const fetchAll = createAsyncThunk(
},
);

export interface GreenboxSpecifier {
playerId: number
snapshotNumber?: number
}

export const fetchPlayerData = createAsyncThunk(
"playerData/fetch",
async (playerId: number) => {
async (specifier: GreenboxSpecifier) => {
const response = await fetch(
`https://oaf.loathers.net/api/greenbox/${playerId}`,
specifier.snapshotNumber
? `https://oaf.loathers.net/api/greenbox/${specifier.playerId}/${specifier.snapshotNumber}`
: `https://oaf.loathers.net/api/greenbox/${specifier.playerId}`
);
const json = await response.json();
if (response.status !== 200) {
throw new Error(json.error);
}
return json.greenboxString || json.data as string;
return {
data: json.greenboxString || json.data as string,
total: json.total,
};
},
);

Expand Down Expand Up @@ -306,9 +320,14 @@ export const greenboxSlice = createSlice({
})
.addCase(fetchPlayerData.fulfilled, (state, action) => {
// Set current player id
state.playerId = action.meta.arg;
state.playerId = action.meta.arg.playerId;
// Parse and load greenbox string
const greenboxString = action.payload;
const greenboxString = action.payload.data;
state.snapshotsTotal = action.payload.total;
if (typeof state.snapshotsTotal !== "number") {
state.snapshotsTotal = state.snapshotsTotal?.greenbox;
}
state.snapshotNumber = action.meta.arg.snapshotNumber ?? state.snapshotsTotal
try {
state.playerData = api.expand(greenboxString);
state.error.playerData = false;
Expand Down