|
| 1 | +import { Platform, ScrollView, StyleSheet, Text, View } from "react-native"; |
| 2 | +import { Header } from "@/components/Header"; |
| 3 | +import { useRef, useState } from "react"; |
| 4 | +import { Card } from "@/components/Card"; |
| 5 | +import { fontStyles } from "@/styles/font"; |
| 6 | +import { IconArrowSmRight } from "@/assets/images/IconArrowSmRight"; |
| 7 | +import { Code } from "@/components/Code"; |
| 8 | +import { Logs } from "@/components/Logs"; |
| 9 | +import BottomSheet, { BottomSheetView } from "@gorhom/bottom-sheet"; |
| 10 | +import { GestureHandlerRootView } from "react-native-gesture-handler"; |
| 11 | +import { Log } from "@/types/log"; |
| 12 | +import { AppwriteException, Client } from "appwrite"; |
| 13 | + |
| 14 | +const client = new Client() |
| 15 | + .setProject(process.env.EXPO_PUBLIC_APPWRITE_PROJECT_ID ?? "") |
| 16 | + .setEndpoint(process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT ?? ""); |
| 17 | + |
| 18 | +export default function HomeScreen() { |
| 19 | + const [connectionState, setConnectionState] = useState< |
| 20 | + "idle" | "loading" | "success" | "error" |
| 21 | + >("idle"); |
| 22 | + const bottomSheetRef = useRef<BottomSheet>(null); |
| 23 | + const [currentSnapIndex, setCurrentSnapIndex] = useState<number>(0); |
| 24 | + const [logs, setLogs] = useState<Array<Log>>([]); |
| 25 | + |
| 26 | + const doPing = async () => { |
| 27 | + setConnectionState("loading"); |
| 28 | + let log: Log; |
| 29 | + try { |
| 30 | + const res = await client.ping(); |
| 31 | + log = { |
| 32 | + date: new Date(), |
| 33 | + method: "GET", |
| 34 | + path: "/v1/ping", |
| 35 | + status: 200, |
| 36 | + response: res, |
| 37 | + }; |
| 38 | + setConnectionState("success"); |
| 39 | + } catch (err) { |
| 40 | + log = { |
| 41 | + date: new Date(), |
| 42 | + method: "GET", |
| 43 | + path: "/v1/ping", |
| 44 | + status: err instanceof AppwriteException ? err.code : 500, |
| 45 | + response: err instanceof AppwriteException ? err.message : "unknown", |
| 46 | + }; |
| 47 | + setConnectionState("error"); |
| 48 | + } |
| 49 | + setLogs([...logs, log]); |
| 50 | + }; |
| 51 | + |
| 52 | + const toggleBottomSheet = () => { |
| 53 | + if (bottomSheetRef.current) { |
| 54 | + const newIndex = currentSnapIndex === 1 ? 0 : 1; |
| 55 | + setCurrentSnapIndex(newIndex); |
| 56 | + bottomSheetRef.current.snapToIndex(newIndex); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const handleSnapChange = (index: number) => { |
| 61 | + setCurrentSnapIndex(index); |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <View style={{ flex: 1 }}> |
| 66 | + <GestureHandlerRootView> |
| 67 | + <ScrollView> |
| 68 | + <Header pingFunction={doPing} state={connectionState} /> |
| 69 | + <View style={styles.cardContainer}> |
| 70 | + <Card> |
| 71 | + <View style={styles.cardHeader}> |
| 72 | + <Text style={fontStyles.titleM}>Edit your app</Text> |
| 73 | + </View> |
| 74 | + <Text> |
| 75 | + <Code variant={"secondary"}>Edit </Code> |
| 76 | + <Code variant={"primary"}>app/index.tsx</Code> |
| 77 | + <Code variant={"secondary"}> to get started with building</Code> |
| 78 | + <Code variant={"secondary"}> |
| 79 | + your app and many more to come |
| 80 | + </Code> |
| 81 | + </Text> |
| 82 | + </Card> |
| 83 | + <Card href={"https://cloud.appwrite.io"}> |
| 84 | + <View style={styles.cardHeader}> |
| 85 | + <Text style={fontStyles.titleM}>Go to console</Text> |
| 86 | + <IconArrowSmRight /> |
| 87 | + </View> |
| 88 | + <Text style={fontStyles.bodyM}> |
| 89 | + Navigate to the console to control and oversee the Appwrite |
| 90 | + services. |
| 91 | + </Text> |
| 92 | + </Card> |
| 93 | + <Card href={"https://appwrite.io/docs"}> |
| 94 | + <View style={styles.cardHeader}> |
| 95 | + <Text style={fontStyles.titleM}>Explore docs</Text> |
| 96 | + <IconArrowSmRight /> |
| 97 | + </View> |
| 98 | + <Text style={fontStyles.bodyM}> |
| 99 | + Discover the full power of Appwrite by diving into our |
| 100 | + documentation. |
| 101 | + </Text> |
| 102 | + </Card> |
| 103 | + </View> |
| 104 | + </ScrollView> |
| 105 | + <BottomSheet |
| 106 | + index={0} |
| 107 | + snapPoints={[Platform.OS === "android" ? 50 : 70, "50%", "90%"]} |
| 108 | + enablePanDownToClose={false} |
| 109 | + handleComponent={null} |
| 110 | + ref={bottomSheetRef} |
| 111 | + onChange={handleSnapChange} |
| 112 | + > |
| 113 | + <BottomSheetView style={styles.bottomSheet}> |
| 114 | + <Logs |
| 115 | + toggleBottomSheet={toggleBottomSheet} |
| 116 | + isOpen={currentSnapIndex > 0} |
| 117 | + logs={logs} |
| 118 | + /> |
| 119 | + </BottomSheetView> |
| 120 | + </BottomSheet> |
| 121 | + </GestureHandlerRootView> |
| 122 | + </View> |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +const styles = StyleSheet.create({ |
| 127 | + bottomSheet: { |
| 128 | + borderTopWidth: 1, |
| 129 | + minHeight: Platform.OS === "android" ? 50 : 70, |
| 130 | + flex: 1, |
| 131 | + borderColor: "#EDEDF0", |
| 132 | + }, |
| 133 | + cardContainer: { |
| 134 | + paddingInline: 20, |
| 135 | + display: "flex", |
| 136 | + gap: 16, |
| 137 | + }, |
| 138 | + scrollview: { |
| 139 | + height: 200, |
| 140 | + }, |
| 141 | + cardHeader: { |
| 142 | + display: "flex", |
| 143 | + justifyContent: "space-between", |
| 144 | + flexDirection: "row", |
| 145 | + alignItems: "center", |
| 146 | + marginBottom: 8, |
| 147 | + }, |
| 148 | + editDescription: { |
| 149 | + display: "flex", |
| 150 | + flexDirection: "row", |
| 151 | + justifyContent: "flex-start", |
| 152 | + }, |
| 153 | +}); |
0 commit comments