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

feat: add detail page for MFA item #47

Merged
merged 2 commits into from
Jan 15, 2025
Merged
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
3 changes: 1 addition & 2 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {ActionSheetProvider} from "@expo/react-native-action-sheet";
import AsyncStorage from "@react-native-async-storage/async-storage";

import "./i18n";
import Header from "./Header";

import NavigationBar from "./NavigationBar";
import {db} from "./db/client";
import migrations from "./drizzle/migrations";
Expand Down Expand Up @@ -105,7 +105,6 @@ const App = () => {
<ActionSheetProvider>
<NavigationContainer>
<PaperProvider>
<Header />
<NavigationBar />
</PaperProvider>
</NavigationContainer>
Expand Down
58 changes: 40 additions & 18 deletions EnterAccountDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,26 @@ const EnterAccountDetails = ({onClose, onAdd, validateSecret}) => {
<Button
onPress={openMenu}
mode="outlined"
icon="chevron-down"
contentStyle={styles.menuButtonContent}
style={styles.menuButton}
>
{t(`editAccount.${selectedItem}`)}
<IconButton
icon={selectedItem === "Time based" ? "clock-outline" : "counter"}
size={25}
iconColor="#8A7DF7"
style={styles.icon}
/>
</Button>
}
contentStyle={styles.menuContent}
>
<Menu.Item
leadingIcon="clock-outline"
onPress={() => handleMenuItemPress("Time based")}
title={t("editAccount.Time based")}
/>
<Menu.Item
leadingIcon="counter"
onPress={() => handleMenuItemPress("Counter based")}
title={t("editAccount.Counter based")}
/>
Expand Down Expand Up @@ -210,40 +216,56 @@ const styles = {
},
buttonContainer: {
flexDirection: "row",
justifyContent: "space-between",
marginTop: 10,
alignItems: "center",
marginTop: 20,
gap: 12,
},
menuButton: {
flex: 1,
marginRight: 10,
height: 50,
justifyContent: "center",
fontSize: 12,
width: 80,
height: 45,
borderColor: "#8A7DF7",
borderWidth: 1,
borderRadius: 22,
backgroundColor: "rgba(138, 125, 247, 0.05)",
padding: 0,
margin: 0,
},
menuButtonContent: {
height: 50,
justifyContent: "center",
margin: 0,
padding: 0,
},
icon: {
margin: 0,
padding: 0,
},
menuContent: {
backgroundColor: "#FFFFFF",
borderRadius: 8,
borderRadius: 12,
elevation: 3,
shadowColor: "#000000",
shadowOffset: {width: 0, height: 2},
shadowOpacity: 0.2,
shadowRadius: 3,
shadowOffset: {width: 0, height: 4},
shadowOpacity: 0.15,
shadowRadius: 8,
marginTop: 4,
},
addButton: {
flex: 1,
backgroundColor: "#8A7DF7",
height: 50,
height: 45,
justifyContent: "center",
paddingHorizontal: 5,
borderRadius: 22,
elevation: 2,
shadowColor: "#8A7DF7",
shadowOffset: {width: 0, height: 4},
shadowOpacity: 0.2,
shadowRadius: 8,
},
buttonLabel: {
fontSize: 14,
fontSize: 15,
color: "white",
textAlign: "center",
fontWeight: "600",
letterSpacing: 0.5,
},
};

Expand Down
165 changes: 92 additions & 73 deletions HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Animated, {
withTiming
} from "react-native-reanimated";
import {MaterialCommunityIcons} from "@expo/vector-icons";
import {useNavigation} from "@react-navigation/native";

import SearchBar from "./SearchBar";
import EnterAccountDetails from "./EnterAccountDetails";
Expand All @@ -35,8 +36,7 @@ import EditAccountDetails from "./EditAccountDetails";
import AvatarWithFallback from "./AvatarWithFallback";
import {useImportManager} from "./ImportManager";
import useStore from "./useStorage";
import {calculateCountdown} from "./totpUtil";
import {generateToken, validateSecret} from "./totpUtil";
import {useTokenRefresh, validateSecret} from "./totpUtil";
import {useAccountSync, useAccounts, useEditAccount} from "./useAccountStore";

const {width, height} = Dimensions.get("window");
Expand Down Expand Up @@ -77,6 +77,7 @@ export default function HomePage() {
}, () => {
setShowScanner(true);
});
const navigation = useNavigation();

useEffect(() => {
setCanSync(Boolean(isConnected && userInfo && serverUrl));
Expand Down Expand Up @@ -259,6 +260,94 @@ export default function HomePage() {
);
};

const handleItemPress = (item) => {
navigation.navigate("ItemDetailPage", {
item: {
...item,
changedAt: item.changedAt.toISOString(),
},
});
};

const ListItem = ({item, onPress}) => {
const {token, timeRemaining} = useTokenRefresh(item.secretKey);

return (
<GestureHandlerRootView>
<Swipeable
ref={swipeableRef}
renderRightActions={(progress, dragX) =>
renderRightActions(progress, dragX, item, handleEditAccount, onAccountDelete)
}
rightThreshold={40}
overshootRight={false}
friction={2}
enableTrackpadTwoFingerGesture
onSwipeableOpen={() => {
if (swipeableRef.current) {
swipeableRef.current.close();
}
}}
>
<List.Item
style={{
height: 80,
paddingVertical: 6,
paddingHorizontal: 16,
justifyContent: "center",
}}
title={
<View style={{justifyContent: "center", paddingLeft: 0, paddingTop: 6}}>
<Text variant="titleMedium" numberOfLines={1}>
{item.accountName}
</Text>
<Text variant="titleLarge" style={{fontWeight: "bold"}}>{token}</Text>
</View>
}
left={() => (
<AvatarWithFallback
source={{uri: `https://cdn.casbin.org/img/social_${item.issuer?.toLowerCase()}.png`}}
fallbackSource={{uri: "https://cdn.casbin.org/img/social_default.png"}}
size={60}
style={{
borderRadius: 10,
backgroundColor: "transparent",
}}
/>
)}
right={() => (
<View style={{justifyContent: "center", alignItems: "center"}}>
<CountdownCircleTimer
key={key}
isPlaying={true}
duration={30}
initialRemainingTime={timeRemaining}
colors={["#004777", "#0072A0", "#0099CC", "#FF6600", "#CC3300", "#A30000"]}
colorsTime={[30, 24, 18, 12, 6, 0]}
size={60}
onComplete={() => {
setKey(prevKey => prevKey + 1);
return {
shouldRepeat: true,
delay: 0,
newInitialRemainingTime: timeRemaining,
};
}}
strokeWidth={5}
>
{({remainingTime}) => (
<Text style={{fontSize: 18, fontWeight: "bold"}}>{remainingTime}s</Text>
)}
</CountdownCircleTimer>
</View>
)}
onPress={() => handleItemPress(item)}
/>
</Swipeable>
</GestureHandlerRootView>
);
};

return (
<View style={{flex: 1}}>
<SearchBar onSearch={handleSearch} />
Expand All @@ -271,77 +360,7 @@ export default function HomePage() {
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}
renderItem={({item}) => (
<GestureHandlerRootView>
<Swipeable
ref={swipeableRef}
renderRightActions={(progress, dragX) =>
renderRightActions(progress, dragX, item, handleEditAccount, onAccountDelete)
}
rightThreshold={40}
overshootRight={false}
friction={2}
enableTrackpadTwoFingerGesture
onSwipeableOpen={() => {
if (swipeableRef.current) {
swipeableRef.current.close();
}
}}
>
<List.Item
style={{
height: 80,
paddingVertical: 6,
paddingHorizontal: 16,
justifyContent: "center",
}}
title={
<View style={{justifyContent: "center", paddingLeft: 0, paddingTop: 6}}>
<Text variant="titleMedium" numberOfLines={1}>
{item.accountName}
</Text>
<Text variant="titleLarge" style={{fontWeight: "bold"}}>{generateToken(item.secretKey)}</Text>
</View>
}
left={() => (
<AvatarWithFallback
source={{uri: `https://cdn.casbin.org/img/social_${item.issuer?.toLowerCase()}.png`}}
fallbackSource={{uri: "https://cdn.casbin.org/img/social_default.png"}}
size={60}
style={{
borderRadius: 10,
backgroundColor: "transparent",
}}
/>
)}
right={() => (
<View style={{justifyContent: "center", alignItems: "center"}}>
<CountdownCircleTimer
key={key}
isPlaying={true}
duration={30}
initialRemainingTime={calculateCountdown()}
colors={["#004777", "#0072A0", "#0099CC", "#FF6600", "#CC3300", "#A30000"]}
colorsTime={[30, 24, 18, 12, 6, 0]}
size={60}
onComplete={() => {
setKey(prevKey => prevKey + 1);
return {
shouldRepeat: true,
delay: 0,
newInitialRemainingTime: calculateCountdown(),
};
}}
strokeWidth={5}
>
{({remainingTime}) => (
<Text style={{fontSize: 18, fontWeight: "bold"}}>{remainingTime}s</Text>
)}
</CountdownCircleTimer>
</View>
)}
/>
</Swipeable>
</GestureHandlerRootView>
<ListItem item={item} onPress={handleItemPress} />
)}
ItemSeparatorComponent={() => <Divider />}
/>
Expand Down
40 changes: 40 additions & 0 deletions HomeStackNavigator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2025 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import React from "react";
import {createStackNavigator} from "@react-navigation/stack";
import HomePage from "./HomePage";
import ItemDetailPage from "./ItemDetailPage";
import Header from "./Header";

const Stack = createStackNavigator();

export default function HomeStackNavigator() {
return (
<Stack.Navigator
screenOptions={{
header: (props) => <Header {...props} />,
}}
>
<Stack.Screen name="HomePage" component={HomePage} />
<Stack.Screen
name="ItemDetailPage"
component={ItemDetailPage}
options={({route}) => ({
headerShown: false,
})}
/>
</Stack.Navigator>
);
}
Loading
Loading