Skip to content

Commit

Permalink
fix: add feed when site url not match
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Sep 10, 2024
1 parent b4f9b1b commit 1a2190c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/renderer/src/components/ui/divider/Divider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const DividerVertical: FC<
return (
<span
className={cn(
"mx-4 inline-block h-full w-[0.5px] select-none bg-black !bg-opacity-30 text-transparent dark:bg-white",
"mx-4 inline-block h-full w-[0.5px] select-none bg-black text-transparent !opacity-20 dark:bg-white",
className,
)}
{...rest}
Expand Down
10 changes: 7 additions & 3 deletions src/renderer/src/modules/discover/feed-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ export const FeedForm: Component<{
asWidget?: boolean

onSuccess?: () => void
}> = ({ id, defaultValues = defaultValue, url, asWidget, onSuccess }) => {
const queryParams = { id, url }
}> = ({ id: _id, defaultValues = defaultValue, url, asWidget, onSuccess }) => {
const queryParams = { id: _id, url }
const feedQuery = useFeed(queryParams)

const feed = useFeedByIdOrUrl(queryParams)
const id = feedQuery.data?.feed.id || _id
const feed = useFeedByIdOrUrl({
id,
url,
})

const hasSub = useSubscriptionByFeedId(feed?.id || "")
const isSubscribed = !!feedQuery.data?.subscription || hasSub
Expand Down
31 changes: 19 additions & 12 deletions src/renderer/src/modules/discover/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,34 @@ const info: Record<
string,
{
label: string
prefix?: string
prefix?: string[]
showModal?: boolean
default?: string
}
> = {
search: {
label: "Any URL or Keyword",
},
rss: {
label: "RSS URL",
prefix: "https://",
default: "https://",
prefix: ["https://", "http://"],
showModal: true,
},
rsshub: {
label: "RSSHub Route",
prefix: "rsshub://",
prefix: ["rsshub://"],
default: "rsshub://",
showModal: true,
},
}

export function DiscoverForm({ type }: { type: string }) {
const { prefix } = info[type]
const { prefix, default: defaultValue } = info[type]
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
keyword: prefix || "",
keyword: defaultValue || "",
},
})
const mutation = useMutation({
Expand Down Expand Up @@ -95,14 +98,18 @@ export function DiscoverForm({ type }: { type: string }) {

const keyword = form.watch("keyword")
useEffect(() => {
if (prefix) {
if (!keyword.startsWith(prefix)) {
form.setValue("keyword", prefix)
} else if (keyword.startsWith(`${prefix}${prefix}`)) {
form.setValue("keyword", keyword.slice(prefix.length))
}
if (!prefix) return
const isValidPrefix = prefix.find((p) => keyword.startsWith(p))
if (!isValidPrefix) {
form.setValue("keyword", prefix[0])

return
}

if (keyword.startsWith(`${isValidPrefix}${isValidPrefix}`)) {
form.setValue("keyword", keyword.slice(isValidPrefix.length))
}
}, [keyword])
}, [form, keyword, prefix])

return (
<>
Expand Down
15 changes: 12 additions & 3 deletions src/renderer/src/store/feed/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ class FeedActions {
},
})

this.upsertMany([res.data.feed])

return res.data
const nonce = nanoid(8)

const finalData = {
...res.data.feed,
id: id || res.data.feed.id || nonce,
}
this.upsertMany([finalData])

return {
...res.data,
feed: finalData,
}
}
}
export const feedActions = new FeedActions()
Expand Down

0 comments on commit 1a2190c

Please sign in to comment.