forked from dubinc/dub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink-card.tsx
155 lines (142 loc) · 5 KB
/
link-card.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import BlurImage from "@/components/shared/blur-image";
import CopyButton from "@/components/shared/copy-button";
import { Chart, LoadingDots } from "@/components/shared/icons";
import useSWR from "swr";
import { fetcher, nFormatter, linkConstructor } from "@/lib/utils";
import Link from "next/link";
import { motion, useMotionValue, useAnimation } from "framer-motion";
import { FRAMER_MOTION_LIST_ITEM_VARIANTS } from "@/lib/constants";
import { SimpleLinkProps } from "@/lib/types";
import toast from "react-hot-toast";
import { useDebouncedCallback } from "use-debounce";
export default function LinkCard({
_key: key,
url,
hashes,
setHashes,
}: {
_key: string;
url: string;
hashes?: SimpleLinkProps[];
setHashes?: (hashes: SimpleLinkProps[]) => void;
}) {
const urlHostname = new URL(url).hostname;
const { data: clicks, isValidating } = useSWR<string>(
`/api/edge/links/${key}/clicks`,
fetcher
);
const cardElem = useRef(null);
const x = useMotionValue(0);
const controls = useAnimation();
const [constrained, setConstrained] = useState(true);
const [velocity, setVelocity] = useState<number>();
const isDelete = (childNode, parentNode) => {
const childRect = childNode.getBoundingClientRect();
const parentRect = parentNode.getBoundingClientRect();
return parentRect.left >= childRect.right ||
parentRect.right <= childRect.left
? true
: undefined;
};
// determine direction of swipe based on velocity
const direction = useMemo(() => {
return velocity >= 1 ? "right" : velocity <= -1 ? "left" : undefined;
}, [velocity]);
const flyAway = (min) => {
const flyAwayDistance = (direction) => {
const parentWidth =
cardElem.current.parentNode.getBoundingClientRect().width;
const childWidth = cardElem.current.getBoundingClientRect().width;
return direction === "left"
? -parentWidth / 2 - childWidth / 2
: parentWidth / 2 + childWidth / 2;
};
console.log("velocity", velocity, "direction", direction);
if (direction && Math.abs(velocity) > min) {
console.log("flying away");
setConstrained(false);
controls.start({
x: flyAwayDistance(direction),
});
}
};
const sendErrorToast = useDebouncedCallback(
() => toast.error("Cannot delete default link."),
100
);
useEffect(() => {
const unsubscribeX = x.onChange(() => {
if (cardElem.current) {
const childNode = cardElem.current;
const parentNode = cardElem.current.parentNode;
const deleted = isDelete(childNode, parentNode);
if (deleted) {
if (hashes && setHashes) {
setHashes(hashes.filter((hash) => hash.key !== key));
toast.success("Link deleted.");
} else {
sendErrorToast(); // debounce to prevent multiple toasts
}
}
}
});
return () => unsubscribeX();
});
return (
<motion.li variants={FRAMER_MOTION_LIST_ITEM_VARIANTS}>
<motion.div
animate={controls}
drag="x"
dragConstraints={constrained && { left: 0, right: 0 }}
dragElastic={1}
ref={cardElem}
style={{ x }}
onDrag={() => setVelocity(x.getVelocity())}
onDragEnd={() => flyAway(500)}
whileTap={{ scale: 1.05 }}
className="cursor-grab active:cursor-grabbing flex items-center border border-gray-200 hover:border-black bg-white p-3 max-w-md rounded-md transition-[border-color]"
>
<BlurImage
src={`https://logo.clearbit.com/${urlHostname}`}
alt={urlHostname}
className="w-10 h-10 rounded-full mr-2 border border-gray-200 pointer-events-none"
width={20}
height={20}
/>
<div>
<div className="flex items-center space-x-2 mb-1">
<a
className="text-blue-800 font-semibold"
href={linkConstructor({ key })}
target="_blank"
rel="noreferrer"
>
{linkConstructor({ key, pretty: true })}
</a>
<CopyButton url={linkConstructor({ key })} />
<Link
href={{ pathname: "/", query: { key } }}
as={`/stats/${encodeURI(key)}`}
shallow
scroll={false}
>
<a className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 hover:scale-105 active:scale-95 transition-all duration-75 text-gray-700">
<Chart className="w-4 h-4" />
<p className="text-sm">
{isValidating || !clicks ? (
<LoadingDots color="#71717A" />
) : (
nFormatter(parseInt(clicks))
)}{" "}
clicks
</p>
</a>
</Link>
</div>
<p className="text-sm text-gray-500 truncate w-72">{url}</p>
</div>
</motion.div>
</motion.li>
);
}