Skip to content

Commit

Permalink
frontend: update swr configs
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 8, 2023
1 parent df9160e commit f542d5c
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 52 deletions.
3 changes: 2 additions & 1 deletion src/GZCTF/ClientApp/src/components/AppNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ const AppNavbar: FC = () => {
useEffect(() => {
if (location.pathname === '/') {
setActive(items[0].label)
} else {
setActive(getLabel(location.pathname) ?? '')
}
setActive(getLabel(location.pathname) ?? '')
}, [location.pathname])

const links = items
Expand Down
5 changes: 4 additions & 1 deletion src/GZCTF/ClientApp/src/components/ChallengePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ const ChallengePanel: FC = () => {
const { id } = useParams()
const numId = parseInt(id ?? '-1')

const { data } = api.game.useGameChallengesWithTeamInfo(numId)
const { data } = api.game.useGameChallengesWithTeamInfo(numId, {
shouldRetryOnError: false,
})
const challenges = data?.challenges

const { data: game } = api.game.useGameGames(numId, {
refreshInterval: 0,
revalidateOnFocus: false,
})

const tags = Object.keys(challenges ?? {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const MobileScoreboardTable: FC<ScoreboardProps> = ({ organization, setOrganizat

const { data: scoreboard } = api.game.useGameScoreboard(numId, {
refreshInterval: 0,
revalidateOnFocus: false,
})

const filtered =
Expand Down
1 change: 1 addition & 0 deletions src/GZCTF/ClientApp/src/components/ScoreboardTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ const ScoreboardTable: FC<ScoreboardProps> = ({ organization, setOrganization })

const { data: scoreboard } = api.game.useGameScoreboard(numId, {
refreshInterval: 0,
revalidateOnFocus: false,
})

const filtered =
Expand Down
4 changes: 3 additions & 1 deletion src/GZCTF/ClientApp/src/components/TeamRank.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const TeamRank: FC<PaperProps> = (props) => {
const { id } = useParams()
const numId = parseInt(id ?? '-1')
const navigate = useNavigate()
const { data, error } = api.game.useGameChallengesWithTeamInfo(numId)
const { data, error } = api.game.useGameChallengesWithTeamInfo(numId, {
shouldRetryOnError: false,
})

const { classes, theme } = useStyle()

Expand Down
1 change: 1 addition & 0 deletions src/GZCTF/ClientApp/src/components/TimeLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const TimeLine: FC<TimeLineProps> = ({ organization }) => {

const { data: scoreboard } = api.game.useGameScoreboard(numId, {
refreshInterval: 0,
revalidateOnFocus: false,
})

const { data: game } = api.game.useGameGames(numId, {
Expand Down
1 change: 1 addition & 0 deletions src/GZCTF/ClientApp/src/components/WithGameTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const WithGameTab: FC<React.PropsWithChildren> = ({ children }) => {
const { role } = useUserRole()
const { data: game } = api.game.useGameGames(numId, {
refreshInterval: 0,
revalidateOnFocus: false,
})

const finished = dayjs() > dayjs(game?.end ?? new Date())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const BloodBonusModel: FC<ModalProps> = (props) => {
const numId = parseInt(id ?? '-1')
const { data: gameSource, mutate } = api.edit.useEditGetGame(numId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})
const [disabled, setDisabled] = useState(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const FlagCreateModal: FC<ModalProps> = (props) => {

const { data: challenge, mutate } = api.edit.useEditGetGameChallenge(numId, numCId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const GameNoticeEditModal: FC<GameNoticeEditModalProps> = (props) => {
const numId = parseInt(id ?? '-1')
const { gameNotice, mutateGameNotice, ...modalProps } = props

const [content, setContent] = useState<string | undefined>(gameNotice?.content)
const [content, setContent] = useState<string>(gameNotice?.content || '')
const [disabled, setDisabled] = useState(false)

useEffect(() => {
setContent(gameNotice?.content)
setContent(gameNotice?.content || '')
}, [gameNotice])

const onConfirm = () => {
Expand All @@ -43,8 +43,9 @@ const GameNoticeEditModal: FC<GameNoticeEditModalProps> = (props) => {
return
}

setDisabled(true)

if (gameNotice && !disabled) {
setDisabled(true)
api.edit
.editUpdateGameNotice(numId, gameNotice.id, {
content: content,
Expand All @@ -63,7 +64,6 @@ const GameNoticeEditModal: FC<GameNoticeEditModalProps> = (props) => {
setDisabled(false)
})
} else {
// add notice
api.edit
.editAddGameNotice(numId, {
content: content,
Expand All @@ -81,6 +81,7 @@ const GameNoticeEditModal: FC<GameNoticeEditModalProps> = (props) => {
.catch(showErrorNotification)
.finally(() => {
setDisabled(false)
setContent('')
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/GZCTF/ClientApp/src/pages/account/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const Profile: FC = () => {
autoClose: true,
})
setDisabled(false)
mutate({ ...user }, { revalidate: false })
mutate()
setAvatarFile(null)
})
.catch(() => {
Expand Down
1 change: 0 additions & 1 deletion src/GZCTF/ClientApp/src/pages/admin/games/[id]/Info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const GameInfoEdit: FC = () => {
const numId = parseInt(id ?? '-1')
const { data: gameSource, mutate } = api.edit.useEditGetGame(numId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})
const [game, setGame] = useState<GameInfoModel>()
Expand Down
3 changes: 1 addition & 2 deletions src/GZCTF/ClientApp/src/pages/admin/games/[id]/Notices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const GameNoticeEdit: FC = () => {
const numId = parseInt(id ?? '-1')
const { data: gameNotices, mutate } = api.edit.useEditGetGameNotices(numId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

Expand Down Expand Up @@ -78,7 +77,7 @@ const GameNoticeEdit: FC = () => {
>
<ScrollArea pos="relative" h="calc(100vh - 180px)" offsetScrollbars>
{!gameNotices || gameNotices?.length === 0 ? (
<Center h="calc(100vh - 180px)">
<Center h="calc(100vh - 200px)">
<Stack spacing={0}>
<Title order={2}>Ouch! 这个比赛还没有通知</Title>
<Text>安然无事真好!</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const GameWriteups: FC = () => {

const { data: writeups } = api.admin.useAdminWriteups(numId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const GameChallengeEdit: FC = () => {

const { data: challenges, mutate } = api.edit.useEditGetGameChallenges(numId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const OneAttachmentWithFlags: FC<FlagEditProps> = ({ onDelete }) => {

const { data: challenge, mutate } = api.edit.useEditGetGameChallenge(numId, numCId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

Expand Down Expand Up @@ -367,11 +366,7 @@ const FlagsWithAttachments: FC<FlagEditProps> = ({ onDelete }) => {

const theme = useMantineTheme()

const { data: challenge } = api.edit.useEditGetGameChallenge(numId, numCId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})
const { data: challenge } = api.edit.useEditGetGameChallenge(numId, numCId)

const [attachmentUploadModalOpened, setAttachmentUploadModalOpened] = useState(false)
const [remoteAttachmentModalOpened, setRemoteAttachmentModalOpened] = useState(false)
Expand Down Expand Up @@ -424,11 +419,7 @@ const GameChallengeEdit: FC = () => {
const theme = useMantineTheme()
const modals = useModals()

const { data: challenge, mutate } = api.edit.useEditGetGameChallenge(numId, numCId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})
const { data: challenge, mutate } = api.edit.useEditGetGameChallenge(numId, numCId)

const onDeleteFlag = (flag: FlagInfoModel) => {
modals.openConfirmModal({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const GameChallengeEdit: FC = () => {

const { data: challenge, mutate } = api.edit.useEditGetGameChallenge(numId, numCId, {
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

Expand Down Expand Up @@ -94,7 +93,6 @@ const GameChallengeEdit: FC = () => {
})
}
mutate(data.data)
console.log(data.data)
mutateChals()
})
.catch(showErrorNotification)
Expand Down
1 change: 0 additions & 1 deletion src/GZCTF/ClientApp/src/pages/games/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import api from '@Api'
const Games: FC = () => {
const { data: allGames } = api.game.useGameGamesAll({
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

Expand Down
4 changes: 3 additions & 1 deletion src/GZCTF/ClientApp/src/pages/games/[id]/Scoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import api from '@Api'
const Scoreboard: FC = () => {
const { id } = useParams()
const numId = parseInt(id ?? '-1')
const { data, error } = api.game.useGameChallengesWithTeamInfo(numId)
const { data, error } = api.game.useGameChallengesWithTeamInfo(numId, {
shouldRetryOnError: false,
})

const [organization, setOrganization] = useState<string | null>('all')
const isMobile = useIsMobile(1080)
Expand Down
1 change: 0 additions & 1 deletion src/GZCTF/ClientApp/src/pages/posts/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const ITEMS_PER_PAGE = 10
const Posts: FC = () => {
const { data: posts, mutate } = api.info.useInfoGetPosts({
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
})

Expand Down
1 change: 0 additions & 1 deletion src/GZCTF/ClientApp/src/pages/posts/[postId]/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const Post: FC = () => {
postId ?? '',
{
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
},
postId?.length === 8
Expand Down
1 change: 0 additions & 1 deletion src/GZCTF/ClientApp/src/pages/posts/[postId]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const PostEdit: FC = () => {
postId ?? '',
{
refreshInterval: 0,
revalidateIfStale: false,
revalidateOnFocus: false,
},
postId?.length === 8
Expand Down
10 changes: 4 additions & 6 deletions src/GZCTF/ClientApp/src/utils/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ export const localStorageProvider = () => {
const cacheKey = 'gzctf-cache'
const map = new Map(JSON.parse(LZString.decompress(localStorage.getItem(cacheKey) || '') || '[]'))

if (!import.meta.env.DEV) {
window.addEventListener('beforeunload', () => {
const appCache = LZString.compress(JSON.stringify(Array.from(map.entries())))
localStorage.setItem(cacheKey, appCache)
})
}
window.addEventListener('beforeunload', () => {
const appCache = LZString.compress(JSON.stringify(Array.from(map.entries())))
localStorage.setItem(cacheKey, appCache)
})

return map as Cache
}
39 changes: 26 additions & 13 deletions src/GZCTF/ClientApp/src/utils/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const useUser = () => {
mutate,
} = api.account.useAccountProfile({
refreshInterval: 0,
revalidateIfStale: false,
shouldRetryOnError: false,
revalidateOnFocus: false,
onErrorRetry: (err, _key, _config, revalidate, { retryCount }) => {
if (err?.status === 403) {
api.account.accountLogOut().then(() => {
Expand All @@ -30,7 +31,10 @@ export const useUser = () => {

if (err?.status === 401) return

if (retryCount >= 5) return
if (retryCount >= 5) {
mutate(undefined, false)
return
}

setTimeout(() => revalidate({ retryCount: retryCount }), 10000)
},
Expand All @@ -51,27 +55,36 @@ export const useTeams = () => {
mutate,
} = api.team.useTeamGetTeamsInfo({
refreshInterval: 120000,
shouldRetryOnError: false,
revalidateOnFocus: false,
})

return { teams, error, mutate }
}

export const useLoginOut = () => {
const navigate = useNavigate()
const { mutate } = useSWRConfig()
const { mutate: mutateProfile } = api.account.useAccountProfile()
const { mutate: mutateProfile } = useUser()

return () => {
api.account.accountLogOut().then(() => {
navigate('/')
mutate((key) => typeof key === 'string' && key.includes('game/'), undefined, {
revalidate: false,
api.account
.accountLogOut()
.then(() => {
navigate('/')
mutate((key) => typeof key === 'string' && key.includes('game/'), undefined, {
revalidate: false,
})
mutateProfile(undefined, { revalidate: false })
showNotification({
color: 'teal',
message: '登出成功',
icon: <Icon path={mdiCheck} size={1} />,
})
})
mutateProfile(undefined, false)
showNotification({
color: 'teal',
message: '登出成功',
icon: <Icon path={mdiCheck} size={1} />,
.catch(() => {
navigate('/')
mutateProfile(undefined, { revalidate: false })
})
})
}
}

0 comments on commit f542d5c

Please sign in to comment.