mirror of https://github.com/MaxLeiter/Drift
client: lint with useTabs
parent
48a8e9f6a9
commit
60f2ab99b3
@ -1,39 +1,39 @@
|
|||||||
export default function generateUUID() {
|
export default function generateUUID() {
|
||||||
if (typeof crypto === "object") {
|
if (typeof crypto === "object") {
|
||||||
if (typeof crypto.randomUUID === "function") {
|
if (typeof crypto.randomUUID === "function") {
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID
|
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID
|
||||||
return crypto.randomUUID()
|
return crypto.randomUUID()
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
typeof crypto.getRandomValues === "function" &&
|
typeof crypto.getRandomValues === "function" &&
|
||||||
typeof Uint8Array === "function"
|
typeof Uint8Array === "function"
|
||||||
) {
|
) {
|
||||||
// https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid
|
// https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid
|
||||||
const callback = (c: string) => {
|
const callback = (c: string) => {
|
||||||
const num = Number(c)
|
const num = Number(c)
|
||||||
return (
|
return (
|
||||||
num ^
|
num ^
|
||||||
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))
|
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))
|
||||||
).toString(16)
|
).toString(16)
|
||||||
}
|
}
|
||||||
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, callback)
|
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, callback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let timestamp = new Date().getTime()
|
let timestamp = new Date().getTime()
|
||||||
let perforNow =
|
let perforNow =
|
||||||
(typeof performance !== "undefined" &&
|
(typeof performance !== "undefined" &&
|
||||||
performance.now &&
|
performance.now &&
|
||||||
performance.now() * 1000) ||
|
performance.now() * 1000) ||
|
||||||
0
|
0
|
||||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
||||||
let random = Math.random() * 16
|
let random = Math.random() * 16
|
||||||
if (timestamp > 0) {
|
if (timestamp > 0) {
|
||||||
random = (timestamp + random) % 16 | 0
|
random = (timestamp + random) % 16 | 0
|
||||||
timestamp = Math.floor(timestamp / 16)
|
timestamp = Math.floor(timestamp / 16)
|
||||||
} else {
|
} else {
|
||||||
random = (perforNow + random) % 16 | 0
|
random = (perforNow + random) % 16 | 0
|
||||||
perforNow = Math.floor(perforNow / 16)
|
perforNow = Math.floor(perforNow / 16)
|
||||||
}
|
}
|
||||||
return (c === "x" ? random : (random & 0x3) | 0x8).toString(16)
|
return (c === "x" ? random : (random & 0x3) | 0x8).toString(16)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
import type { PostVisibility } from "./types"
|
import type { PostVisibility } from "./types"
|
||||||
|
|
||||||
export default function getPostPath(visibility: PostVisibility, id: string) {
|
export default function getPostPath(visibility: PostVisibility, id: string) {
|
||||||
switch (visibility) {
|
switch (visibility) {
|
||||||
case "private":
|
case "private":
|
||||||
return `/post/private/${id}`
|
return `/post/private/${id}`
|
||||||
case "protected":
|
case "protected":
|
||||||
return `/post/protected/${id}`
|
return `/post/protected/${id}`
|
||||||
case "unlisted":
|
case "unlisted":
|
||||||
case "public":
|
case "public":
|
||||||
return `/post/${id}`
|
return `/post/${id}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,19 +1,19 @@
|
|||||||
import { useRef, useEffect } from "react"
|
import { useRef, useEffect } from "react"
|
||||||
|
|
||||||
function useTraceUpdate(props: { [key: string]: any }) {
|
function useTraceUpdate(props: { [key: string]: any }) {
|
||||||
const prev = useRef(props)
|
const prev = useRef(props)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const changedProps = Object.entries(props).reduce((ps, [k, v]) => {
|
const changedProps = Object.entries(props).reduce((ps, [k, v]) => {
|
||||||
if (prev.current[k] !== v) {
|
if (prev.current[k] !== v) {
|
||||||
ps[k] = [prev.current[k], v]
|
ps[k] = [prev.current[k], v]
|
||||||
}
|
}
|
||||||
return ps
|
return ps
|
||||||
}, {} as { [key: string]: any })
|
}, {} as { [key: string]: any })
|
||||||
if (Object.keys(changedProps).length > 0) {
|
if (Object.keys(changedProps).length > 0) {
|
||||||
console.log("Changed props:", changedProps)
|
console.log("Changed props:", changedProps)
|
||||||
}
|
}
|
||||||
prev.current = props
|
prev.current = props
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useTraceUpdate
|
export default useTraceUpdate
|
||||||
|
|||||||
@ -1,29 +1,29 @@
|
|||||||
export type PostVisibility = "unlisted" | "private" | "public" | "protected"
|
export type PostVisibility = "unlisted" | "private" | "public" | "protected"
|
||||||
|
|
||||||
export type ThemeProps = {
|
export type ThemeProps = {
|
||||||
theme: "light" | "dark" | string
|
theme: "light" | "dark" | string
|
||||||
changeTheme: () => void
|
changeTheme: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Document = {
|
export type Document = {
|
||||||
title: string
|
title: string
|
||||||
content: string
|
content: string
|
||||||
id: string
|
id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type File = {
|
export type File = {
|
||||||
id: string
|
id: string
|
||||||
title: string
|
title: string
|
||||||
content: string
|
content: string
|
||||||
html: string
|
html: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Files = File[]
|
type Files = File[]
|
||||||
|
|
||||||
export type Post = {
|
export type Post = {
|
||||||
id: string
|
id: string
|
||||||
title: string
|
title: string
|
||||||
description: string
|
description: string
|
||||||
visibility: PostVisibility
|
visibility: PostVisibility
|
||||||
files: Files
|
files: Files
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,33 +1,33 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from "next"
|
import { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
const getRawFile = async (req: NextApiRequest, res: NextApiResponse) => {
|
const getRawFile = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
const { id, download } = req.query
|
const { id, download } = req.query
|
||||||
const file = await fetch(`${process.env.API_URL}/files/raw/${id}`, {
|
const file = await fetch(`${process.env.API_URL}/files/raw/${id}`, {
|
||||||
headers: {
|
headers: {
|
||||||
Accept: "text/plain",
|
Accept: "text/plain",
|
||||||
"x-secret-key": process.env.SECRET_KEY || "",
|
"x-secret-key": process.env.SECRET_KEY || "",
|
||||||
Authorization: `Bearer ${req.cookies["drift-token"]}`
|
Authorization: `Bearer ${req.cookies["drift-token"]}`
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const json = await file.json()
|
const json = await file.json()
|
||||||
res.setHeader("Content-Type", "text/plain; charset=utf-8")
|
res.setHeader("Content-Type", "text/plain; charset=utf-8")
|
||||||
res.setHeader("Cache-Control", "s-maxage=86400")
|
res.setHeader("Cache-Control", "s-maxage=86400")
|
||||||
if (file.ok) {
|
if (file.ok) {
|
||||||
const data = json
|
const data = json
|
||||||
const { title, content } = data
|
const { title, content } = data
|
||||||
// serve the file raw as plain text
|
// serve the file raw as plain text
|
||||||
|
|
||||||
if (download) {
|
if (download) {
|
||||||
res.setHeader("Content-Disposition", `attachment; filename="${title}"`)
|
res.setHeader("Content-Disposition", `attachment; filename="${title}"`)
|
||||||
} else {
|
} else {
|
||||||
res.setHeader("Content-Disposition", `inline; filename="${title}"`)
|
res.setHeader("Content-Disposition", `inline; filename="${title}"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).write(content, "utf-8")
|
res.status(200).write(content, "utf-8")
|
||||||
res.end()
|
res.end()
|
||||||
} else {
|
} else {
|
||||||
res.status(404).send("File not found")
|
res.status(404).send("File not found")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default getRawFile
|
export default getRawFile
|
||||||
|
|||||||
Loading…
Reference in New Issue