Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

feat(mobile): add edit link functionality

daniel31x13822415c6950f
diff --git a/apps/mobile/app/login.tsx b/apps/mobile/app/login.tsx
index 602cf016..4c1c171f 100644
--- a/apps/mobile/app/login.tsx
+++ b/apps/mobile/app/login.tsx
@@ -46,7 +46,7 @@ export default function HomeScreen() {
)
}
>
- Login
+ <Text className="text-white">Login</Text>
</Button>
</View>
</View>
diff --git a/apps/mobile/components/ActionSheets/AddLinkSheet.tsx b/apps/mobile/components/ActionSheets/AddLinkSheet.tsx
index a664730b..07881b04 100644
--- a/apps/mobile/components/ActionSheets/AddLinkSheet.tsx
+++ b/apps/mobile/components/ActionSheets/AddLinkSheet.tsx
@@ -1,4 +1,4 @@
-import { View } from "react-native";
+import { Text, View } from "react-native";
import { useRef, useState } from "react";
import ActionSheet, { ActionSheetRef } from "react-native-actions-sheet";
import Input from "@/components/ui/Input";
@@ -40,7 +40,7 @@ export default function AddLinkSheet() {
variant="accent"
className="mb-2"
>
- Save to Linkwarden
+ <Text className="text-white">Save to Linkwarden</Text>
</Button>
<Button
@@ -51,7 +51,7 @@ export default function AddLinkSheet() {
variant="outline"
className="mb-2"
>
- Cancel
+ <Text>Cancel</Text>
</Button>
</View>
</ActionSheet>
diff --git a/apps/mobile/components/ActionSheets/EditLinkSheet.tsx b/apps/mobile/components/ActionSheets/EditLinkSheet.tsx
index 445fb31b..637bb75d 100644
--- a/apps/mobile/components/ActionSheets/EditLinkSheet.tsx
+++ b/apps/mobile/components/ActionSheets/EditLinkSheet.tsx
@@ -1,64 +1,240 @@
import { View, Text } from "react-native";
-import { useRef, useState } from "react";
+import { useCallback, useEffect, useMemo, useState } from "react";
import ActionSheet, {
- ActionSheetRef,
+ FlatList,
+ Route,
+ SheetManager,
SheetProps,
+ useSheetRouteParams,
+ useSheetRouter,
} from "react-native-actions-sheet";
import Input from "@/components/ui/Input";
import { Button } from "@/components/ui/Button";
-import { useAddLink } from "@linkwarden/router/links";
+import { useAddLink, useUpdateLink } from "@linkwarden/router/links";
import useAuthStore from "@/store/auth";
+import {
+ CollectionIncludingMembersAndLinkCount,
+ LinkIncludingShortenedCollectionAndTags,
+} from "@linkwarden/types";
+import { IconSymbol } from "../ui/IconSymbol";
+import { useCollections } from "@linkwarden/router/collections";
-export default function EditLinkSheet(props: SheetProps<"edit-link-sheet">) {
- const actionSheetRef = useRef<ActionSheetRef>(null);
+const Main = (props: SheetProps<"edit-link-sheet">) => {
const { auth } = useAuthStore();
- const addLink = useAddLink(auth);
- const [link, setLink] = useState("");
+
+ const params = useSheetRouteParams("edit-link-sheet", "main");
+
+ const [link, setLink] = useState<
+ LinkIncludingShortenedCollectionAndTags | undefined
+ >(props.payload?.link);
+ const editLink = useUpdateLink(auth);
+ const router = useSheetRouter("edit-link-sheet");
+
+ useEffect(() => {
+ if (params?.link) {
+ setLink(params.link);
+ }
+ }, [params?.link]);
return (
- <ActionSheet ref={actionSheetRef} gestureEnabled>
- <View className="px-8 py-5">
+ <View className="px-8 py-5">
+ <Input
+ placeholder="Name"
+ className="mb-4"
+ value={link?.name || ""}
+ onChangeText={(text) => link?.id && setLink({ ...link, name: text })}
+ />
+
+ {props.payload?.link?.url && (
<Input
- placeholder="e.g. https://example.com"
+ placeholder="URL"
className="mb-4"
- value={link}
- onChangeText={setLink}
+ value={link?.url || ""}
+ onChangeText={(text) => link?.id && setLink({ ...link, url: text })}
/>
+ )}
- <Text>{props.payload?.id}</Text>
-
- <Button
- onPress={() =>
- addLink.mutate(
- { url: link },
- {
- onSuccess: () => {
- actionSheetRef.current?.hide();
- setLink("");
- },
- onError: (error) => {
- console.error("Error adding link:", error);
- },
- }
- )
- }
- variant="accent"
- className="mb-2"
- >
- Save to Linkwarden
- </Button>
+ <Button
+ variant="input"
+ className="mb-4"
+ onPress={() => router?.navigate("collections", { link })}
+ >
+ <View className="flex-row items-center gap-2 w-[90%]">
+ <IconSymbol
+ size={20}
+ name="folder.fill"
+ color={link?.collection.color || "gray"}
+ />
+ <Text numberOfLines={1} className="w-[90%]">
+ {link?.collection.name}
+ </Text>
+ </View>
+ <IconSymbol size={16} name="chevron.right" color={"gray"} />
+ </Button>
+
+ {/* <Button variant="input" className="mb-4 h-auto">
+ {link?.tags && link?.tags.length > 0 ? (
+ <View className="flex-row flex-wrap items-center gap-2 w-[90%]">
+ {link.tags.map((tag) => (
+ <View
+ key={tag.id}
+ className="bg-gray-200 rounded-md h-7 px-2 py-1"
+ >
+ <Text numberOfLines={1}>{tag.name}</Text>
+ </View>
+ ))}
+ </View>
+ ) : (
+ <Text className="text-gray-500">No tags</Text>
+ )}
+ <IconSymbol size={16} name="chevron.right" color={"gray"} />
+ </Button> */}
- <Button
- onPress={() => {
- actionSheetRef.current?.hide();
- setLink("");
- }}
- variant="outline"
- className="mb-2"
- >
- Cancel
+ <Input
+ multiline
+ placeholder="Description"
+ className="mb-4 h-28"
+ value={link?.description || ""}
+ onChangeText={(text) =>
+ link?.id && setLink({ ...link, description: text })
+ }
+ />
+
+ <Button
+ onPress={() =>
+ editLink.mutate(link as LinkIncludingShortenedCollectionAndTags, {
+ onSuccess: () => {
+ SheetManager.hide("edit-link-sheet");
+ },
+ onError: (error) => {
+ console.error("Error adding link:", error);
+ },
+ })
+ }
+ variant="accent"
+ className="mb-2"
+ >
+ <Text className="text-white">Save</Text>
+ </Button>
+
+ <Button
+ onPress={() => {
+ SheetManager.hide("edit-link-sheet");
+ }}
+ variant="outline"
+ className="mb-2"
+ >
+ <Text>Cancel</Text>
+ </Button>
+ </View>
+ );
+};
+
+const Collections = () => {
+ const { auth } = useAuthStore();
+ const addLink = useAddLink(auth);
+ const [searchQuery, setSearchQuery] = useState("");
+ const router = useSheetRouter("edit-link-sheet");
+ const { link: currentLink } = useSheetRouteParams<
+ "edit-link-sheet",
+ "collections"
+ >("edit-link-sheet", "collections");
+ const params = useSheetRouteParams("edit-link-sheet", "collections");
+ const collections = useCollections(auth);
+
+ const filteredCollections = useMemo(() => {
+ if (!collections.data) return [];
+ const q = searchQuery.trim().toLowerCase();
+ if (q === "") return collections.data;
+ return collections.data.filter((col) => col.name.toLowerCase().includes(q));
+ }, [collections.data, searchQuery]);
+
+ const renderItem = useCallback(
+ ({
+ item: collection,
+ }: {
+ item: CollectionIncludingMembersAndLinkCount;
+ }) => {
+ const onSelect = () => {
+ // 1. Create a brand-new link object with the new collection
+ const updatedLink = {
+ ...currentLink!,
+ collection,
+ };
+
+ // 2. Navigate back to "main", passing the updated link as payload
+ router?.popToTop();
+ router?.navigate("main", { link: updatedLink });
+ };
+
+ return (
+ <Button variant="input" className="mb-2" onPress={onSelect}>
+ <View className="flex-row items-center gap-2 w-[75%]">
+ <IconSymbol
+ size={20}
+ name="folder.fill"
+ color={collection.color || "gray"}
+ />
+ <Text numberOfLines={1} className="w-full">
+ {collection.name}
+ </Text>
+ </View>
+ <View className="flex-row items-center gap-2">
+ {params.link?.collection.id === collection.id && (
+ <IconSymbol size={16} name="checkmark" color="" />
+ )}
+ <Text className="text-gray-600">
+ {collection._count?.links ?? 0}
+ </Text>
+ </View>
</Button>
- </View>
- </ActionSheet>
+ );
+ },
+ [addLink, params.link, router]
+ );
+
+ return (
+ <View className="px-8 py-5 max-h-[80vh]">
+ <Input
+ placeholder="Search collections"
+ className="mb-4"
+ value={searchQuery}
+ onChangeText={setSearchQuery}
+ />
+
+ <FlatList
+ data={filteredCollections}
+ keyExtractor={(e, i) => i.toString()}
+ renderItem={renderItem}
+ ListEmptyComponent={
+ <Text style={{ textAlign: "center", color: "#666", marginTop: 20 }}>
+ No collections match “{searchQuery}”
+ </Text>
+ }
+ contentContainerStyle={{ paddingBottom: 20 }}
+ />
+ </View>
+ );
+};
+
+const routes: Route[] = [
+ {
+ name: "main",
+ component: Main,
+ },
+ {
+ name: "collections",
+ component: Collections,
+ },
+];
+
+export default function EditLinkSheet() {
+ return (
+ <ActionSheet
+ gestureEnabled
+ enableRouterBackNavigation={true}
+ routes={routes}
+ initialRoute="main"
+ />
);
}
diff --git a/apps/mobile/components/ActionSheets/Sheets.tsx b/apps/mobile/components/ActionSheets/Sheets.tsx
index a977888b..56af9d6c 100644
--- a/apps/mobile/components/ActionSheets/Sheets.tsx
+++ b/apps/mobile/components/ActionSheets/Sheets.tsx
@@ -5,6 +5,10 @@ import {
} from "react-native-actions-sheet";
import AddLinkSheet from "./AddLinkSheet";
import EditLinkSheet from "./EditLinkSheet";
+import {
+ CollectionIncludingMembersAndLinkCount,
+ LinkIncludingShortenedCollectionAndTags,
+} from "@linkwarden/types";
registerSheet("add-link-sheet", AddLinkSheet);
registerSheet("edit-link-sheet", EditLinkSheet);
@@ -14,14 +18,16 @@ declare module "react-native-actions-sheet" {
"add-link-sheet": SheetDefinition;
"edit-link-sheet": SheetDefinition<{
payload: {
- id: number;
+ link: LinkIncludingShortenedCollectionAndTags;
+ };
+ routes: {
+ main: RouteDefinition<{
+ link: LinkIncludingShortenedCollectionAndTags;
+ }>;
+ collections: RouteDefinition<{
+ link: LinkIncludingShortenedCollectionAndTags;
+ }>;
};
- // routes: {
- // main: RouteDefinition;
- // collections: RouteDefinition<{
- // data: string;
- // }>;
- // };
}>;
}
}
diff --git a/apps/mobile/components/LinkListing.tsx b/apps/mobile/components/LinkListing.tsx
index e262fec2..126a84ab 100644
--- a/apps/mobile/components/LinkListing.tsx
+++ b/apps/mobile/components/LinkListing.tsx
@@ -125,7 +125,7 @@ const LinkListing = ({ link }: Props) => {
key="edit-link"
onSelect={() => {
SheetManager.show("edit-link-sheet", {
- payload: { id: link.id as number },
+ payload: { link: link },
});
}}
>
diff --git a/apps/mobile/components/ui/Button.tsx b/apps/mobile/components/ui/Button.tsx
index fb5e52c1..d9fde168 100644
--- a/apps/mobile/components/ui/Button.tsx
+++ b/apps/mobile/components/ui/Button.tsx
@@ -14,12 +14,14 @@ const buttonVariants = cva(
{
variants: {
variant: {
- default: "bg-slate-500",
+ default: "bg-slate-500 text-white",
primary: "bg-primary text-primary-foreground",
- accent: "bg-violet-600 border border-violet-400",
- destructive: "bg-destructive",
+ accent: "bg-violet-600 border border-violet-400 text-white",
+ destructive: "bg-destructive text-white",
outline: "border",
secondary: "bg-secondary text-secondary-foreground",
+ input:
+ "bg-gray-100 rounded-lg px-4 justify-between flex-row font-normal",
metal: "bg-neutral-content text-base-content border border-neutral/30",
ghost: "",
simple: "bg-base-200",
@@ -42,16 +44,8 @@ const buttonVariants = cva(
export type ButtonProps = TouchableOpacityProps &
VariantProps<typeof buttonVariants> & {
- /**
- * Render this instead of children when loading.
- * Disables the button while true.
- */
isLoading?: boolean;
- /** Optional icon element, placed before the text */
- icon?: React.ReactNode;
- /** Contents of the button */
children: React.ReactNode;
- /** Merge additional Tailwind classes */
className?: string;
};
@@ -65,7 +59,6 @@ export const Button = forwardRef<
size,
className,
isLoading = false,
- icon,
children,
disabled,
...props
@@ -82,25 +75,7 @@ export const Button = forwardRef<
disabled={disabled || isLoading}
{...props}
>
- {isLoading ? (
- <ActivityIndicator />
- ) : (
- <View className="flex-row items-center">
- {icon && <View className="mr-2">{icon}</View>}
- <Text
- className={cn(
- "text-sm font-medium",
- variant === "default" ||
- variant === "accent" ||
- variant === "destructive"
- ? "text-white"
- : "text-black"
- )}
- >
- {children}
- </Text>
- </View>
- )}
+ {isLoading ? <ActivityIndicator /> : children}
</TouchableOpacity>
);
}
diff --git a/packages/router/collections.tsx b/packages/router/collections.tsx
index 6b0dce2a..b1499be9 100644
--- a/packages/router/collections.tsx
+++ b/packages/router/collections.tsx
@@ -1,14 +1,33 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
-import { CollectionIncludingMembersAndLinkCount } from "@linkwarden/types";
+import {
+ CollectionIncludingMembersAndLinkCount,
+ MobileAuth,
+} from "@linkwarden/types";
import { useSession } from "next-auth/react";
-const useCollections = () => {
- const { status } = useSession();
+const useCollections = (auth?: MobileAuth) => {
+ let status: "loading" | "authenticated" | "unauthenticated";
+
+ if (!auth) {
+ const session = useSession();
+ status = session.status;
+ } else {
+ status = auth?.status;
+ }
return useQuery({
queryKey: ["collections"],
queryFn: async (): Promise<CollectionIncludingMembersAndLinkCount[]> => {
- const response = await fetch("/api/v1/collections");
+ const response = await fetch(
+ (auth?.instance ? auth?.instance : "") + "/api/v1/collections",
+ auth?.session
+ ? {
+ headers: {
+ Authorization: `Bearer ${auth.session}`,
+ },
+ }
+ : undefined
+ );
const data = await response.json();
return data.response;
},
diff --git a/packages/router/links.tsx b/packages/router/links.tsx
index 502c650c..09af88f4 100644
--- a/packages/router/links.tsx
+++ b/packages/router/links.tsx
@@ -168,18 +168,24 @@ const useAddLink = (auth?: MobileAuth) => {
});
};
-const useUpdateLink = () => {
+const useUpdateLink = (auth?: MobileAuth) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => {
- const response = await fetch(`/api/v1/links/${link.id}`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(link),
- });
+ const response = await fetch(
+ (auth?.instance ? auth?.instance : "") + `/api/v1/links/${link.id}`,
+ {
+ method: "PUT",
+ headers: {
+ "Content-Type": "application/json",
+ ...(auth?.session
+ ? { Authorization: `Bearer ${auth.session}` }
+ : {}),
+ },
+ body: JSON.stringify(link),
+ }
+ );
const data = await response.json();