Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix and various enhancement #11

Merged
merged 26 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@emotion/styled": "^11.8.1",
"@mui/icons-material": "^5.8.2",
"@mui/material": "^5.8.2",
"@mui/x-date-pickers": "^5.0.0-beta.1",
"@swc/helpers": "^0.4.2",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
Expand All @@ -16,6 +17,7 @@
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-hook-form": "^7.31.3",
"react-hook-form-mui": "^5.1.0",
"react-scripts": "5.0.1",
"react-toastify": "^9.0.4",
"swr": "^1.3.0",
Expand Down Expand Up @@ -48,10 +50,10 @@
},
"devDependencies": {
"@craco/craco": "^6.4.3",
"craco-swc": "^0.5.1",
"@types/jest": "^27.0.1",
"@types/node": "^16.7.13",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0"
"@types/react-dom": "^18.0.0",
"craco-swc": "^0.5.1"
}
}
96 changes: 75 additions & 21 deletions app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { useState, useEffect, useCallback } from 'react';
import { invoke, clipboard } from '@tauri-apps/api';
import { ToastContainer, toast, Slide } from 'react-toastify';
import { useForm, SubmitHandler } from 'react-hook-form';
import { useForm, SubmitHandler, Controller } from 'react-hook-form';
import 'react-toastify/dist/ReactToastify.css';
import dayjs from 'dayjs';
import {
Expand Down Expand Up @@ -74,7 +74,16 @@ const Home = () => {
const [linkInfos, setLinkInfos] = useState<LinkInfo[]>([]);
const [scores, setScores] = useState<LinkScoreMap[]>([]);

const { register, handleSubmit, reset } = useForm<LinkInfo>();
const {
register,
handleSubmit,
reset,
setValue,
getValues,
control,
getFieldState,
formState: { dirtyFields, isDirty },
} = useForm<LinkInfo>();
const [page, setPage] = useState(0);
const itemPerPage = 5;
const pageCount = Math.ceil(linkInfos.length / itemPerPage);
Expand Down Expand Up @@ -285,7 +294,7 @@ const Home = () => {
</List>
);
};

console.log('dirty:', dirtyFields);
return (
<>
<AppBar position='fixed'>
Expand Down Expand Up @@ -354,26 +363,71 @@ const Home = () => {
</ButtonGroup>
</Grid>
<form onSubmit={handleSubmit(createLink)}>
<TextField
fullWidth
label='url'
margin='normal'
{...register('url', { required: true })}></TextField>
<TextField
fullWidth
label='title'
margin='normal'
{...register('title', { required: true })}></TextField>
<TextField
fullWidth
label='description(optional)'
margin='normal'
{...register('desc', {
required: false,
value: '',
})}></TextField>
<Controller
control={control}
name='url'
render={({ field: { value, onChange, onBlur } }) => (
<TextField
fullWidth
label='URL'
margin='normal'
required={true}
onChange={onChange}
onBlur={onBlur}
value={value ?? ''}
/>
)}
/>

<Controller
control={control}
name='title'
render={({ field: { value, onChange, onBlur } }) => (
<TextField
fullWidth
label='Title'
margin='normal'
required={true}
onChange={onChange}
onBlur={onBlur}
value={value ?? ''}
/>
)}
/>
<Controller
control={control}
name='desc'
render={({ field: { value, onChange, onBlur } }) => (
<TextField
fullWidth
label='Description (optional)'
margin='normal'
required={false}
onChange={onChange}
onBlur={onBlur}
value={value ?? ''}
/>
)}
/>

<Button type='submit'>Create</Button>
<Button
onClick={() => {
if (isDirty && !dirtyFields.name && !dirtyFields.title) {
let url = getValues('url');
invoke('generate_link_preview', {
url: url.toString(),
}).then((val) => {
let data = val as OpenGraph;
setValue('title', data.title, { shouldDirty: true });
setValue('desc', data.description, { shouldDirty: true });
console.log(dirtyFields);
});
}
}}
color='error'>
Auto Filled
</Button>
</form>
</Grid>
</Grid>
Expand Down
23 changes: 20 additions & 3 deletions core/src/base/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub struct Link {
desc: String,
url: Url,

// Only shared on desktop
// Surely it is little bit hack but we need to share the item between Rust and JS, so we have to bypass it
// with such a hacky way.
#[serde(skip_serializing_if = "Option::is_none")]
created_time: Option<std::time::SystemTime>,
}

Expand Down Expand Up @@ -42,7 +46,10 @@ impl Link {
s.finish().to_string()
}
/// Write zipped file to path
pub async fn write_to_path(&self, path: PathBuf) {
///
/// Note that the `created_time` field will be omitted, in order to avoid confliction between desktop and mobile.
kirillt marked this conversation as resolved.
Show resolved Hide resolved
pub async fn write_to_path(&mut self, path: PathBuf) {
self.created_time = None;
let j = serde_json::to_string(self).unwrap();
let link_file = File::create(path).unwrap();
let mut zip = zip::ZipWriter::new(link_file);
Expand All @@ -56,7 +63,7 @@ impl Link {
.await
.unwrap_or_default();
let image_data = preview_data.fetch_image().await.unwrap_or_default();
zip.start_file("preview.png", options).unwrap();
zip.start_file("link.png", options).unwrap();
zip.write(&image_data).unwrap();
zip.finish().unwrap();
}
Expand All @@ -68,7 +75,7 @@ impl Link {
let scraper = reqwest::get(url.into()).await?.text().await?;
let html = Html::parse_document(&scraper.as_str());
Ok(OpenGraph {
title: select_og(&html, OpenGraphTag::Title),
title: select_og(&html, OpenGraphTag::Title).or(select_title(&html)),
description: select_og(&html, OpenGraphTag::Description),
url: select_og(&html, OpenGraphTag::Url),
image: select_og(&html, OpenGraphTag::Image),
Expand All @@ -89,6 +96,16 @@ fn select_og(html: &Html, tag: OpenGraphTag) -> Option<String> {

None
}

fn select_title(html: &Html) -> Option<String> {
let selector = Selector::parse("title").unwrap();

if let Some(element) = html.select(&selector).next() {
return Some(element.value().name().to_string());
}

None
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct OpenGraph {
/// Represents the "og:title" OpenGraph meta tag.
Expand Down
2 changes: 1 addition & 1 deletion core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn create_link(
Ok(val) => val,
Err(e) => return Err(e.to_string()),
};
let link = Link::new(title, desc, url);
let mut link = Link::new(title, desc, url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant mutability

let name = format!("{}.link", link.format_name());
dbg!(&name);
link.write_to_path(PathBuf::from(format!("{}/{}", &state.path, name)))
Expand Down
67 changes: 64 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@
core-js-pure "^3.20.2"
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.10", "@babel/runtime@^7.16.3", "@babel/runtime@^7.17.2", "@babel/runtime@^7.18.3", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
"@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.10", "@babel/runtime@^7.16.3", "@babel/runtime@^7.17.2", "@babel/runtime@^7.18.3", "@babel/runtime@^7.18.6", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.6.tgz#6a1ef59f838debd670421f8c7f2cbb8da9751580"
integrity sha512-t9wi7/AW6XtKahAe20Yw0/mMljKq0B1r2fPdvaAdV/KPDZewFXdaaa6K7lxmZBZ8FBNpCiAT6iHPmd6QO9bKfQ==
Expand Down Expand Up @@ -1188,6 +1188,39 @@
resolved "https://registry.yarnpkg.com/@csstools/selector-specificity/-/selector-specificity-2.0.2.tgz#1bfafe4b7ed0f3e4105837e056e0a89b108ebe36"
integrity sha512-IkpVW/ehM1hWKln4fCA3NzJU8KwD+kIOvPZA4cqxoJHtE21CCzjyp+Kxbu0i5I4tBNOlXPL9mjwnWlL0VEG4Fg==

"@date-io/core@^2.14.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@date-io/core/-/core-2.14.0.tgz#03e9b9b9fc8e4d561c32dd324df0f3ccd967ef14"
integrity sha512-qFN64hiFjmlDHJhu+9xMkdfDG2jLsggNxKXglnekUpXSq8faiqZgtHm2lsHCUuaPDTV6wuXHcCl8J1GQ5wLmPw==

"@date-io/date-fns@^2.14.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@date-io/date-fns/-/date-fns-2.14.0.tgz#92ab150f488f294c135c873350d154803cebdbea"
integrity sha512-4fJctdVyOd5cKIKGaWUM+s3MUXMuzkZaHuTY15PH70kU1YTMrCoauA7hgQVx9qj0ZEbGrH9VSPYJYnYro7nKiA==
dependencies:
"@date-io/core" "^2.14.0"

"@date-io/dayjs@^2.14.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@date-io/dayjs/-/dayjs-2.14.0.tgz#8d4e93e1d473bb5f25210866204dc33384ca4c20"
integrity sha512-4fRvNWaOh7AjvOyJ4h6FYMS7VHLQnIEeAV5ahv6sKYWx+1g1UwYup8h7+gPuoF+sW2hTScxi7PVaba2Jk/U8Og==
dependencies:
"@date-io/core" "^2.14.0"

"@date-io/luxon@^2.14.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@date-io/luxon/-/luxon-2.14.0.tgz#cd1641229e00a899625895de3a31e3aaaf66629f"
integrity sha512-KmpBKkQFJ/YwZgVd0T3h+br/O0uL9ZdE7mn903VPAG2ZZncEmaUfUdYKFT7v7GyIKJ4KzCp379CRthEbxevEVg==
dependencies:
"@date-io/core" "^2.14.0"

"@date-io/moment@^2.14.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@date-io/moment/-/moment-2.14.0.tgz#8300abd6ae8c55d8edee90d118db3cef0b1d4f58"
integrity sha512-VsoLXs94GsZ49ecWuvFbsa081zEv2xxG7d+izJsqGa2L8RPZLlwk27ANh87+SNnOUpp+qy2AoCAf0mx4XXhioA==
dependencies:
"@date-io/core" "^2.14.0"

"@emotion/babel-plugin@^11.7.1":
version "11.9.2"
resolved "https://registry.yarnpkg.com/@emotion/babel-plugin/-/babel-plugin-11.9.2.tgz#723b6d394c89fb2ef782229d92ba95a740576e95"
Expand Down Expand Up @@ -1688,7 +1721,7 @@
resolved "https://registry.yarnpkg.com/@mui/types/-/types-7.1.4.tgz#4185c05d6df63ec673cda15feab80440abadc764"
integrity sha512-uveM3byMbthO+6tXZ1n2zm0W3uJCQYtwt/v5zV5I77v2v18u0ITkb8xwhsDD2i3V2Kye7SaNR6FFJ6lMuY/WqQ==

"@mui/utils@^5.9.0":
"@mui/utils@^5.4.1", "@mui/utils@^5.9.0":
version "5.9.0"
resolved "https://registry.yarnpkg.com/@mui/utils/-/utils-5.9.0.tgz#2e1ac58905b767de47412cb32475862875b8e880"
integrity sha512-GAaiWP6zBC3RE1NHP9y1c1iKZh5s/nyKKqWxfTrw5lNQY5tWTh9/47F682FuiE5WT1o3h4w/LEkSSIZpMEDzrA==
Expand All @@ -1699,6 +1732,24 @@
prop-types "^15.8.1"
react-is "^18.2.0"

"@mui/x-date-pickers@^5.0.0-beta.1":
version "5.0.0-beta.1"
resolved "https://registry.yarnpkg.com/@mui/x-date-pickers/-/x-date-pickers-5.0.0-beta.1.tgz#d681249066c81d73f6ca88d500f77e922ae2e7c5"
integrity sha512-Vx/mxnCDTQ8a5tMcRpku49+YgDl91XS5FJ0ZjPWR7JQOW8EEurDmVreNESrrIugTzQhSUp4eJXdGmncwxzwI9A==
dependencies:
"@babel/runtime" "^7.18.6"
"@date-io/core" "^2.14.0"
"@date-io/date-fns" "^2.14.0"
"@date-io/dayjs" "^2.14.0"
"@date-io/luxon" "^2.14.0"
"@date-io/moment" "^2.14.0"
"@mui/utils" "^5.4.1"
"@types/react-transition-group" "^4.4.5"
clsx "^1.2.1"
prop-types "^15.7.2"
react-transition-group "^4.4.2"
rifm "^0.12.1"

"@next/env@12.2.2":
version "12.2.2"
resolved "https://registry.yarnpkg.com/@next/env/-/env-12.2.2.tgz#cc1a0a445bd254499e30f632968c03192455f4cc"
Expand Down Expand Up @@ -7742,7 +7793,7 @@ prompts@^2.0.1, prompts@^2.4.2:
kleur "^3.0.3"
sisteransi "^1.0.5"

prop-types@^15.6.2, prop-types@^15.8.1:
prop-types@^15.6.2, prop-types@^15.7.2, prop-types@^15.8.1:
version "15.8.1"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==
Expand Down Expand Up @@ -7875,6 +7926,11 @@ react-error-overlay@^6.0.11:
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.11.tgz#92835de5841c5cf08ba00ddd2d677b6d17ff9adb"
integrity sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==

react-hook-form-mui@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/react-hook-form-mui/-/react-hook-form-mui-5.1.0.tgz#2025a5eab14c61bfbf9e704c90a78f3cc7e0a93a"
integrity sha512-fRBk9l7IuMfWLsK4IHEhoN7eVJuGpzpQV4zqdOv+gcda2zxJ1zidrshcf2SVRP1tebOx4hki/wayxkLjuW1Hdw==

react-hook-form@^7.31.3:
version "7.33.1"
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.33.1.tgz#8c4410e3420788d3b804d62cc4c142915c2e46d0"
Expand Down Expand Up @@ -8189,6 +8245,11 @@ reusify@^1.0.4:
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==

rifm@^0.12.1:
version "0.12.1"
resolved "https://registry.yarnpkg.com/rifm/-/rifm-0.12.1.tgz#8fa77f45b7f1cda2a0068787ac821f0593967ac4"
integrity sha512-OGA1Bitg/dSJtI/c4dh90svzaUPt228kzFsUkJbtA2c964IqEAwWXeL9ZJi86xWv3j5SMqRvGULl7bA6cK0Bvg==

rimraf@^3.0.0, rimraf@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
Expand Down