Skip to content

Commit

Permalink
refactor: 重构数据获取逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed May 3, 2024
1 parent cea883e commit b0ca071
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 301 deletions.
8 changes: 2 additions & 6 deletions frontend/src/components/FooterBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { ExternalLink, Footer, InternalLink, Text } from "@sscreator/ui";
import { useLocation } from "wouter-preact";
import { useData } from "../hooks/useData";
import type { GetResponse } from "../models/status";
import { useStatus } from "../models/status";

export default function FooterBlock() {
const [, setLocation] = useLocation();
const { data: toolStatus } = useData<Record<string, never>, GetResponse>({
method: "GET",
endpoint: "/v1/status",
});
const { data: toolStatus } = useStatus();

return (
<Footer className="mx-auto max-w-4xl w-[90vw] mt-8">
Expand Down
16 changes: 4 additions & 12 deletions frontend/src/components/ToolMetaInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,15 @@ import {
Text,
} from "@sscreator/ui";
import { useLocation } from "wouter-preact";
import { useData } from "../hooks/useData";
import type { GetToolStatusResponse } from "../models/status";
import { ToolStatusEnum } from "../models/status";
import { useToolStatus } from "../models/status";
import { getToolSlug } from "../utils/URLHelper";
import { getDateTimeWithoutSecond, parseTime } from "../utils/timeHelper";

export default function ToolMetaInfo() {
const [, setLocation] = useLocation();
const { data: toolStatus } = useData<
Record<string, never>,
GetToolStatusResponse
>({
method: "GET",
endpoint: `/v1/status/${getToolSlug()}`,
});
const { data: toolStatus } = useToolStatus({ toolSlug: getToolSlug() });
const showUnavaliableModal = useComputed(() =>
toolStatus ? toolStatus.status === ToolStatusEnum.UNAVALIABLE : false,
toolStatus ? toolStatus.status === "UNAVAILABLE" : false,
);

return (
Expand Down Expand Up @@ -74,7 +66,7 @@ export default function ToolMetaInfo() {
</Grid>
)}

{toolStatus?.status === ToolStatusEnum.DOWNGRADED && (
{toolStatus?.status === "DOWNGRADED" && (
<Notice color="warning" title="服务降级">
<Text>
{toolStatus?.reason ??
Expand Down
98 changes: 81 additions & 17 deletions frontend/src/models/JPEPFTNMacket.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,109 @@
export interface GetRulesResponse {
import { useData } from "../hooks/useData";

interface GetRulesResponse {
isOpen: boolean;
buyOrderMinimumPrice: number;
sellOrderMinimumPrice: number;
FTNOrderFee: number;
goodsOrderFee: number;
}
export function useRules() {
return useData<Record<string, never>, GetRulesResponse>({
method: "GET",
endpoint: "/v1/jpep/ftn-macket/rules",
});
}

export interface GetCurrentPriceResponse {
interface GetCurrentPriceResponse {
buyPrice: number;
sellPrice: number;
}
export interface GetCurrentAmountResponse {
export function useCurrentPrice() {
return useData<Record<string, never>, GetCurrentPriceResponse>({
method: "GET",
endpoint: "/v1/jpep/ftn-macket/current-price",
});
}

interface GetCurrentAmountResponse {
buyAmount: number;
sellAmount: number;
}
export function useCurrentAmount() {
return useData<Record<string, never>, GetCurrentAmountResponse>({
method: "GET",
endpoint: "/v1/jpep/ftn-macket/current-amount",
});
}

export interface GetPriceHistoryRequest {
interface GetCurrentAmountDistributionRequest {
type: "buy" | "sell";
range: "24h" | "7d" | "15d" | "30d";
resolution: "5m" | "1h" | "1d";
limit?: number;
}

export interface GetPriceHistoryResponse {
history: Record<number, number>;
interface GetCurrentAmountDistributionResponse {
amountDistribution: Record<number, number>;
}
export function useCurrentAmountDistribution({
type,
limit,
}: GetCurrentAmountDistributionRequest) {
return useData<
GetCurrentAmountDistributionRequest,
GetCurrentAmountDistributionResponse
>({
method: "GET",
endpoint: "/v1/jpep/ftn-macket/current-amount-distribution",
queryArgs: {
type,
limit,
},
});
}

export interface GetAmountHistoryRequest {
interface GetPriceHistoryRequest {
type: "buy" | "sell";
range: "24h" | "7d" | "15d" | "30d";
resolution: "5m" | "1h" | "1d";
}

export interface GetAmountHistoryResponse {
interface GetPriceHistoryResponse {
history: Record<number, number>;
}
export function usePriceHistory({
type,
range,
resolution,
}: GetPriceHistoryRequest) {
return useData<GetPriceHistoryRequest, GetPriceHistoryResponse>({
method: "GET",
endpoint: "/v1/jpep/ftn-macket/price-history",
queryArgs: {
type,
range,
resolution,
},
});
}

export interface GetCurrentAmountDistributionRequest {
interface GetAmountHistoryRequest {
type: "buy" | "sell";
limit?: number;
range: "24h" | "7d" | "15d" | "30d";
resolution: "5m" | "1h" | "1d";
}

export interface GetCurrentAmountDistributionResponse {
amountDistribution: Record<number, number>;
interface GetAmountHistoryResponse {
history: Record<number, number>;
}
export function useAmountHistory({
type,
range,
resolution,
}: GetAmountHistoryRequest) {
return useData<GetAmountHistoryRequest, GetAmountHistoryResponse>({
method: "GET",
endpoint: "/v1/jpep/ftn-macket/amount-history",
queryArgs: {
type,
range,
resolution,
},
});
}
14 changes: 14 additions & 0 deletions frontend/src/models/articles.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { useDataTrigger } from "../hooks/useData";

export interface GetWordFreqResponse {
title: string;
wordFreq: Record<string, number>;
}
export function useWordFreq({ articleSlug }: { articleSlug: string }) {
return useDataTrigger<Record<string, never>, GetWordFreqResponse>({
method: "GET",
endpoint: `/v1/articles/${articleSlug}/word-freq`,
});
}

export interface GetLPRecommendCheckResponse {
articleTitle: string;
canRecommendNow: boolean;
FPReward: number;
nextCanRecommendDate: number;
}
export function useLPRecommendCheck({ articleSlug }: { articleSlug: string }) {
return useDataTrigger<Record<string, never>, GetLPRecommendCheckResponse>({
method: "GET",
endpoint: `/v1/articles/${articleSlug}/lp-recommend-check`,
});
}
70 changes: 55 additions & 15 deletions frontend/src/models/lottery.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,86 @@
export interface GetRewardsResponse {
import { useData } from "../hooks/useData";

interface GetRewardsResponse {
rewards: string[];
}
export function useRewards() {
return useData<Record<string, never>, GetRewardsResponse>({
method: "GET",
endpoint: "/v1/lottery/rewards",
});
}

export interface GetRecordsRequest {
interface GetRecordsRequest {
offset?: number;
limit?: number;
excluded_awards?: string[];
}

export interface GetRecordsItem {
interface GetRecordsItem {
time: number;
rewardName: string;
userName: string;
userUrl: string;
}

export interface GetRecordsResponse {
interface GetRecordsResponse {
records: GetRecordsItem[];
}
export function useRecords({
offset,
limit,
excluded_awards,
}: GetRecordsRequest) {
return useData<GetRecordsRequest, GetRecordsResponse>({
method: "GET",
endpoint: "/v1/lottery/records",
queryArgs: {
offset,
limit,
excluded_awards,
},
});
}

export interface GetSummaryRewardItem {
interface GetSummaryRequest {
range: "1d" | "7d" | "30d" | "all";
}
interface GetSummaryRewardItem {
rewardName: string;
winsCount: number;
winnersCount: number;
averageWinsCountPerWinner: number;
winningRate: number;
rarity: number;
}

export interface GetSummaryRequest {
range: "1d" | "7d" | "30d" | "all";
}

export interface GetSummaryResponse {
rewards: GetSummaryRewardItem[];
}
export function useSummary({ range }: GetSummaryRequest) {
return useData<GetSummaryRequest, GetSummaryResponse>({
method: "GET",
endpoint: "/v1/lottery/summary",
queryArgs: {
range,
},
});
}

export interface GetRewardWinsHistoryRequest {
interface GetRewardWinsHistoryRequest {
range: "1d" | "30d" | "60d";
resolution: "1h" | "1d";
}

export interface GetRewardWinsHistoryResponse {
interface GetRewardWinsHistoryResponse {
history: Record<number, number>;
}
export function useRewardWinsHistory({
range,
resolution,
}: GetRewardWinsHistoryRequest) {
return useData<GetRewardWinsHistoryRequest, GetRewardWinsHistoryResponse>({
method: "GET",
endpoint: "/v1/lottery/reward-wins-history",
queryArgs: {
range,
resolution,
},
});
}
24 changes: 16 additions & 8 deletions frontend/src/models/status.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
export interface GetResponse {
import { useData } from "../hooks/useData";

interface GetResponse {
version: string;
downgradedTools: string[];
unavaliableTools: string[];
}

export enum ToolStatusEnum {
NORMAL = "NORMAL",
UNAVALIABLE = "UNAVAILABLE",
DOWNGRADED = "DOWNGRADED",
export function useStatus() {
return useData<Record<string, never>, GetResponse>({
method: "GET",
endpoint: "/v1/status",
});
}

export interface GetToolStatusResponse {
status: ToolStatusEnum;
interface GetToolStatusResponse {
status: "NORMAL" | "UNAVAILABLE" | "DOWNGRADED";
reason: string | null;
lastUpdateTime: number | null;
dataUpdateFreq: string | null;
dataCount: number | null;
dataSource: Record<string, string> | null;
}
export function useToolStatus({ toolSlug }: { toolSlug: string }) {
return useData<Record<string, never>, GetToolStatusResponse>({
method: "GET",
endpoint: `/v1/status/${toolSlug}`,
});
}
Loading

0 comments on commit b0ca071

Please sign in to comment.